ProgrammingVery hardAmazon OA · Goldman Sachs OA

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[8] = {1, 2, 3, 4, 5, 6, 7, 3}
Integer slow = 0, fast = 0, steps = 0
do
    slow = nxt[slow]
    fast = nxt[nxt[fast]]
    steps = steps + 1
while (slow != fast)
Print steps, slow
  1. A. 3 3
  2. B. 5 5
  3. C. 4 6
  4. D. 6 3

Correct answer: B. 5 5

The path is 0 → 1 → 2 → 3 and then the cycle 3 → 4 → 5 → 6 → 7 → 3 of length 5. Tracking slow/fast: (1, 2), (2, 4), (3, 6), (4, 3), (5, 5). They meet after 5 steps at node 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic