ProgrammingVery hardTCS NQT · Accenture

The Josephus game: n = 7 people stand in a circle numbered 1 to 7, and every third person is removed (counting continues around the circle, with the count starting at 1 for person 1). The procedure below returns the survivor. What is printed?

The Josephus game: n = 7 people stand in a circle numbered 1 to 7, and every third person is removed (counting continues around the circle, with the count starting at 1 for person 1). The procedure below returns the survivor. What is printed?
people = [1..7]; idx = 0
while more than one person remains
    idx = (idx + 2) mod count
    remove people[idx]  (the next index now refers to the following person)
end while
Print the survivor
  1. A. 4
  2. B. 7
  3. C. 3
  4. D. 2

Correct answer: A. 4

Eliminating every third person from seven gives survivor 4 (order removed: 3, 6, 2, 7, 5, 1). Skipping one instead of two gives 7, printing the first person removed gives 3, and stepping three gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic