Challenge - 5 Problems
Tabulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci Tabulation
What is the output of the following JavaScript code that uses bottom-up tabulation to find the 6th Fibonacci number?
DSA Javascript
function fibTab(n) {
const dp = [0, 1];
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
console.log(fibTab(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 starts 0,1,1,2,3,5,8,... The 6th index (0-based) is 8.
❓ Predict Output
intermediateCount Ways to Climb Stairs
What does this code print? It counts ways to climb 4 stairs taking 1 or 2 steps at a time using bottom-up DP.
DSA Javascript
function climbStairs(n) {
const dp = Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
console.log(climbStairs(4));Attempts:
2 left
💡 Hint
Think of ways to reach step i from i-1 and i-2.
✗ Incorrect
Ways to climb 4 stairs are 5: (1,1,1,1), (1,1,2), (1,2,1), (2,1,1), (2,2).
🔧 Debug
advancedIdentify the Error in Tabulation Code
What error will this code produce when run? It tries to find minimum coins for amount 5 with coins [1,3,4].
DSA Javascript
function minCoins(coins, amount) {
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let coin of coins) {
if (i - coin >= 0) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
console.log(minCoins([1,3,4], 5));Attempts:
2 left
💡 Hint
Check how dp array updates for amount 5.
✗ Incorrect
Minimum coins for 5 are 2 (4 + 1). The code runs fine and returns 2.
❓ Predict Output
advancedLongest Increasing Subsequence Length
What is the output of this code that finds length of longest increasing subsequence using bottom-up DP?
DSA Javascript
function lengthOfLIS(nums) {
const n = nums.length;
const dp = Array(n).fill(1);
for (let i = 1; i < n; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Math.max(...dp);
}
console.log(lengthOfLIS([10,9,2,5,3,7,101,18]));Attempts:
2 left
💡 Hint
Longest increasing subsequence is a sequence where each number is bigger than previous.
✗ Incorrect
The longest increasing subsequence is [2,3,7,101] or [2,3,7,18] with length 4. The code returns 4.
❓ Predict Output
expertNumber of Unique Paths in Grid
What is the output of this code that calculates number of unique paths from top-left to bottom-right in a 3x3 grid using bottom-up DP?
DSA Javascript
function uniquePaths(m, n) {
const dp = Array(m).fill(null).map(() => Array(n).fill(1));
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
console.log(uniquePaths(3, 3));Attempts:
2 left
💡 Hint
Number of ways to reach a cell is sum of ways from top and left cells.
✗ Incorrect
For 3x3 grid, unique paths count is 6.
