0
0
DSA Cprogramming~20 mins

Overlapping Subproblems and Optimal Substructure in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Overlapping Subproblems and Optimal Substructure
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fibonacci Recursive Calls
What is the output of the following C code that prints Fibonacci numbers using recursion with overlapping subproblems?
DSA C
#include <stdio.h>

int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}

int main() {
    for (int i = 0; i < 5; i++) {
        printf("%d ", fib(i));
    }
    return 0;
}
A0 1 1 2 3
B1 1 2 3 5
C0 1 2 3 5
D1 2 3 5 8
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0 and 1 for fib(0) and fib(1).
🧠 Conceptual
intermediate
1:30remaining
Identifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems in dynamic programming?
ASorting an array using quicksort
BFinding the shortest path in a graph using Dijkstra's algorithm
CCalculating factorial of a number using recursion
DComputing Fibonacci numbers using naive recursion
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same smaller problem is solved multiple times.
🔧 Debug
advanced
2:30remaining
Debugging Memoization Implementation
What is the output of this C code with memoization for Fibonacci, and why?
DSA C
#include <stdio.h>

int memo[10] = {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() {
    for (int i = 0; i < 7; i++) {
        printf("%d ", fib(i));
    }
    return 0;
}
A0 1 1 2 3 5 8
B0 1 2 3 5 8 13
C0 1 1 2 3 5 0
D0 1 1 2 3 5 21
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
🚀 Application
advanced
2:00remaining
Optimal Substructure in Coin Change Problem
Given unlimited coins of denominations 1, 3, and 4, what is the minimum number of coins needed to make amount 6 using optimal substructure?
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Try breaking amount 6 into smaller amounts and combine optimal solutions.
🧠 Conceptual
expert
2:30remaining
Recognizing Problems Without Optimal Substructure
Which problem does NOT exhibit optimal substructure, meaning its optimal solution cannot be constructed from optimal solutions of subproblems?
AShortest path in a weighted graph without negative cycles
BMatrix chain multiplication
CLongest simple path in a graph
DKnapsack problem with fractional items
Attempts:
2 left
💡 Hint
Optimal substructure means combining optimal subproblem solutions gives global optimum.