ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
// numbers stored as digit lists, least significant digit first
List a = 2 -> 4 -> 3, b = 5 -> 6 -> 4
Integer carry = 0
List out = empty
while (a or b has digits left OR carry > 0)
    Integer s = carry + (next digit of a, or 0) + (next digit of b, or 0)
    append s mod 10 to out
    carry = s / 10
end while
Print out
  1. A. 8 0 7
  2. B. 7 10 7
  3. C. 8 0 7 1
  4. D. 7 0 8

Correct answer: D. 7 0 8

The lists hold 342 and 465 written backwards. Adding digit by digit: 2 + 5 = 7; 4 + 6 = 10 → 0 carry 1; 3 + 4 + 1 = 8. out = 7 0 8, i.e. 807 = 342 + 465 stored backwards.

More Pseudocode and Code Tracing questions

Exams that ask this topic