ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[8] = {10, 9, 2, 5, 3, 7, 101, 18}
Integer L[8], best = 0
for (each i from 0 to 7)
    L[i] = 1
    for (each j from 0 to i - 1)
        if (a[j] < a[i] AND L[j] + 1 > L[i])
            L[i] = L[j] + 1
        end if
    end for
    best = max(best, L[i])
end for
Print best
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 6

Correct answer: A. 4

L[i] is the longest strictly increasing subsequence ending at a[i]. One longest chain is 2, 5, 7, 101 (or 2, 3, 7, 18), so best = 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic