🏷️ Category: Algorithms / Dynamic Programming  |  🎯 Difficulty: Easy  |  πŸ”₯ Frequency: β˜…β˜…β˜…β˜…β˜…

πŸ“‹ Problem Statement

Return the nth Fibonacci Sequence number. The sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21…

πŸ“₯ Input  β†’ n = 6    πŸ“€ Output β†’ 8
πŸ“₯ Input  β†’ n = 10   πŸ“€ Output β†’ 55
πŸ“₯ Input  β†’ n = 0    πŸ“€ Output β†’ 0

🧠 What the Interviewer is Testing

ConceptWhy It Matters
Recursion vs iterationTrade-off awareness
MemoizationOptimization knowledge
Time complexity analysisUnderstanding exponential growth

⚠️ Never give just the naive recursive solution. Always show iterative or memoized version.


πŸ’‘ Approach

❌ Naive Recursion   β†’ O(2^n)  β€” exponential, very slow
βœ… Iterative        β†’ O(n)    β€” simple and efficient
βœ… Memoized         β†’ O(n)    β€” recursive with caching
βœ… Matrix / Math    β†’ O(logn) β€” advanced

βœ… Solution 1 β€” Iterative (Best for interviews)

def fib_iterative(n: int) -> int:
    if n <= 1:
        return n
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

# ── Test Cases ─────────────────────────────────
print(fib_iterative(6))    # βœ… 8
print(fib_iterative(10))   # βœ… 55
print(fib_iterative(0))    # βœ… 0

βœ… Solution 2 β€” Memoized Recursive

from functools import lru_cache

@lru_cache(maxsize=None)
def fib_memo(n: int) -> int:
    if n <= 1:
        return n
    return fib_memo(n - 1) + fib_memo(n - 2)

print(fib_memo(10))   # βœ… 55

❌ Naive Recursion (for reference β€” avoid)

# O(2^n) β€” DO NOT use this in interviews without optimizing
def fib_naive(n: int) -> int:
    if n <= 1:
        return n
    return fib_naive(n - 1) + fib_naive(n - 2)

πŸ“Š Complexity Analysis

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Approach         β”‚ Time       β”‚ Space                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Naive Recursive  β”‚ O(2^n)     β”‚ O(n) call stack          β”‚
β”‚ Iterative βœ…     β”‚ O(n)       β”‚ O(1)                     β”‚
β”‚ Memoized βœ…      β”‚ O(n)       β”‚ O(n) cache               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎀 How to Explain in the Interview

β€œThe naive recursive solution is O(2^n) because it recalculates the same values repeatedly. The iterative approach is O(n) time and O(1) space β€” just track the previous two values. With lru_cache, we can keep the recursive style while making it O(n) through memoization.”

β€œThe naive recursive solution is O(2^n) because it recalculates the same values repeatedly. The iterative approach is O(n) time and O(1) space β€” just track the previous two values. With lru_cache, we can keep the recursive style while making it O(n) through memoization.”


πŸ” Common Follow-up Questions

Q1 β†’ Print entire Fibonacci sequence up to n?

def fib_sequence(n: int) -> list:
    if n <= 0: return [0]
    seq = [0, 1]
    while len(seq) <= n:
        seq.append(seq[-1] + seq[-2])
    return seq[:n+1]

Q2 β†’ Using a generator?

def fib_gen(n: int):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

πŸ“ Set 1 of 4 β€” Python Interview Prep Series β€” devprepguide.com

Scroll to Top