Skip to content
playdsa
Preferences

Make yourself comfortable.

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

Theme
Advanced settings

The Crossing Routes

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Intersect two ordered interval lists: The Crossing Routes.

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

How you win

  1. 1Recognize when Two-list interval intersection matches the clues
  2. 2Keep this true after every move: every discarded interval has been compared with every interval it could overlap
  3. 3Reach the result within O(m + n)

Rules and pressure

  • Target cost: O(m + n)
  • State rule: every discarded interval has been compared with every interval it could overlap
Lesson 1 of 3

Live algorithm trace

Two-list interval intersection

Complete execution
1 of 6

Compare the first intervals from both sorted lists.

1i, j = 0, 0
2start = max(A[i].start, B[j].start)
3end = min(A[i].end, B[j].end)
4if start <= end: emit intersection
5advance the interval with smaller end
6return intersections
i = 0j = 0output = []
Truth to preserve / Cost target
Truth to preserve

every discarded interval has been compared with every interval it could overlap

Cost target

O(m + n)

The overlap of two closed intervals starts at the later start and ends at the earlier end. After testing it, advance whichever interval ends first because it cannot overlap any later interval on the other route.

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