Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Half-open segments [1,4), [3,7), [6,10), [9,12), [2,5), [8,11) are given. Choose pairwise disjoint segments to maximise the total length. What is that maximum total length?
- A. 9
- B. 6
- C. 20
- D. 7
Correct answer: D. 7
This is a weighted interval problem where the weight of a segment is its length; sorting by end and taking a DP over compatible predecessors finds the optimum. Summing all lengths ignores the disjointness rule.
More Coding Interview Problems questions
- Closed intervals [0,3], [2,5], [4,8], [6,9], [8,12] are available. What is the minimum number of them needed so that every point of the…
- 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…
- 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…