Data Structures & AlgorithmsModerateTCS NQT · Python basics

Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order.

Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order.
a = [5, 1, 9, 3]
print(sum(sorted(a, reverse=True)[::2]))
What is printed?
  1. A. 14
  2. B. 12
  3. C. 10
  4. D. 8

Correct answer: B. 12

Sorted descending gives [9, 5, 3, 1], and the even positions hold 9 and 3, totalling 12. Summing the odd positions (5 + 1 = 6) is the trap.

More Coding Interview Problems questions

Exams that ask this topic