Data Structures & AlgorithmsHardCodeforces Div2 C/D · Amazon SDE OA

Forty-one people stand in a circle numbered 1 to 41. Starting from person 1, every third person is removed, repeatedly, counting continues around the circle. Run the simulation and report the number of the last survivor.

Forty-one people stand in a circle numbered 1 to 41. Starting from person 1, every third person is removed, repeatedly, counting continues around the circle. Run the simulation and report the number of the last survivor.
from collections import deque
q=deque(range(1,42))
while len(q)>1:
    q.rotate(-2)
    q.popleft()
print(q[0])
What is printed?
  1. A. 31
  2. B. 30
  3. C. 11
  4. D. 28

Correct answer: A. 31

Rotate two people past the front, remove the third, and repeat. A 0-indexed answer is off by one, counting from the wrong starting position changes the survivor, and simulating with 40 people answers a different circle.

More Coding Interview Problems questions

Exams that ask this topic