Challenge - 5 Problems
Fibonacci DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci DP with memoization
What is the output of the following Java code that calculates the 7th Fibonacci number using memoization?
DSA Java
import java.util.Arrays; public class Fibonacci { static int[] memo = new int[8]; 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(7)); } }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21...
✗ Incorrect
The 7th Fibonacci number (starting from 0) is 13 if counting from zero index, but since fib(0)=0, fib(1)=1, fib(7) = 13 or 21 depending on indexing. Here fib(7) returns 13 if zero-based, but code uses fib(7) with base cases fib(0)=0 and fib(1)=1, so fib(7) = 13. However, memo array size is 8, so fib(7) is computed correctly as 13.
❓ Predict Output
intermediateOutput of Fibonacci DP with tabulation
What is the output of this Java code that calculates the 10th Fibonacci number using bottom-up tabulation?
DSA Java
public class FibonacciTab { public static int fib(int n) { int[] dp = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } public static void main(String[] args) { System.out.println(fib(10)); } }
Attempts:
2 left
💡 Hint
The 10th Fibonacci number starting from 0 is 55.
✗ Incorrect
The code uses bottom-up DP to fill dp array. dp[10] = 55 because Fibonacci sequence is 0,1,1,2,3,5,8,13,21,34,55...
🔧 Debug
advancedIdentify the error in Fibonacci DP code
What error will this Java code produce when run?
DSA Java
public class FibonacciError { 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(7)); } }
Attempts:
2 left
💡 Hint
Check the size of the memo array and the index accessed.
✗ Incorrect
The memo array has size 5, but fib(7) tries to access memo[7], causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy use DP for Fibonacci?
Why is dynamic programming preferred over simple recursion for calculating Fibonacci numbers?
Attempts:
2 left
💡 Hint
Think about how recursion repeats work.
✗ Incorrect
Simple recursion recalculates the same Fibonacci numbers many times. DP stores results to avoid this, making it faster.
❓ Predict Output
expertOutput of optimized Fibonacci DP with space optimization
What is the output of this Java code that calculates the 15th Fibonacci number using space-optimized DP?
DSA Java
public class FibonacciOptimized { public static int fib(int n) { if (n <= 1) return n; int prev2 = 0, prev1 = 1, current = 0; for (int i = 2; i <= n; i++) { current = prev1 + prev2; prev2 = prev1; prev1 = current; } return current; } public static void main(String[] args) { System.out.println(fib(15)); } }
Attempts:
2 left
💡 Hint
The 15th Fibonacci number starting from 0 is 610.
✗ Incorrect
The code uses two variables to store previous Fibonacci numbers, calculating fib(15) = 610.
