Bird
Raised Fist0
DSA Javaprogramming~20 mins

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

Choose your learning style10 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 with Memoization
What is the output of the following Java code that calculates Fibonacci numbers using memoization?
DSA Java
import java.util.HashMap;

public class FibMemo {
    private static HashMap<Integer, Integer> memo = new HashMap<>();

    public static int fib(int n) {
        if (n <= 1) return n;
        if (memo.containsKey(n)) return memo.get(n);
        int result = fib(n - 1) + fib(n - 2);
        memo.put(n, result);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(fib(6));
    }
}
A8
B21
C13
D5
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13...
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Coin Change Count
What is the output of this Java code that counts ways to make change using recursion?
DSA Java
public class CoinChange {
    public static int countWays(int[] coins, int n, int sum) {
        if (sum == 0) return 1;
        if (sum < 0 || n <= 0) return 0;
        return countWays(coins, n - 1, sum) + countWays(coins, n, sum - coins[n - 1]);
    }

    public static void main(String[] args) {
        int[] coins = {1, 2, 3};
        System.out.println(countWays(coins, coins.length, 4));
    }
}
A7
B4
C6
D5
Attempts:
2 left
💡 Hint
Think about all combinations of coins that sum to 4.
🔧 Debug
advanced
2:00remaining
Identify the error in this memoized Fibonacci code
What error will this Java code produce when run?
DSA Java
import java.util.HashMap;

public class FibMemoError {
    private static HashMap<Integer, Integer> memo = new HashMap<>();

    public static int fib(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        if (memo.containsKey(n)) return memo.get(n);
        int result = fib(n - 1) + fib(n - 2);
        memo.put(n, result);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(fib(-1));
    }
}
AStackOverflowError
B0
CIllegalArgumentException
DNo output, program hangs
Attempts:
2 left
💡 Hint
Consider what happens when fib is called with negative input.
🧠 Conceptual
advanced
1:30remaining
Which property defines Optimal Substructure?
Which statement best describes the property of optimal substructure in dynamic programming?
AA problem requires exponential time to solve without memoization.
BA problem has overlapping subproblems that are solved multiple times.
CA problem can be divided into subproblems whose solutions can be combined to solve the original problem optimally.
DA problem can be solved only by brute force methods.
Attempts:
2 left
💡 Hint
Optimal substructure means the best solution uses best solutions of parts.
🚀 Application
expert
2:30remaining
Number of Unique Paths in a Grid with Memoization
What is the output of this Java code that calculates the number of unique paths from top-left to bottom-right in a 3x3 grid using memoization?
DSA Java
import java.util.HashMap;

public class UniquePaths {
    private static HashMap<String, Integer> memo = new HashMap<>();

    public static int countPaths(int r, int c) {
        if (r == 0 && c == 0) return 1;
        if (r < 0 || c < 0) return 0;
        String key = r + "," + c;
        if (memo.containsKey(key)) return memo.get(key);
        int paths = countPaths(r - 1, c) + countPaths(r, c - 1);
        memo.put(key, paths);
        return paths;
    }

    public static void main(String[] args) {
        System.out.println(countPaths(2, 2));
    }
}
A5
B4
C8
D6
Attempts:
2 left
💡 Hint
Count paths moving only right or down in a 3x3 grid (indices 0 to 2).

Practice

(1/5)
1. Which of the following best describes the overlapping subproblems property in dynamic programming?
easy
A. The problem has no repeated subproblems and each subproblem is unique.
B. The problem can be broken into smaller subproblems that are solved multiple times.
C. The solution requires sorting the input data first.
D. The problem can be solved using a greedy approach without recursion.

Solution

  1. Step 1: Understand overlapping subproblems

    Overlapping subproblems means the same smaller problems appear multiple times during solving.
  2. Step 2: Match with options

    The problem can be broken into smaller subproblems that are solved multiple times. correctly states that subproblems repeat and are solved multiple times.
  3. Final Answer:

    The problem can be broken into smaller subproblems that are solved multiple times. -> Option B
  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
  • Mixing greedy approach with dynamic programming
