ProgrammingVery hardWipro NLTH · Cognizant GenC

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
List L = [1, 2, 3, 4, 5, 6, 7]
Integer idx = 0, k = 3
while (size(L) > 1)
    idx = (idx + k - 1) mod size(L)
    remove the element at position idx from L
end while
Print L[0]
  1. A. 1
  2. B. 5
  3. C. 7
  4. D. 4

Correct answer: D. 4

Removals in order: 3, 6, 2, 7, 5, 1, leaving 4. (After each removal idx already points at the next element.)

More Pseudocode and Code Tracing questions

Exams that ask this topic