Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

Bridge the Overlaps

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Merge connected spans: Bridge the Overlaps.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Interval merging matches the clues
  2. 2Keep this true after every move: merged intervals are sorted and nonoverlapping
  3. 3Reach the result within O(n log n)

Rules and pressure

  • Target cost: O(n log n)
  • State rule: merged intervals are sorted and nonoverlapping
Lesson 1 of 3

Live algorithm trace

Interval merging

Complete execution
1 of 5

Sort intervals by start. This input is already in start order.

1intervals.sort()
2compare next start with last end
3merge or append
4return merged
merged = []
Truth to preserve / Cost target
Truth to preserve

merged intervals are sorted and nonoverlapping

Cost target

O(n log n)

After sorting intervals by start time, only the most recently merged interval can overlap the next one. Extend it or begin a new span.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead