Challenge - 5 Problems
Fibonacci Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci Recursive Call
What is the output of the following C# code that prints Fibonacci numbers using recursion?
DSA C#
using System; class Program { static int Fib(int n) { if (n <= 1) return n; return Fib(n - 1) + Fib(n - 2); } static void Main() { for (int i = 0; i < 6; i++) { Console.Write(Fib(i) + " "); } } }
Attempts:
2 left
💡 Hint
Remember Fibonacci starts with 0 and 1, then sums previous two numbers.
✗ Incorrect
The Fibonacci sequence starts with 0 and 1. The function returns n if n is 0 or 1. For other n, it returns sum of Fib(n-1) and Fib(n-2). So for i from 0 to 5, the outputs are 0,1,1,2,3,5.
❓ Predict Output
intermediateValue of Fibonacci at n=7
What is the value returned by Fib(7) in the following recursive Fibonacci function?
DSA C#
int Fib(int n) { if (n <= 1) return n; return Fib(n - 1) + Fib(n - 2); }
Attempts:
2 left
💡 Hint
Calculate Fibonacci numbers step by step or recall the sequence: 0,1,1,2,3,5,8,13,21...
✗ Incorrect
The function defines the standard Fibonacci sequence where Fib(0)=0, Fib(1)=1, and Fib(n)=Fib(n-1)+Fib(n-2) for n>1. Thus: Fib(2)=1, Fib(3)=2, Fib(4)=3, Fib(5)=5, Fib(6)=8, Fib(7)=13.
🔧 Debug
advancedIdentify the error in Fibonacci recursion
What error will the following C# code produce when calling Fib(5)?
DSA C#
int Fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return Fib(n - 1) - Fib(n - 2); }
Attempts:
2 left
💡 Hint
Check the operator used in the recursive return statement.
✗ Incorrect
The function subtracts Fib(n-2) from Fib(n-1) instead of adding. This causes incorrect Fibonacci values but no runtime or compile errors.
❓ Predict Output
advancedOutput of Fibonacci with memoization
What is the output of this C# program that uses recursion with memoization to print Fibonacci numbers up to n=5?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int,int> memo = new Dictionary<int,int>(); static int Fib(int n) { if (memo.ContainsKey(n)) return memo[n]; if (n <= 1) return n; int result = Fib(n - 1) + Fib(n - 2); memo[n] = result; return result; } static void Main() { for (int i = 0; i <= 5; i++) { Console.Write(Fib(i) + " "); } } }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
Memoization does not change the Fibonacci sequence output. It only optimizes the recursion. The output remains 0 1 1 2 3 5.
🧠 Conceptual
expertTime complexity of naive recursive Fibonacci
What is the time complexity of the naive recursive Fibonacci function without memoization?
Attempts:
2 left
💡 Hint
Consider how many calls are made for each Fib(n).
✗ Incorrect
Naive recursive Fibonacci calls itself twice for each n > 1, leading to exponential growth in calls, so time complexity is O(2^n).
