Bird
Raised Fist0
DSA Cprogramming~10 mins

Overlapping Subproblems and Optimal Substructure in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the nth Fibonacci number using recursion.

DSA C
int fib(int n) {
    if (n <= 1) {
        return n;
    }
    return fib(n - 1) [1] fib(n - 2);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the sum.
2fill in blank
medium

Complete the code to memoize the Fibonacci function using an array.

DSA C
int fibMemo(int n, int memo[]) {
    if (n <= 1) {
        return n;
    }
    if (memo[n] != -1) {
        return memo[n];
    }
    memo[n] = fibMemo(n - 1, memo) [1] fibMemo(n - 2, memo);
    return memo[n];
}
Drag options to blanks, or click blank then click option'
A*
B-
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators in the sum.
Not storing the result in memo[n].
3fill in blank
hard

Fix the error in the code to correctly compute the minimum cost path in a grid using recursion.

DSA C
#include <limits.h>
int minCostPath(int cost[][], int m, int n) {
    if (m == 0 && n == 0) {
        return cost[0][0];
    }
    if (m < 0 || n < 0) {
        return [1];
    }
    int left = minCostPath(cost, m, n - 1);
    int up = minCostPath(cost, m - 1, n);
    return cost[m][n] + (left < up ? left : up);
}
Drag options to blanks, or click blank then click option'
Acost[m][n]
B-1
CINT_MAX
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 for invalid indices which can incorrectly lower the cost.
Returning negative values causing wrong minimum calculations.
4fill in blank
hard

Fill both blanks to create a bottom-up dynamic programming solution for the Fibonacci sequence.

DSA C
int fibDP(int n) {
    int dp[n + 1];
    dp[0] = 0;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i [1] 1] [2] dp[i - 2];
    }
    return dp[n];
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for the index.
Using wrong operators for combining previous values.
5fill in blank
hard

Fill all three blanks to implement a memoized recursive solution for the Longest Common Subsequence (LCS).

DSA C
int lcsMemo(char *X, char *Y, int m, int n, int memo[][100]) {
    if (m == 0 || n == 0) {
        return 0;
    }
    if (memo[m][n] != -1) {
        return memo[m][n];
    }
    if (X[m - 1] [1] Y[n - 1]) {
        memo[m][n] = 1 + lcsMemo(X, Y, m - 1, n - 1, memo);
    } else {
        memo[m][n] = (lcsMemo(X, Y, m - 1, n, memo) [2] lcsMemo(X, Y, m, n - 1, memo));
    }
    return memo[m][n];
}
Drag options to blanks, or click blank then click option'
A==
B!=
Cmax
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for character comparison.
Using '+' instead of max to combine subsequences.

Practice

(1/5)
1. Which of the following best describes the concept of overlapping subproblems in dynamic programming?
easy
A. The problem requires sorting elements before solving.
B. The problem has a unique solution without repeating any subproblems.
C. The problem can be broken down into smaller subproblems which are solved multiple times.
D. The problem can be solved only using recursion without optimization.

Solution

  1. Step 1: Understand overlapping subproblems

    Overlapping subproblems means the same smaller problems appear multiple times during the solution process.
  2. Step 2: Match with 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.
  3. Final Answer:

    The problem can be broken down into smaller subproblems which are solved multiple times. -> Option C
  4. Quick Check:

    Overlapping subproblems = repeated smaller problems [OK]
Hint: Look for repeated smaller problems solved multiple times [OK]
Common Mistakes:
  • Confusing overlapping subproblems with unique subproblems
  • Thinking sorting is required for overlapping subproblems
  • Assuming recursion alone solves overlapping subproblems
2. Which of the following is the correct syntax to declare a memoization array for storing results of subproblems in C?
easy
A. int memo[] = 0;
B. int memo = new int[1000];
C. int memo(1000);
D. int memo[1000] = {0};

