ProgrammingHardTCS NQT · Accenture

Array a = [4, 9, 2, 8, 2, 7]. The procedure below counts the prefix records, that is, the positions i where a[i] is strictly greater than every element before it (position 0 always counts). What is printed?

Array a = [4, 9, 2, 8, 2, 7]. The procedure below counts the prefix records, that is, the positions i where a[i] is strictly greater than every element before it (position 0 always counts). What is printed?
count = 1; mx = a[0]
for i = 1 to n - 1
    if a[i] > mx then count = count + 1; mx = a[i]
end for
Print count
  1. A. 2
  2. B. 6
  3. C. 1
  4. D. 5

Correct answer: A. 2

The records are 4 and 9, so the count is 2. Counting the array length gives 6, starting the count at 0 gives 1, and counting distinct values gives 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic