Data Structures & AlgorithmsHardAmazon · Microsoft OA

Edge case: Python's round() and sorting strings with mixed case.

Edge case: Python's round() and sorting strings with mixed case.
print(round(2.5), round(3.5), round(-0.5), sorted(['b', 'A', 'a', 'B']))
What is printed?
  1. A. 2 4 0 ['A', 'B', 'a', 'b']
  2. B. 3 4 -1 ['A', 'a', 'B', 'b']
  3. C. 2 4 0 ['a', 'A', 'b', 'B']
  4. D. 3 4 0 ['A', 'B', 'a', 'b']

Correct answer: A. 2 4 0 ['A', 'B', 'a', 'b']

round uses banker's rounding (ties go to the even neighbour): 2, 4 and −0 printed as 0. Strings sort by code point, and every uppercase letter (A = 65) precedes every lowercase one (a = 97).

More Coding Interview Problems questions

Exams that ask this topic