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?

  1. A. 9
  2. B. 6
  3. C. 20
  4. 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

Exams that ask this topic