ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def walk(n):
    if n == 0:
        return []
    return [n] + walk(n - 1)
print(walk(3), len(walk(500)))
  1. A. [3, 2, 1] 501
  2. B. [3, 2, 1] 1000
  3. C. [3, 2, 1] 500
  4. D. [1, 2, 3] 500

Correct answer: C. [3, 2, 1] 500

walk builds the list from the largest value down. The recursion depth is 501, which is under the default limit of 1000, so the call succeeds.

More Python Programming questions

Exams that ask this topic