Challenge - 5 Problems
Fibonacci Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Fibonacci Recursive Call
What is the output of the following TypeScript code that calculates Fibonacci numbers recursively?
DSA Typescript
function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(5));Attempts:
2 left
💡 Hint
Remember the Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
The fibonacci function returns the nth Fibonacci number. fibonacci(5) returns 5 because the sequence is 0,1,1,2,3,5,8 and fib(5) = 5.
❓ Predict Output
intermediate2:00remaining
Value of Variable After Recursive Fibonacci
What is the value of variable `result` after running this code?
DSA Typescript
let result = 0; function fibonacci(n: number): number { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } result = fibonacci(3);
Attempts:
2 left
💡 Hint
Calculate fibonacci(3) step by step.
✗ Incorrect
fibonacci(3) = fibonacci(2) + fibonacci(1) = (fibonacci(1) + fibonacci(0)) + 1 = (1 + 0) + 1 = 1 + 1 = 2.
🔧 Debug
advanced2:00remaining
Identify the Error in Fibonacci Recursive Function
What error will this TypeScript code produce when run?
DSA Typescript
function fibonacci(n: number): number {
if (n === 0) return 0;
if (n === 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 3);
}
console.log(fibonacci(4));Attempts:
2 left
💡 Hint
Check the recursive calls carefully for base cases.
✗ Incorrect
The function calls fibonacci(n - 3) without proper base case coverage for n < 3, causing infinite recursion and stack overflow.
🧠 Conceptual
advanced2:00remaining
Time Complexity of Recursive Fibonacci
What is the time complexity of the naive recursive Fibonacci function?
DSA Typescript
function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}Attempts:
2 left
💡 Hint
Consider how many calls are made for each fibonacci call.
✗ Incorrect
Each call to fibonacci(n) makes two calls to fibonacci(n-1) and fibonacci(n-2), leading to exponential growth in calls, so time complexity is O(2^n).
🚀 Application
expert2:00remaining
Output of Memoized Fibonacci Recursive Function
What is the output of this memoized Fibonacci function when called with fibonacci(6)?
DSA Typescript
function fibonacci(n: number, memo: {[key: number]: number} = {}): number {
if (n <= 1) return n;
if (memo[n] !== undefined) return memo[n];
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
}
console.log(fibonacci(6));Attempts:
2 left
💡 Hint
Memoization avoids repeated calculations, but the Fibonacci sequence values remain the same.
✗ Incorrect
fibonacci(6) returns 8 because the sequence is 0,1,1,2,3,5,8,13 and the 6th index (0-based) is 8.