Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Fibonacci Using Recursion in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the sum of the two preceding numbers
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the square of the previous number

Solution

  1. Step 1: Understand Fibonacci sequence definition

    The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
  2. Step 2: Recognize recursion use

    Recursion calls the function itself to calculate previous two numbers and sums them.
  3. Final Answer:

    Each number is the sum of the two preceding numbers -> Option A
  4. Quick Check:

    Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds last two numbers, not multiply or subtract [OK]
Common Mistakes:
  • Thinking Fibonacci multiplies previous numbers
  • Confusing subtraction or squares with addition
  • Assuming Fibonacci is a product sequence
2. Which of the following is the correct TypeScript function signature for a recursive Fibonacci function?
easy
A. function fib(n: string): string
B. function fib(n: boolean): boolean
C. function fib(n: number): number
D. function fib(): void

Solution

  1. Step 1: Identify input and output types

    The Fibonacci function takes a number input and returns a number output.
  2. Step 2: Match correct function signature

    Only the signature with number input and number output matches the Fibonacci function requirements.
  3. Final Answer:

    function fib(n: number): number -> Option C
  4. Quick Check:

    Input and output types must be number [OK]
Hint: Fibonacci input/output are numbers, not strings or booleans [OK]
Common Mistakes:
  • Using wrong parameter types like string or boolean
  • Missing return type or using void
  • Confusing function signature syntax
3. What is the output of the following TypeScript code?
function fib(n: number): number {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
console.log(fib(4));
medium
A. 5
B. 7
C. 4
D. 3

Solution

  1. Step 1: Calculate fib(4) using recursion

    fib(4) = fib(3) + fib(2) - fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + 1 = (1 + 0) + 1 = 2 - fib(2) = fib(1) + fib(0) = 1 + 0 = 1 So fib(4) = 2 + 1 = 3
  2. Step 2: Confirm output

    The console.log prints 5 as the result of fib(4).
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    fib(4) = 5 [OK]
Hint: Trace fib calls step-by-step to sum previous two results [OK]
Common Mistakes:
  • Confusing fib(4) with fib(5)
  • Miscounting base cases fib(0) and fib(1)
  • Adding wrong recursive results
4. Identify the error in this recursive Fibonacci function and select the fix:
function fib(n: number): number {
  if (n == 0) return 0;
  if (n == 1) return 1;
  return fib(n - 1) - fib(n - 2);
}
medium
A. Change base case n == 0 to n <= 0
B. Change subtraction to addition in return statement
C. Remove base cases completely
D. Change function to return string instead of number

Solution

  1. Step 1: Analyze return statement

    The function uses subtraction (fib(n - 1) - fib(n - 2)) which is incorrect for Fibonacci.
  2. Step 2: Correct the operation

    Fibonacci requires addition of previous two numbers, so change '-' to '+'.
  3. Final Answer:

    Change subtraction to addition in return statement -> Option B
  4. Quick Check:

    Fibonacci sums previous two numbers [OK]
Hint: Fibonacci adds, never subtracts previous two results [OK]
Common Mistakes:
  • Using subtraction instead of addition
  • Removing base cases causing infinite recursion
  • Changing return type unnecessarily
5. Given the recursive Fibonacci function below, what is the output of fib(0) and fib(1)? Why are these base cases important?
function fib(n: number): number {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
hard
A. fib(0) = 0, fib(1) = 1; base cases are optional
B. fib(0) = 1, fib(1) = 0; base cases speed up recursion
C. fib(0) = 1, fib(1) = 1; base cases prevent wrong output
D. fib(0) = 0, fib(1) = 1; base cases stop infinite recursion

Solution

  1. Step 1: Evaluate base cases fib(0) and fib(1)

    fib(0) returns 0 and fib(1) returns 1 as per the condition n <= 1 returns n.
  2. Step 2: Understand importance of base cases

    Base cases stop the recursion from continuing infinitely by providing direct answers for smallest inputs.
  3. Final Answer:

    fib(0) = 0, fib(1) = 1; base cases stop infinite recursion -> Option D
  4. Quick Check:

    Base cases return 0 and 1 to end recursion [OK]
Hint: Base cases return n for n ≤ 1 to stop recursion [OK]
Common Mistakes:
  • Mixing up fib(0) and fib(1) values
  • Thinking base cases are optional
  • Ignoring recursion stopping condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard