ProgrammingHardAmazon OA · Infosys SP

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 a[9] = {3, 2, 3, 1, 2, 4, 5, 5, 6}, k = 4
sort a in decreasing order
Print a[k - 1]
  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 2

Correct answer: C. 4

Sorted decreasingly: 6, 5, 5, 4, 3, 3, 2, 2, 1. The 4th element is 4 — duplicates count separately here. The 4th largest DISTINCT value would be 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic