ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
def tile(n):
    dp = [0] * (n + 1)
    dp[0] = 1
    for i in range(1, n + 1):
        dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)
    return dp[n]
print([tile(k) for k in range(6)])
  1. A. [0, 1, 1, 2, 3, 5]
  2. B. [1, 1, 2, 3, 5, 8]
  3. C. [1, 1, 2, 3, 5, 9]
  4. D. [1, 2, 3, 5, 8, 13]

Correct answer: B. [1, 1, 2, 3, 5, 8]

Each length is the sum of the two previous lengths, which is the Fibonacci recurrence. The base case dp[0] = 1 gives 1, 1, 2, 3, 5, 8 for lengths 0 to 5.

More Python Programming questions

Exams that ask this topic