ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
from functools import cache
@cache
def cat(n):
    if n <= 1:
        return 1
    return sum(cat(i) * cat(n - 1 - i) for i in range(n))
print([cat(k) for k in range(6)], cat.cache_info().currsize)
  1. A. [1, 1, 2, 5, 14, 42] 6
  2. B. [1, 1, 2, 5, 14, 42] 5
  3. C. [1, 1, 2, 5, 14, 41] 6
  4. D. [1, 2, 5, 14, 42, 132] 6

Correct answer: A. [1, 1, 2, 5, 14, 42] 6

The memoised recurrence builds the Catalan numbers, and each value from 0 to 5 gets exactly one cache entry. The sequence starts 1, 1, 2, 5, 14, 42.

More Python Programming questions

Exams that ask this topic