Challenge - 5 Problems
Mastering DP vs Recursion vs Greedy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci with Memoization
What is the output of the following Java code that uses recursion with memoization to calculate Fibonacci numbers?
DSA Java
import java.util.*; public class FibMemo { static Map<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)); } }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0,1 and each next number is sum of previous two.
✗ Incorrect
The Fibonacci sequence is 0,1,1,2,3,5,8,... fib(6) is the 6th Fibonacci number starting from 0 index, which is 8.
🧠 Conceptual
intermediateChoosing Greedy vs DP Approach
Which of the following problem characteristics best indicates that a greedy algorithm is the right choice over dynamic programming?
Attempts:
2 left
💡 Hint
Greedy algorithms work when making local optimal choices leads to a global optimum.
✗ Incorrect
Greedy algorithms work well when the problem has both optimal substructure and the greedy choice property, meaning local choices lead to global optimum.
🔧 Debug
advancedWhy Does This Recursive Solution Cause Stack Overflow?
Consider this recursive Java method to calculate factorial:
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
What is the main reason this code might cause a stack overflow error for large n?
Attempts:
2 left
💡 Hint
Think about how recursion uses the call stack and what happens with large inputs.
✗ Incorrect
Deep recursion calls add stack frames; for large n, this can exceed stack size causing overflow.
❓ Predict Output
advancedOutput of Greedy Interval Scheduling
What is the output of this Java code that selects the maximum number of non-overlapping intervals using a greedy approach?
DSA Java
import java.util.*; public class IntervalScheduling { static class Interval implements Comparable<Interval> { int start, end; Interval(int s, int e) { start = s; end = e; } public int compareTo(Interval other) { return this.end - other.end; } } public static int maxIntervals(Interval[] intervals) { Arrays.sort(intervals); int count = 0, lastEnd = Integer.MIN_VALUE; for (Interval i : intervals) { if (i.start >= lastEnd) { count++; lastEnd = i.end; } } return count; } public static void main(String[] args) { Interval[] intervals = { new Interval(1, 3), new Interval(2, 5), new Interval(4, 7), new Interval(6, 9), new Interval(8, 10) }; System.out.println(maxIntervals(intervals)); } }
Attempts:
2 left
💡 Hint
Greedy interval scheduling picks intervals by earliest finish time without overlap.
✗ Incorrect
The selected intervals are (1,3), (4,7), (8,10). (2,5) overlaps with (1,3), (6,9) overlaps with (4,7) so not chosen. Maximum count is 3.
🧠 Conceptual
expertWhen to Prefer Dynamic Programming Over Greedy
Which scenario best explains when dynamic programming is necessary instead of a greedy approach?
Attempts:
2 left
💡 Hint
Think about when greedy fails to find the best solution and you need to consider all possibilities.
✗ Incorrect
Dynamic programming is needed when greedy choices can lead to suboptimal solutions because local choices don't guarantee global optimum.
