Challenge - 5 Problems
Master of 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 Python code that uses recursion with memoization to compute Fibonacci numbers?
DSA Python
def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] print(fib(6))
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13...
✗ Incorrect
The function computes Fibonacci numbers using recursion and memoization. fib(6) returns the 6th Fibonacci number which is 8.
🧠 Conceptual
intermediateChoosing Greedy vs DP for Coin Change Problem
Which statement correctly describes when a greedy algorithm works for the coin change problem?
Attempts:
2 left
💡 Hint
Think about US coin denominations and why greedy works there.
✗ Incorrect
Greedy works for coin change only if the coin denominations are canonical, meaning the greedy choice leads to an optimal solution. For example, US coins (1,5,10,25) satisfy this.
❓ Predict Output
advancedOutput of DP Longest Increasing Subsequence Length
What is the output of the following Python code that finds the length of the longest increasing subsequence using dynamic programming?
DSA Python
def length_of_LIS(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp) print(length_of_LIS([10,9,2,5,3,7,101,18]))
Attempts:
2 left
💡 Hint
Find the longest increasing subsequence in the list.
✗ Incorrect
The longest increasing subsequence is [2,3,7,101] which has length 4.
❓ Predict Output
advancedOutput of Greedy Interval Scheduling Algorithm
What is the output (number of intervals selected) of the following greedy algorithm that selects the maximum number of non-overlapping intervals?
DSA Python
intervals = [(1,3), (2,5), (4,7), (6,9), (8,10)] intervals.sort(key=lambda x: x[1]) count = 0 end = float('-inf') for interval in intervals: if interval[0] >= end: count += 1 end = interval[1] print(count)
Attempts:
2 left
💡 Hint
Sort intervals by finish time and pick compatible ones.
✗ Incorrect
The greedy algorithm picks intervals (1,3), (4,7), and (8,10) for a total of 3 intervals.
🧠 Conceptual
expertWhy DP is Needed Over Greedy in Weighted Interval Scheduling
Why does the weighted interval scheduling problem require dynamic programming instead of a greedy approach?
Attempts:
2 left
💡 Hint
Think about why just picking earliest finishing intervals might not maximize total weight.
✗ Incorrect
Weighted interval scheduling requires considering overlapping intervals and their weights, so we must consider multiple combinations to find the maximum total weight, which greedy cannot do.
