intermediate
Interval task patterns
Sort, merge, sweep, and reason about inclusive or exclusive endpoints before coding.
Interval tasks ask you to reason about ranges on a line: merge overlaps, insert a range, count rooms, find free time, or process start and end events. Sorting by start time often unlocks a one-pass solution; heaps or sweep lines handle active intervals.
Trade-off: compare time, memory, and implementation complexity before committing to a structure or pattern.
On interviews: The key is defining endpoint semantics. Inclusive and exclusive ends change overlap checks, especially for calendars and meeting rooms.
Common pitfalls: Do not merge adjacent intervals unless the requirements say they overlap. Tie-breaking start and end events incorrectly can change active counts.
Checklist:
- Define endpoint semantics.
- Sort by start or event time.
- Maintain active intervals.
- Test touching boundaries.