Data Structures & AlgorithmsHardAmazon · Microsoft OA
You must report the 100 largest values from a stream of 10⁹ integers using about 1 MB of memory. Which approach fits?
- A. Sort the whole stream, then take the last 100
- B. Keep a max-heap of all values seen so far
- C. A size-100 min-heap whose root is replaced by any larger arrival
- D. Use quickselect on the full stream
Correct answer: C. A size-100 min-heap whose root is replaced by any larger arrival
The heap's root is the smallest of the current top 100, so each new value needs one comparison and at most one O(log 100) replacement. Sorting, a full max-heap and quickselect all require storing the whole stream (4 GB).
More Coding Interview Problems questions
- Edge case: JavaScript type coercion. javascript console.log(typeof null, [] + {}, [] == ![], 0.1 + 0.2 === 0.3); What is printed?
- Edge case: += versus + on a list that has another name. python a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a) What is printed?
- Edge case: changing a dict while iterating over it. python d = {1: 'a', 2: 'b', 3: 'c'} try: for k in d: if k == 2: d[4] = 'd' print("ok"…
- Edge case: Python's round() and sorting strings with mixed case. python print(round(2.5), round(3.5), round(-0.5), sorted(['b', 'A', 'a'…
- 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)…