Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Intervals [1,6), [2,4), [3,8), [5,7), [6,9), [7,10) are given. How many unordered pairs of these intervals overlap (share at least one common point of positive length)?
- A. 11
- B. 18
- C. 5
- D. 9
Correct answer: D. 9
Half-open intervals overlap exactly when max(start) < min(end), which holds for 9 pairs. Using ≤ also counts pairs that only touch at an endpoint, such as [1,6) and [6,9) (11), counting ordered pairs gives 18, and checking only neighbours after sorting gives 5.
More Coding Interview Problems questions
- Problem: Sum the minimum of every non-empty subarray of [3, 1, 2, 4] by brute force. python a = [3, 1, 2, 4] print(sum(min(a[i:j + 1]) for…
- Problem: Count inversions in [2, 4, 1, 3, 5] with a Fenwick tree over the values. python def inv(a): n = max(a) t = [0] (n + 1) def…
- Problem: Run Bellman-Ford style negative-cycle detection (all distances start at 0, V - 1 rounds, then one extra check) on 3 vertices with…
- Problem: A directed graph has edges 0->1, 0->2, 1->2, 1->3, 2->3. Let A be its adjacency matrix. How many walks of exactly 3 edges go from…
- Problem: Undirected graph on 5 vertices has edges (0,1), (1,2), (2,0), (2,3), (3,4). Count the bridges by deleting each edge in turn and…
- Problem: Topological order by DFS finishing times (reverse postorder) for the DAG adj = {0: [1, 2], 1: [3], 2: [3], 3: []} on 4 vertices…