ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
from functools import lru_cache
calls = 0
@lru_cache(maxsize=None)
def f(n):
    global calls
    calls += 1
    if n < 2:
        return n
    return f(n - 1) + f(n - 2)
print(f(30), calls)
  1. A. 832040 59
  2. B. 1346269 31
  3. C. 832040 60
  4. D. 832040 31

Correct answer: D. 832040 31

With the cache each f(n) for n = 0..30 runs its body once, so calls is 31; f(30) is 832040. Without the cache the same call would take about a million calls.

More Python Programming questions

Exams that ask this topic