ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0)

What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0)
Integer a[6] = {3, 4, 5, 6, 1, 8}, target = 9
Map seen = empty           // value -> index
for (each j from 0 to 5)
    if (seen contains (target - a[j]))
        Print seen[target - a[j]], j
        stop
    end if
    seen[a[j]] = j
end for
  1. A. 0 3
  2. B. 4 5
  3. C. 3 0
  4. D. 1 2

Correct answer: D. 1 2

The map remembers values already passed. j = 2 (value 5) is the first index whose partner 4 has been seen (at index 1), so 1 2 is printed. A double loop over i first would find 3 + 6 at 0 3 instead.

More Pseudocode and Code Tracing questions

Exams that ask this topic