Intervals
Sort first, then reason about overlap.
Merge, select, and allocate are different interval tasks with different greedy states.
Mechanism and state
First define the requested operation: union merging/insertion, maximum compatible selection, conflict detection, peak resource count, or point stabbing. Closed [s,e] ranges include both endpoints; half-open [s,e) meetings release time e. Sort by start for union scans and conflict checks, by finish for unweighted selection and balloon stabbing, and combine start order with active-end state for room counting. Clone caller-owned order when mutation is not permitted.
Why the pattern works
The objective determines the proof. A union scan owns completed components and a growing active union. Earliest-finish selection uses an exchange argument that preserves room for later choices. Conflict detection extends a conflict-free start-ordered prefix. Room counting matches the active-set lower bound with an allocation upper bound. Closed balloon stabbing moves an optimum’s shot to the earliest uncovered right endpoint. None of these proofs can be replaced by a generic claim that sorting solves all interval tasks.
Work through an example
Closed [1,2] plus [2,3] merges at coordinate two; half-open [1,2) and [2,3) are compatible meetings. For [0,3),[3,6),[2,4), shortest duration keeps only the middle interval, while earliest finish keeps two. For rooms [0,30),[5,10),[15,20), release the end at ten before the third start so the peak remains two. Closed balloons [1,3],[2,4],[3,5],[4,6] form one union component but need two arrow positions.
Where the reasoning fails
Choose equality rules from endpoint semantics, and specify a policy for zero-duration rows: the meeting APIs reject them, while closed insertion and balloon APIs allow point intervals. Use comparingInt or Integer.compare rather than subtracting coordinates. Preserve input and output ownership separately. A heap iterator is not a sorted sequence; active meeting ends, allocated room histories, and arrow history are different states.
Reconstruct the reasoning
Compare all five objectives on small interval sets. Construct a shortest-duration selection failure, a closed touching-endpoint merge, a back-to-back room reuse, and a connected balloon union with no common point. State whether each output is a union, count, boolean, selected set, or hitting-point set before choosing a sort order.