ProgrammingHardGoldman Sachs OA · Infosys SP

What will be the output of the following pseudocode? (Arrays are indexed from 0)

What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer nxt[6] = {3, 5, 0, 1, 2, 4}
Integer x = 0, steps = 0
do
    x = nxt[x]
    steps = steps + 1
while (x != 0)
Print steps
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: D. 6

Follow the links from 0: 0 → 3 → 1 → 5 → 4 → 2 → 0. The walk returns to 0 after 6 steps, so all six indices lie on one cycle.

More Pseudocode and Code Tracing questions

Exams that ask this topic