ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from functools import cache
@cache
def paths(r, c):
    return 1 if r == 0 or c == 0 else paths(r - 1, c) + paths(r, c - 1)
print(paths(16, 16))
  1. A. 601080390
  2. B. 300540195
  3. C. 155117520
  4. D. 1073741824

Correct answer: A. 601080390

The number of lattice paths to a 16 by 16 grid is C(32, 16) = 601080390. The cache turns the exponential recursion into a 17 by 17 table of calls, and the depth stays about 32.

More Python Programming questions

Exams that ask this topic