ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from functools import lru_cache
import sys
sys.setrecursionlimit(1000)
@lru_cache(None)
def f(n):
    return 0 if n == 0 else 1 + f(n - 1)
try:
    print(f(1500))
except RecursionError as e:
    print(type(e).__name__)
  1. A. 1500
  2. B. 0
  3. C. MemoryError
  4. D. RecursionError

Correct answer: D. RecursionError

The cache only avoids repeated work. f(1500) first recurses 1500 levels deep before any value is cached, which exceeds the 1000 limit. Bottom-up loops avoid this completely.

More Python Programming questions

Exams that ask this topic