2. Which of the following Java method signatures correctly represents a memoized Fibonacci function?
easy
A. public int fib(int n, int[] memo)
B. public void fib(int n)
C. public int fib(String n, int[] memo)
D. public int fib(int n, String[] memo)

Solution

  1. Step 1: Identify correct parameter types

    Memoization requires storing results, so an int array for memo is needed. The input n should be int.
  2. Step 2: Check method signatures

    public int fib(int n, int[] memo) uses int n and int[] memo, which is correct for memoized Fibonacci.
  3. Final Answer:

    public int fib(int n, int[] memo) -> Option A
  4. Quick Check:

    Memo array type matches input type [OK]
Hint: Memo array must match return type and input must be int [OK]
Common Mistakes:
  • Using void return type for Fibonacci function
  • Using String type for numeric input or memo array
  • Not passing memo array as parameter
3. What is the output of the following Java code snippet implementing a simple memoized Fibonacci?
public class FibMemo {
    static int[] memo = new int[6];
    public static 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];
    }
    public static void main(String[] args) {
        System.out.println(fib(5));
    }
}
medium
A. 8
B. 0
C. 5
D. StackOverflowError

Solution

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

    fib(5) calls fib(4) and fib(3), and so on. Memo stores results to avoid repeats.
  2. Step 2: Calculate fib(5)

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

    5 -> Option C
  4. Quick Check:

    Fibonacci(5) = 5 [OK]
Hint: Remember Fibonacci sequence: 0,1,1,2,3,5... [OK]
Common Mistakes:
  • Confusing fib(5) with fib(6)
  • Ignoring memoization effect
  • Expecting sum of all calls instead of fib(5)
4. Identify the error in this Java code snippet for computing Fibonacci with memoization:
public class Fib {
    static int[] memo = new int[5];
    public static 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];
    }
    public static void main(String[] args) {
        System.out.println(fib(5));
    }
}
medium
A. No error, prints correct Fibonacci number
B. StackOverflowError due to infinite recursion
C. Compilation error due to missing return
D. ArrayIndexOutOfBoundsException due to memo size

Solution

  1. Step 1: Check memo array size and access

    Memo array size is 5, so valid indices are 0 to 4. fib(5) tries to access memo[5], causing error.
  2. Step 2: Identify exception type

    Accessing memo[5] causes ArrayIndexOutOfBoundsException at runtime.
  3. Final Answer:

    ArrayIndexOutOfBoundsException due to memo size -> Option D
  4. Quick Check:

    Array size < access index = error [OK]
Hint: Check array size matches max index accessed [OK]
Common Mistakes:
  • Assuming memo size is enough without checking
  • Thinking recursion causes stack overflow here
  • Ignoring runtime exceptions
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 overlapping subproblems and optimal substructure.
B. The problem can be solved by sorting all paths.
C. The problem requires only greedy choices without recursion.
D. The problem has no repeated subproblems and no optimal substructure.

Solution

  1. Step 1: Understand minimum cost path problem

    The problem breaks into smaller subproblems (cost to reach each cell) that overlap.
  2. Step 2: Check properties for dynamic programming

    Optimal substructure means optimal solution uses optimal solutions of subproblems. Overlapping subproblems means subproblems repeat.
  3. Step 3: Match with options

    The problem has overlapping subproblems and optimal substructure. correctly states both properties, justifying dynamic programming.
  4. Final Answer:

    The problem has overlapping subproblems and optimal substructure. -> Option A
  5. Quick Check:

    Overlapping subproblems + optimal substructure = DP [OK]
Hint: Look for repeated subproblems and optimal smaller solutions [OK]
Common Mistakes:
  • Thinking sorting solves the problem
  • Assuming greedy approach always works
  • Ignoring overlapping subproblems
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