ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[6] = {100, 4, 200, 1, 3, 2}
Set s = all values of a
Integer best = 0, len, x
for (each v in a)
    if (s does not contain v - 1)
        x = v
        len = 1
        while (s contains x + 1)
            x = x + 1
            len = len + 1
        end while
        best = max(best, len)
    end if
end for
Print best
  1. A. 3
  2. B. 4
  3. C. 6
  4. D. 2

Correct answer: B. 4

Only values with no predecessor start a count, so each run is walked once: 1, 2, 3, 4 is the longest run of consecutive integers. best = 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic