Challenge - 5 Problems
Tabulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bottom-Up DP Fibonacci Implementation
What is the output of this Rust code that uses bottom-up tabulation to compute Fibonacci numbers?
DSA Rust
fn fibonacci(n: usize) -> usize {
let mut dp = vec![0; n + 1];
dp[0] = 0;
if n > 0 {
dp[1] = 1;
}
for i in 2..=n {
dp[i] = dp[i - 1] + dp[i - 2];
}
dp[n]
}
fn main() {
println!("{}", fibonacci(7));
}Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21...
✗ Incorrect
The code computes Fibonacci numbers using bottom-up DP. fibonacci(7) returns dp[7], which is the 7th Fibonacci number (0-indexed): 0,1,1,2,3,5,8,13. So output is 13.
❓ Predict Output
intermediateOutput of Bottom-Up DP for Coin Change Ways
What is the output of this Rust code that calculates the number of ways to make change for amount 5 using coins [1, 2, 5] with bottom-up DP?
DSA Rust
fn coin_change_ways(coins: &[usize], amount: usize) -> usize {
let mut dp = vec![0; amount + 1];
dp[0] = 1;
for &coin in coins {
for x in coin..=amount {
dp[x] += dp[x - coin];
}
}
dp[amount]
}
fn main() {
println!("{}", coin_change_ways(&[1, 2, 5], 5));
}Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.
✗ Incorrect
The code counts ways to make 5 using coins 1,2,5. The ways are: 5; 2+2+1; 2+1+1+1; 1+1+1+1+1. Total 4 ways.
🔧 Debug
advancedIdentify the Error in Bottom-Up DP for Longest Increasing Subsequence
What is the output of this Rust code that attempts to find the length of the longest increasing subsequence using bottom-up DP? Note the bug in the update logic.
DSA Rust
fn length_of_lis(nums: &[i32]) -> usize {
let n = nums.len();
let mut dp = vec![1; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] {
dp[i] = dp[i].max(dp[j] + 1);
}
}
}
*dp.iter().max().unwrap()
}
fn main() {
let nums = vec![10,9,2,5,3,7,101,18];
println!("{}", length_of_lis(&nums));
}Attempts:
2 left
💡 Hint
Check how dp[i] is updated inside the inner loop. It lacks a max comparison.
✗ Incorrect
The code outputs 4, which is correct for this input (LIS length is 4, e.g., 2<3<7<18). However, it has a bug: dp[i] is set to dp[j]+1 without comparing to current dp[i] (should be dp[i] = dp[i].max(dp[j] + 1)). It takes the dp from the last qualifying j, not the maximum. This fails on other inputs, e.g., [10,20,1,30] outputs 2 instead of 3.
🧠 Conceptual
advancedUnderstanding Time Complexity of Bottom-Up DP
What is the time complexity of the bottom-up DP solution for the 0/1 Knapsack problem with n items and capacity W?
Attempts:
2 left
💡 Hint
Consider the nested loops over items and capacity.
✗ Incorrect
The bottom-up DP uses a 2D table of size n by W, filling each cell once, so time complexity is O(n * W).
🚀 Application
expertFinal DP Table State for Edit Distance Problem
Given the strings s1 = "kitten" and s2 = "sitting", what is the value in the bottom-right cell of the DP table after running the bottom-up edit distance algorithm?
Attempts:
2 left
💡 Hint
Edit distance counts minimum insertions, deletions, and substitutions to convert s1 to s2.
✗ Incorrect
The edit distance between "kitten" and "sitting" is 3: substitute 'k'->'s', substitute 'e'->'i', insert 'g'.
