ProgrammingHardAmazon 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 a[8] = {170, 45, 75, 90, 802, 24, 2, 66}
List bucket[10] = all empty
for (each i from 0 to 7)
    append a[i] to bucket[a[i] mod 10]
end for
Integer k = 0
for (each d from 0 to 9)
    for (each x in bucket[d], in order)
        a[k] = x
        k = k + 1
    end for
end for
Print a[2], a[3], a[5]
  1. A. 2 802 75
  2. B. 802 2 45
  3. C. 90 802 24
  4. D. 802 2 24

Correct answer: B. 802 2 45

One pass of LSD radix sort groups by the units digit, keeping the original order inside each group: 170, 90 | 802, 2 | 24 | 45, 75 | 66. So a[2], a[3], a[5] = 802 2 45.

More Pseudocode and Code Tracing questions

Exams that ask this topic