ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def t(n):
    if n == 0:
        return 0
    if n < 3:
        return 1
    return t(n - 1) + t(n - 2) + t(n - 3)
print(t(25), t.cache_info().currsize)
  1. A. 1389537 26
  2. B. 1389537 25
  3. C. 1389536 26
  4. D. 1389537 27

Correct answer: A. 1389537 26

The tribonacci-style recurrence reuses the cached values for n from 0 to 25, so the cache holds 26 entries. The cache turns an exponential recursion into one call per n.

More Python Programming questions

Exams that ask this topic