ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE

What does the following Python 3 code print? from functools import lru_cache calls = 0 @lru_cache(None) def f(n): global calls calls += 1 return n if n < 2 else f(n - 1) + f(n - 2) print(f(14), calls)

  1. A. 233 14
  2. B. 377 15
  3. C. 15 377
  4. D. 15 14

Correct answer: B. 377 15

With memoisation each n from 0..N runs the body exactly once, so calls = N+1 while the Fibonacci value is unchanged. Output: 377 15.

More Python Programming questions

Exams that ask this topic