Solution

  1. Step 1: Understand C array declaration

    In C, arrays are declared with fixed size and can be initialized with {0} to set all elements to zero.
  2. Step 2: Check options for valid C syntax

    int memo[1000] = {0}; uses correct C syntax: int memo[1000] = {0}; initializes an array of 1000 integers to zero.
  3. Final Answer:

    int memo[1000] = {0}; -> Option D
  4. Quick Check:

    C array declaration with initialization = int memo[1000] = {0}; [OK]
Hint: Use fixed size array with braces to initialize in C [OK]
Common Mistakes:
  • Using C++ style vector or new keyword in C
  • Declaring arrays without size
  • Incorrect initialization syntax
3. Consider the following C code for Fibonacci with memoization:
int memo[100] = {0};

int fib(int n) {
    if (n <= 1) return n;
    if (memo[n] != 0) return memo[n];
    memo[n] = fib(n-1) + fib(n-2);
    return memo[n];
}

int main() {
    printf("%d", fib(5));
    return 0;
}
What is the output when this code runs?
medium
A. 0
B. 5
C. 3
D. 8

Solution

  1. Step 1: Trace fib(5) calls with memoization

    fib(5) calls fib(4) + fib(3). fib(4) calls fib(3) + fib(2), and so on. Base cases fib(0)=0, fib(1)=1.
  2. Step 2: Calculate fib(5)

    fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=fib(4)+fib(3)=3+2=5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Fibonacci(5) = 5 [OK]
Hint: Recall Fibonacci sequence: 0,1,1,2,3,5... [OK]
Common Mistakes:
  • Confusing index with Fibonacci value
  • Ignoring memoization effect
  • Miscomputing base cases
4. The following code attempts to compute the nth Fibonacci number using memoization but has a bug:
int memo[100] = {0};

int fib(int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    if (memo[n] != 0) return memo[n];
    memo[n] = fib(n-1) + fib(n-2);
    return memo[n];
}

int main() {
    printf("%d", fib(0));
    return 0;
}
What is the bug in this code?
medium
A. fib(0) returns 0 but memo[0] is never set, causing repeated calls.
B. The memo array is not initialized to zero.
C. The base case for fib(1) is missing.
D. The memoization check uses wrong index.

Solution

  1. Step 1: Analyze base cases and memo usage

    fib(0) returns 0 but does not store it in memo[0]. So memo[0] remains 0, which is the default value.
  2. Step 2: Understand effect on memoization

    Because the base case results are not stored in memo[n], fib(0) (and fib(1)) will be called repeatedly in the recursion tree for larger n, instead of returning a cached value.
  3. Final Answer:

    fib(0) returns 0 but memo[0] is never set, causing repeated calls. -> Option A
  4. Quick Check:

    Base case not stored in memo causes repeated calls [OK]
Hint: Store base case results in memo to avoid repeated calls [OK]
Common Mistakes:
  • Assuming memo array auto-initializes for base cases
  • Missing base case for fib(1)
  • Incorrect memo check condition
5. You want to find the minimum cost to reach the top of stairs where each step has a cost. You can climb 1 or 2 steps at a time. The cost array is cost[] = {10, 15, 20}. Using dynamic programming with overlapping subproblems and optimal substructure, what is the minimum cost to reach the top (beyond last step)?
hard
A. 15
B. 20
C. 25
D. 10

Solution

  1. Step 1: Define subproblems and recurrence

    Minimum cost to reach step i: dp[i] = cost[i] + min(dp[i-1], dp[i-2]). dp[0]=10, dp[1]=15.
  2. Step 2: Calculate minimum cost step by step

    dp[2] = 20 + min(10,15) = 30. To reach top (beyond last step), min(dp[1], dp[2]) = min(15,30) = 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Minimum cost to top = 15 [OK]
Hint: Use min cost of last two steps for final answer [OK]
Common Mistakes:
  • Adding all costs instead of choosing min path
  • Ignoring that you can start from step 0 or 1
  • Confusing cost to step vs cost beyond last step
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard