Three-phase interval scan

Add a Closed Coverage Span

Original & variantInsert Interval ↗

Original examples and wording. The question below states the exact input format and output contract.

01. The question

Given {"intervals":[[start,end],...],"new":[start,end]}, insert a closed coverage span and coalesce all overlap. Existing spans must be sorted and already separated: each next start must exceed the previous end. Touching endpoints count as overlap. Every span has start < end.

Before revealing the idea, predict the result and describe what information you would need to track.

02. Worked example

Input{"intervals":[[1,3],[6,8],[11,14]],"new":[3,11]}

Output[[1,14]]

  1. The new start 3 touches the first span’s end, so merge them.
  2. Extend through [6,8] and then the touching [11,14].
  3. The single closed union is [1,14].

Try your code

Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.

Edit your test cases

Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.

Input contract & reference code

Example data: {"intervals":[[1,3],[6,8],[11,14]],"new":[3,11]}. Parameter: "". Numeric parameters arrive as strings; convert them with int(parameter) when needed.

The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.

Python runtime notices

Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.

Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.

03. The idea

Try sketching your own approach first. What would you track, and why would it work?

Reveal the idea
  1. Copy spans strictly before the new span.
  2. Expand its endpoints through every overlapping or touching span.
  3. Append the combined span, then copy all later spans.

04. Watch it happen

Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.

Input & settings

Bounded original examples: at most 16 list nodes, 12 intervals, 16 labels or 40 ASCII characters. Pointer identities and interval endpoint rules are stated in the task.

Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.

Working state / what changes

Ready to begin

Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.

Python / why it changes

The line behind the step

def solve(data, parameter=''):
    start, end = data["new"]
    result = []
    index = 0
    intervals = data["intervals"]
    while index < len(intervals) and intervals[index][1] < start:
        result.append(intervals[index][:])
        index += 1
    while index < len(intervals) and intervals[index][0] <= end:
        start = min(start, intervals[index][0])
        end = max(end, intervals[index][1])
        index += 1
    result.append([start, end])
    while index < len(intervals):
        result.append(intervals[index][:])
        index += 1
    return result
How to use this walkthrough

Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.

05. Python solution

def solve(data, parameter=''):
    start, end = data["new"]
    result = []
    index = 0
    intervals = data["intervals"]
    while index < len(intervals) and intervals[index][1] < start:
        result.append(intervals[index][:])
        index += 1
    while index < len(intervals) and intervals[index][0] <= end:
        start = min(start, intervals[index][0])
        end = max(end, intervals[index][1])
        index += 1
    result.append([start, end])
    while index < len(intervals):
        result.append(intervals[index][:])
        index += 1
    return result

06. Cost & edge cases

Time
O(n)
Space
O(n) returned spans, O(1) scan workspace
  • An empty existing list returns just the new span.
  • Existing touching spans are invalid because they are not already coalesced.
  • The inserted span may contain or touch several old spans.

Keep going