0%
0 / 13 answered
Algorithmic Efficiency Practice Test
•13 QuestionsQuestion
1 / 13
Q1
A smartwatch app computes Fibonacci numbers for animations; it may request n up to 45, and each computation must finish under 0.1 seconds. Two approaches are given:
Pseudocode (Naive Recursion):
FIB_RECURSIVE(n)
IF n <= 1
RETURN n
RETURN FIB_RECURSIVE(n-1) + FIB_RECURSIVE(n-2)
Time complexity: $O(2^n)$
Pseudocode (Dynamic Programming):
FIB_DP(n)
IF n <= 1
RETURN n
prev <- 0
curr <- 1
FOR i <- 2 TO n
next <- prev + curr
prev <- curr
curr <- next
RETURN curr
Time complexity: $O(n)$
Considering the time complexities mentioned, which algorithm has a lower time complexity for large inputs?
A smartwatch app computes Fibonacci numbers for animations; it may request n up to 45, and each computation must finish under 0.1 seconds. Two approaches are given:
Pseudocode (Naive Recursion):
FIB_RECURSIVE(n)
IF n <= 1
RETURN n
RETURN FIB_RECURSIVE(n-1) + FIB_RECURSIVE(n-2)
Time complexity: $O(2^n)$
Pseudocode (Dynamic Programming):
FIB_DP(n)
IF n <= 1
RETURN n
prev <- 0
curr <- 1
FOR i <- 2 TO n
next <- prev + curr
prev <- curr
curr <- next
RETURN curr
Time complexity: $O(n)$
Considering the time complexities mentioned, which algorithm has a lower time complexity for large inputs?