Question 1
EasyWhich two properties make a problem suitable for dynamic programming?
- 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
Which two properties make a problem suitable for dynamic programming?
Sorting and searching
Recursion and loops
Overlapping subproblems and optimal substructure
Greedy choice and sorting
Answer: C
DP stores answers to repeated subproblems and builds the optimum from optimal sub-solutions.
What is memoisation?
A sorting technique
Bottom-up DP using tables
Writing notes in code
Top-down DP: caching the results of recursive calls
Answer: D
Tabulation is the bottom-up alternative that fills a table iteratively.
What is the output of this code?
a, b = 0, 1
for _ in range(10):
a, b = b, a + b
print(a)
34
55
Answer: B
Bottom-up Fibonacci keeps only the last two values: F(10) = 55.
What is the time complexity of computing the n-th Fibonacci number with DP?
O(2ⁿ)
O(n²)
O(n)
O(log n) only
Answer: C
Each value from 2 to n is computed once from the previous two.
What is the output of this code?
n = 5
ways = [1, 1] + [0] * (n - 1)
for i in range(2, n + 1):
ways[i] = ways[i - 1] + ways[i - 2]
print(ways[n])
13
5
Answer: D
Climbing 5 stairs taking 1 or 2 steps: ways(n) = ways(n − 1) + ways(n − 2), giving 1, 1, 2, 3, 5, 8.
Which of these problems is typically solved with dynamic programming?
Reversing a string
0/1 knapsack
Finding the maximum of an array
Binary search
Answer: B
Each item is taken or not; DP over (item, capacity) avoids trying all 2ⁿ subsets.
What is tabulation in DP?
Top-down recursion with caching
Printing a table
Using a hash table for the input
Filling a table bottom-up from the smallest subproblems
Answer: D
It avoids recursion overhead and stack limits.
What does it mean that a problem has overlapping subproblems?
Its subproblems are all different
The same smaller subproblems are solved again and again
It has no subproblems
Its input overlaps with its output
Answer: B
Storing each subproblem's answer avoids solving it repeatedly.
What does optimal substructure mean?
The structure of the code is optimal
The problem has a single solution
The input is already sorted
An optimal solution can be built from optimal solutions to its subproblems
Answer: D
For example, a shortest path is made of shortest sub-paths.
Where does bottom-up dynamic programming (tabulation) start filling its table?
At random
From the smallest subproblems
From the middle
From the largest subproblem
Answer: B
Each entry is computed from entries that are already filled.
89
21
6
8