ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def solve(n, memo={}):
    if n in memo:
        return memo[n]
    if n < 2:
        return n
    memo[n] = solve(n - 1) + solve(n - 2)
    return memo[n]
print(solve(30), len(solve.__defaults__[0]))
  1. A. 832040 30
  2. B. 514229 28
  3. C. 832040 31
  4. D. 832040 29

Correct answer: D. 832040 29

solve(30) is the 30th Fibonacci number, 832040. The default memo dict stores keys 2 through 30, which is 29 entries, because n < 2 returns before any storage.

More Python Programming questions

Exams that ask this topic