Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Closed intervals [1,6], [4,10], [5,5], [8,12], [2,3] are given. For the query points 5, 9 and 1, count how many intervals contain each point, then add the three counts. What is the total?
- A. 7
- B. 6
- C. 4
- D. 3
Correct answer: B. 6
Sort the starts and ends separately and use binary search to count intervals with start at most q minus those with end below q. Counting only intervals that contain the first query misses the others.
More Coding Interview Problems questions
- Piles of bananas [3, 6, 7, 11] must be eaten within H = 8 hours. Each hour you eat up to k bananas from one pile (a pile is finished in…
- Stalls at positions [1, 2, 4, 8, 9, 13] are available. Place 3 cows in distinct stalls so that the smallest distance between any two cows…
- Consider the bitmask DP that counts subsets of {1..n} with sum equal to S. For n = 4 and S = 5, what does the DP answer, counting the…
- Problem: A runner moves up, down, left or right one cell at a time on this 5×5 grid. '.' is open, '#' is a wall. The runner starts at (row…
- Problem: An undirected road network has 5 towns numbered 0 to 4 and these roads with lengths: 0–1 (7), 0–2 (2), 2–1 (3), 1–3 (1), 2–3 (8)…
- Problem: Nine nodes 0 to 8 start with no edges. These union operations are applied in order: (0,1), (2,3), (1,2), (4,5), (6,7), (7,8)…