Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Support 'add number' and 'find a pair summing to t' where adds are 10⁵ and finds are rare, versus the opposite workload. Which statement is correct?
- A. A sorted array is optimal for both workloads
- B. A heap gives O(1) for both operations
- C. The two workloads need the same structure
- D. Frequent adds: keep a hash map of counts (O(1) add, O(n) find); frequent finds: keep a set of all pair sums (O(n) add, O(1) find)
Correct answer: D. Frequent adds: keep a hash map of counts (O(1) add, O(n) find); frequent finds: keep a set of all pair sums (O(n) add, O(1) find)
This is a design trade-off: whichever operation dominates should be O(1). With counts, find scans each value and checks t − x (handling x = t − x needing count ≥ 2). Precomputing all pair sums makes add O(n) but find O(1).
More Coding Interview Problems questions
- Problem: Map each word of a 10⁶-word document to its frequency, then print words in alphabetical order. Which choice gives O(n log n) with…
- Problem: Count the number of distinct islands by shape in a grid (shapes match only by translation). python def distinct(g): R, C =…
- Problem: Count subarrays with at most k distinct values, then use it to count subarrays with exactly k distinct values. python from…
- Problem: Find the number of 'nice' subarrays containing exactly k odd numbers using prefix counts. python def nice(a, k): cnt = {0: 1}…
- Problem: Return the shortest subarray with sum at least k when values can be negative (monotonic deque over prefix sums). python from…
- Problem: Given n meetings with values, choose non-overlapping ones (end < next start) to maximise total value – weighted interval…