Question 21
EasyHow can the space for computing Fibonacci numbers bottom-up be reduced from O(n) to O(1)?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
How can the space for computing Fibonacci numbers bottom-up be reduced from O(n) to O(1)?
Use a hash map
Use recursion
Sort the table
Keep only the last two values instead of the whole table
Sign up free to see the answer and a step-by-step explanation.
Which of these is NOT usually solved with dynamic programming?
Binary search in a sorted array
0/1 knapsack
Longest common subsequence
Edit distance
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
dp = [1] + [0] * 4
for c in [1, 2]:
for a in range(c, 5):
dp[a] += dp[a - c]
print(dp[4])
2
4
Sign up free to see the answer and a step-by-step explanation.
Which technique is used to find the best order for multiplying a chain of matrices?
Greedy choice of the smallest matrix
Binary search
Dynamic programming
Breadth-first search
Sign up free to see the answer and a step-by-step explanation.
Why is the dynamic-programming version of Fibonacci much faster than the plain recursive one?
Each value is computed only once and reused
It uses a faster computer
It uses floating-point numbers
It skips half the numbers
Sign up free to see the answer and a step-by-step explanation.
How many ways are there to make 5 using coins of 1, 2 and 5 (order doesn't matter)?
5
3
4
6
Sign up free to see the answer and a step-by-step explanation.
What is the main cost of memoisation?
Extra memory to store the results of subproblems
It gives wrong answers
It needs a sorted input
It makes every call slower
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
coins, amt = [1, 2, 5], 11
INF = float('inf')
dp = [0] + [INF] * amt
for x in range(1, amt + 1):
for c in coins:
if c <= x:
dp[x] = min(dp[x], dp[x - c] + 1)
print(dp[amt])
2
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
coins, amt = [1, 2, 5], 5
dp = [1] + [0] * amt
for c in coins:
for x in range(c, amt + 1):
dp[x] += dp[x - c]
print(dp[amt])
2
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [10, 9, 2, 5, 3, 7, 101, 18]
dp = [1] * len(a)
for i in range(len(a)):
for j in range(i):
if a[j] < a[i]:
dp[i] = max(dp[i], dp[j] + 1)
print(max(dp))
2
3
Sign up free to see the answer and a step-by-step explanation.
3
5
-1
3
4
5
4
5