What does Overlapping Subproblems mean in dynamic programming?
It means the problem can be broken down into smaller subproblems which are solved multiple times. Instead of solving them again, we store their results to save time.
Click to reveal answer
beginner
Define Optimal Substructure in simple words.
A problem has optimal substructure if the best solution to the problem can be made by combining the best solutions to its smaller parts.
Click to reveal answer
intermediate
Why is Overlapping Subproblems important for dynamic programming?
Because it allows us to use memoization or tabulation to avoid repeated work, making the solution faster.
Click to reveal answer
intermediate
Give an example of a problem that shows both overlapping subproblems and optimal substructure.
The Fibonacci sequence problem: calculating Fibonacci numbers involves solving the same smaller Fibonacci problems multiple times (overlapping subproblems), and the best solution for a number depends on the best solutions of the previous two numbers (optimal substructure).
Click to reveal answer
intermediate
How does Optimal Substructure help in designing recursive solutions?
It ensures that solving smaller parts optimally leads to the overall optimal solution, so we can build the answer step-by-step using recursion.
Click to reveal answer
Which of the following best describes overlapping subproblems?
ASubproblems repeat and are solved multiple times
BSubproblems are always larger than the main problem
CSubproblems are unrelated to the main problem
DSubproblems are independent and solved once
✗ Incorrect
Overlapping subproblems means the same smaller problems appear multiple times and can be reused.
What does optimal substructure imply about a problem?
AThe problem cannot be divided into smaller parts
BThe best solution depends on the best solutions of smaller parts
CThe problem has no solution
DThe problem is solved only once
✗ Incorrect
Optimal substructure means the overall best solution is made from best solutions of smaller subproblems.
Which problem is a classic example of overlapping subproblems and optimal substructure?
ASorting an array
BFinding maximum in an array
CFibonacci sequence calculation
DLinear search
✗ Incorrect
Fibonacci calculation solves the same smaller Fibonacci numbers repeatedly and builds the answer from smaller solutions.
What technique uses overlapping subproblems to improve efficiency?
AMemoization
BRandom guessing
CBrute force
DSorting
✗ Incorrect
Memoization stores results of subproblems to avoid repeated work.
If a problem lacks optimal substructure, what does it mean?
AIt is always easy to solve
BWe can solve it using dynamic programming
CIt has overlapping subproblems
DThe best solution cannot be built from smaller best solutions
✗ Incorrect
Without optimal substructure, combining smaller solutions won't guarantee the best overall solution.
Explain in your own words what overlapping subproblems and optimal substructure mean and why they matter in dynamic programming.
Think about solving a problem by breaking it into smaller parts and reusing answers.
You got /4 concepts.
Describe a simple example problem that shows both overlapping subproblems and optimal substructure. Explain how these properties appear in that problem.
Consider a problem where you calculate values multiple times and combine smaller answers.
You got /3 concepts.
Practice
(1/5)
1. Which of the following best describes the property of Overlapping Subproblems in dynamic programming?
easy
A. The problem can be broken down into smaller subproblems which are solved multiple times.
B. The problem has a unique solution without repeating any subproblems.
C. The problem requires sorting before solving.
D. The problem can only be solved using recursion without memoization.
Solution
Step 1: Understand Overlapping Subproblems
Overlapping subproblems means the same smaller problems appear multiple times during the solution process.
Step 2: Match the definition to options
The problem can be broken down into smaller subproblems which are solved multiple times. correctly states that subproblems are solved multiple times, which is the key idea behind overlapping subproblems.
Final Answer:
The problem can be broken down into smaller subproblems which are solved multiple times. -> Option A
2. Which of the following C++ function signatures correctly represents a memoized Fibonacci function using dynamic programming?
easy
A. void fib(int n);
B. int fib();
C. int fib(int n);
D. int fib(int n, int memo[]);
Solution
Step 1: Identify memoization parameters
Memoization requires an extra data structure (like an array) to store results of subproblems.
Step 2: Check function signatures
int fib(int n, int memo[]); includes an array parameter to store memoized results, which is necessary for memoization.
Final Answer:
int fib(int n, int memo[]); -> Option D
Quick Check:
Memoization needs storage parameter [OK]
Hint: Memoized functions need extra storage parameter [OK]
Common Mistakes:
Ignoring the memo array parameter
Using void return type for Fibonacci
Not passing any parameters for recursion
3. What is the output of the following C++ code snippet?
int fib(int n, int memo[]) {
if (n <= 1) return n;
if (memo[n] != -1) return memo[n];
memo[n] = fib(n-1, memo) + fib(n-2, memo);
return memo[n];
}
int main() {
int n = 5;
int memo[6];
for(int i = 0; i <= n; i++) memo[i] = -1;
std::cout << fib(n, memo);
return 0;
}
medium
A. 13
B. 8
C. 3
D. 5
Solution
Step 1: Understand Fibonacci calculation
fib(5) = fib(4) + fib(3). Using memoization, fib(5) = 8.
Step 2: Trace the code execution
Memo array initialized with -1, so recursive calls compute and store values. fib(5) returns 8.
4. Identify the error in the following code snippet implementing memoized Fibonacci:
int fib(int n, int memo[]) {
if (n == 0) return 0;
if (n == 1) return 1;
if (memo[n] != 0) return memo[n];
memo[n] = fib(n-1, memo) + fib(n-2, memo);
return memo[n];
}
medium
A. The function should return void instead of int.
B. Base cases should return -1 instead of 0 or 1.
C. Memo array should be initialized with -1, not 0.
D. The memo array size is not checked inside the function.
Solution
Step 1: Check memoization condition
The code checks if memo[n] != 0 to decide if value is computed, but 0 is a valid Fibonacci value.
Step 2: Correct initialization for memo array
Memo array should be initialized with a value like -1 to indicate uncomputed states, avoiding confusion with valid 0.
Final Answer:
Memo array should be initialized with -1, not 0. -> Option C
Quick Check:
Use -1 for uninitialized memo values [OK]
Hint: Initialize memo with -1 to distinguish computed from uncomputed [OK]
Common Mistakes:
Using 0 as default memo value causing wrong cache hits
Changing base case return values incorrectly
Ignoring memo array size checks
5. Given the problem of finding the minimum cost path in a grid from top-left to bottom-right, which property justifies using dynamic programming to solve it efficiently?
hard
A. The problem has both overlapping subproblems and optimal substructure.
B. The problem has optimal substructure but no overlapping subproblems.
C. The problem has overlapping subproblems but no optimal substructure.
D. The problem has neither overlapping subproblems nor optimal substructure.
Solution
Step 1: Understand minimum cost path problem
The problem breaks into smaller subproblems (paths to cells) that overlap and solutions combine optimally.
Step 2: Identify properties
It has overlapping subproblems (same cells computed multiple times) and optimal substructure (optimal path uses optimal subpaths).
Final Answer:
The problem has both overlapping subproblems and optimal substructure. -> Option A