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?- A. 31
- B. 30
- C. 11
- 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
- Compute the sum of the prefix-function (KMP failure) array of the string "abacabab", where pi[i] is the length of the longest proper…
- A directed weighted graph has edges 0→1 (2), 0→2 (2), 1→3 (1), 2→3 (1), 1→4 (4), 2→4 (5), 3→4 (3), 3→5 (4), 4→5 (1). Count the number of…
- On vertices 0..4 a weighted directed graph has these edges, stored in the order shown in the code: (3→4, −2), (2→4, 8), (4→1, 2), (1→3…
- A task graph has six nodes 0 to 5 and dependencies 5→0, 4→0, 4→1, 5→2, 2→3, 3→1 (an edge u→v means u must finish before v). Among all…
- Eight vertices 0..7 receive the edges (0,1), (2,3), (4,5), (1,2), (5,6) in that order in a disjoint-set structure with union by size and…
- Compute the sum over all subarrays of the minimum element, for a = [3, 1, 2, 1, 3]. Subarrays are counted by position, so equal values in…