Data Structures & AlgorithmsModerateTCS NQT Digital · Python basics

Problem: What does the following print?

Problem: What does the following print?
a = list(range(10))
print(a[::-3], a[2:8:4])
  1. A. [9, 6, 3] [2, 6]
  2. B. [0, 3, 6, 9] [2, 6]
  3. C. [9, 6, 3, 0] [2, 6, 8]
  4. D. [9, 6, 3, 0] [2, 6]

Correct answer: D. [9, 6, 3, 0] [2, 6]

a[::-3] starts at index 9 and steps back by 3 down to 0, while a[2:8:4] picks indices 2 and 6. Forgetting the endpoint 0 is a trap.

More Coding Interview Problems questions

Exams that ask this topic