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 m[4][4] = {{1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6, 9, 16}, {10, 13, 14, 17}}
Integer t = 9, r = 0, c = 3, steps = 0
while (r < 4 AND c >= 0)
    steps = steps + 1
    if (m[r][c] == t)
        break
    else if (m[r][c] > t)
        c = c - 1
    else
        r = r + 1
    end if
end while
Print r, c, steps
  1. A. 2 2 5
  2. B. 2 2 4
  3. C. 2 2 6
  4. D. 1 2 3

Correct answer: B. 2 2 4

The staircase search starts at the top-right: 11 > 9 → left; 7 < 9 → down; 8 < 9 → down; 9 found at row 2, column 2 on the 4th probe: 2 2 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic