← Two Pointers Quiz: all 8 questions
Two Pointers Quiz · Question 5 of 8
What does nums look like after this code runs on [0, 1, 0, 3, 12]? (code)
What does nums look like after this code runs on [0, 1, 0, 3, 12]?
write = 0
for read in range(len(nums)):
if nums[read] != 0:
nums[write], nums[read] = nums[read], nums[write]
write += 1
- A.[0, 0, 1, 3, 12]
- B.[1, 3, 12, 0, 0]
- C.[12, 3, 1, 0, 0]
- D.[1, 0, 3, 0, 12]
Answer & explanation
Correct answer: [1, 3, 12, 0, 0]
This is Move Zeroes: non-zeros are written in order to the front, zeros end up at the back.
Can you get the rest right without peeking?
Take the timed Two Pointers Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →