Data Structures & AlgorithmsModerateTCS NQT Digital · Python semantics

Problem: What does the following print?

Problem: What does the following print?
print(sorted([3, 1, 2], key=lambda x: -x)[0])
What is printed?
  1. A. 3
  2. B. 1
  3. C. 2
  4. D. -5

Correct answer: A. 3

Sorting by the negated key puts the largest value first, so element 0 is 3.

More Coding Interview Problems questions

Exams that ask this topic