Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

A queue holds 1 to 6 in order. The front element is moved to the back, then the front is removed, repeated until one element remains. Which element survives?

  1. A. 6
  2. B. 3
  3. C. 5
  4. D. 7

Correct answer: C. 5

This is the Josephus problem with step 2; the survivor is at 2 * (6 - 2^2) + 1 = 5 for n = 6 with the standard formula, so tracing the queue confirms it.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic