0
0
DSA Typescriptprogramming~20 mins

Fibonacci Using Recursion in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fibonacci Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A5
B3
C13
D8
Attempts:
2 left
💡 Hint
Remember the Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
Predict Output
intermediate
2: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);
A2
B5
C3
D1
Attempts:
2 left
💡 Hint
Calculate fibonacci(3) step by step.
🔧 Debug
advanced
2: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));
AOutput is 3
BOutput is 5
CTypeError: fibonacci is not a function
DStack overflow error due to infinite recursion
Attempts:
2 left
💡 Hint
Check the recursive calls carefully for base cases.
🧠 Conceptual
advanced
2: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);
}
AO(n^2)
BO(2^n)
CO(n)
DO(log n)
Attempts:
2 left
💡 Hint
Consider how many calls are made for each fibonacci call.
🚀 Application
expert
2: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));
A21
B34
C8
D13
Attempts:
2 left
💡 Hint
Memoization avoids repeated calculations, but the Fibonacci sequence values remain the same.