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?
- A. 2 4 0 ['A', 'B', 'a', 'b']
- B. 3 4 -1 ['A', 'a', 'B', 'b']
- C. 2 4 0 ['a', 'A', 'b', 'B']
- 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
- Problem: Shops sit on a circular ring with profits p = [5, -3, 5, 4, -8, 6]. Choose a contiguous arc of at least one shop (the arc may…
- Problem: Nine nodes 0..8 start disconnected. Apply these unions in order (a union of two nodes already in one set does nothing)…
- Problem: How many prime numbers are there that are less than or equal to 10^6?
- Problem: Let phi(k) be Euler's totient (the count of integers g in [1, k] with gcd(g, k) = 1). What is phi(1) + phi(2) + ... + phi(10)?
- Problem: Count the integers x with 1 <= x <= 1000 whose decimal digits sum to exactly 10.
- Problem: Modulo the prime p = 998244353, which value is the multiplicative inverse of 2, i.e. the x in [0, p) with 2x ≡ 1 (mod p)?