Big O Gym › Problem

Big O Practice: Naive Fib

Time and space complexity practice in Python and JavaScript. Concept tested: branching recursion → exponential.

def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

Variables

  • n - value of the input integer (not array length)

Explanation

Two recursive calls per invocation with no memo grows the call tree to roughly 2ⁿ nodes - **O(2ⁿ)** time (more tightly Θ(φⁿ)). Despite the exponential call count, the recursion stack only goes `n` deep at any one time - **O(n)** space.

More Big O practice problems · Today's daily problem