Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

Question

Given passenger pickup requests, each with an interval [si,ei][s_i,e_i], determine whether one car can pick up all passengers without conflicts.

An interval may represent exclusive use of the car or a window in which a pickup instant can be chosen. These are different scheduling problems. Analyze both explicit models:

  1. Fixed commitments: the car is occupied for each entire supplied interval. Decide whether any two commitments overlap. State the difference between half-open intervals [si,ei)[s_i,e_i) and closed intervals [si,ei][s_i,e_i].
  2. Flexible pickup windows: choose one pickup instant in each closed window [si,ei][s_i,e_i]. Assume zero service time, zero travel time, no location or capacity constraints, and allow several pickups at the same instant. Decide whether a schedule exists.

Explain what additional information is needed if pickups require positive service or transit time. Treat the empty request set as feasible.

Hint 1

First decide what an interval means: time the car is busy, or a window in which one pickup instant is chosen. The two models have different answers.

Hint 2

For busy intervals, sorting by start time means each interval only needs to be compared with the one before it. For pickup windows, try choosing a specific instant in each window.

How to Answer in the Interview

A phone-screen question that looks like a standard interval problem. The code takes two minutes. The points go to the model, the endpoint rule, and why comparing neighbors is enough.

Clarify first:

  • "Is the car busy for the whole interval, or is the interval a window in which the pickup happens?" The two readings have different answers.
  • "Do touching endpoints conflict?" Closed [1,3][1,3] and [3,5][3,5] share the instant 33. Half-open [1,3)[1,3) and [3,5)[3,5) do not.
  • "Does a pickup take time, or does the car drive between passengers?" Then the intervals alone cannot decide feasibility.

TIP

How I'd say it in the interview: "First, what does an interval mean? If the car is busy for the whole interval, I sort by start and check each one against the previous end, with < or <= for half-open or closed endpoints, O(n log n). If it's a pickup window and pickups take no time, I pick everyone up at their window start, so any nonempty windows work. If pickups or driving take time, I need those numbers first."

Then walk through:

  1. Show that the same two requests conflict in one model and not in the other.
  2. Fixed commitments: sort by start, compare with the previous end, state the endpoint rule.
  3. Prove that neighbor checks are enough. Most candidates lose points here by asserting it, or by never saying which endpoint rule they used.
  4. Flexible windows: build the schedule at the window starts and name the only way it fails.
  5. List the data needed once pickups or travel take time.

Solution

For fixed commitments, one car works exactly when no two intervals overlap: sort by start and compare each interval with the previous end, in O(n log n) time. For flexible windows with zero service and travel time and shared instants allowed, one car works exactly when every window is nonempty, si≤eis_i\le e_i. The empty request set is feasible in both.

IMPORTANT

An availability window is not an occupied interval. The flexible-window result depends on zero travel and service time and on allowing simultaneous pickups.

Same requests, opposite answers

Take requests A =[1,4]=[1,4] and B =[3,5]=[3,5].

Handwritten timeline with requests A=(1,4) and B=(3,5) as closed intervals. They conflict as fixed commitments because both use the car during (3,4), but are feasible as flexible windows by choosing pickup times 1 and 3 when travel and service times are zero.

As fixed commitments, both need the car during (3,4)(3,4), so they conflict. As pickup windows, picking up A at 11 and B at 33 works, because a pickup occupies no time.

Model 1: fixed commitments

Sort the intervals by start time, s1≤s2≤⋯≤sns_1\le s_2\le\cdots\le s_n, and scan them in that order, keeping the end of the previous interval. For half-open intervals, interval kk is compatible when

sk≥ek−1.s_k\ge e_{k-1}.

Equality is allowed because [s,e)[s,e) has ended at the instant ee. For closed intervals the test is strict, sk>ek−1s_k>e_{k-1}, because sharing one endpoint is an overlap. A half-open [s,s)[s,s) is empty and is dropped before the scan. A closed [s,s][s,s] occupies one instant and stays.

Why neighbors are enough: suppose intervals 11 to k−1k-1 passed the scan. In the half-open case each passing check gives ej−1≤sje_{j-1}\le s_j, and each nonempty interval has sj<ejs_j<e_j, so the ends form a chain:

e1≤s2<e2≤s3<e3≤⋯≤sk−1<ek−1.e_1\le s_2<e_2\le s_3<e_3\le\cdots\le s_{k-1}<e_{k-1}.

So every earlier end is at most ek−1e_{k-1}. If sk≥ek−1s_k\ge e_{k-1}, then sk≥ejs_k\ge e_j for every j<kj<k, and interval kk misses all earlier intervals. In the closed case the chain has << and ≤\le swapped, and the strict test sk>ek−1s_k>e_{k-1} clears every earlier end in the same way.

If the check fails, the conflict is real. Sorting gives sk−1≤sks_{k-1}\le s_k, and failure gives sk<ek−1s_k<e_{k-1}, so the instant sks_k lies in both interval k−1k-1 and interval kk. With the closed rule, failure means sk≤ek−1s_k\le e_{k-1}, and sks_k again lies in both.

Number check: [1,3)[1,3) then [3,5)[3,5) passes because 3≥33\ge3. The closed pair [1,3][1,3], [3,5][3,5] fails because 3>33>3 is false. Sorting costs O(n log n), the scan O(n), and the sorted copy O(n) memory.

Model 2: flexible instantaneous pickups

Build a schedule directly: pick up passenger ii at ti=sit_i=s_i. This is allowed because si∈[si,ei]s_i\in[s_i,e_i] whenever si≤eis_i\le e_i, pickups and travel take no time, and two pickups may share an instant. So every collection of nonempty windows is feasible. For two windows [2,2][2,2], both pickups happen at 22.

The only failure is an empty window: if si>eis_i>e_i, no instant lies in [si,ei][s_i,e_i]. The test is one pass, O(n), with no sorting.

What positive service or travel time needs

Once a pickup takes time or the car must move, you also need:

  • The service duration, and whether service must finish inside the window.
  • Passenger locations and travel times between them, or a minimum gap between pickups.
  • Vehicle capacity, if passengers stay in the car.
  • Whether the pickup order is fixed or can be chosen.

With a fixed order and a required gap did_i before pickup ii, take each pickup as early as possible:

t1=s1,ti=max⁡(si,  ti−1+di).t_1=s_1,\qquad t_i=\max\left(s_i,\;t_{i-1}+d_i\right).

The order is feasible exactly when every ti≤eit_i\le e_i. Picking up earlier never hurts, because a later ti−1t_{i-1} can only push tit_i later. This checks one supplied order. When the order is free, sorting by start time is not proven to find a feasible order, so the order becomes part of the search.

Code

The two functions implement the two models. Each one is the answer for its model.

def fixed_commitments_feasible(intervals, closed=False):
    intervals = list(intervals)
    if any(start > end for start, end in intervals):
        raise ValueError("interval starts after its end")
    if not closed:
        intervals = [(s, e) for s, e in intervals if s < e]  # drop empty [s, s)

    previous_end = None
    for start, end in sorted(intervals):
        if previous_end is not None:
            conflict = start <= previous_end if closed else start < previous_end
            if conflict:
                return False
        previous_end = end
    return True

def instantaneous_windows_feasible(windows):
    # Zero travel and service time, simultaneous pickups allowed.
    return all(start <= end for start, end in windows)

requests = [(1, 4), (3, 5)]
touching = [(1, 3), (3, 5)]
print("A, B as commitments:", fixed_commitments_feasible(requests))
print("A, B as windows:", instantaneous_windows_feasible(requests))
print("Touching, half-open:", fixed_commitments_feasible(touching))
print("Touching, closed:", fixed_commitments_feasible(touching, closed=True))
print("Unsorted disjoint:", fixed_commitments_feasible([(5, 6), (1, 2)]))
print("Two windows at 2:", instantaneous_windows_feasible([(2, 2), (2, 2)]))
print("Empty window:", instantaneous_windows_feasible([(5, 4)]))
print("No requests:", fixed_commitments_feasible([]),
      instantaneous_windows_feasible([]))

Output:

A, B as commitments: False
A, B as windows: True
Touching, half-open: True
Touching, closed: False
Unsorted disjoint: True
Two windows at 2: True
Empty window: False
No requests: True True

Additional Notes

Common mistakes

  • Running the overlap check on pickup windows. It borrows the rule for busy intervals before deciding what a window means, and rejects [1,4][1,4], [3,5][3,5] although pickups at 11 and 33 work.
  • Using the wrong endpoint rule. Switching < and <= changes the model, not the code style.
  • Comparing neighbors without sorting. In the input order [5,6)[5,6), [1,2)[1,2), the scan sees 1<61<6 and rejects two disjoint intervals.
  • Reaching for the earliest-finish greedy. It maximizes how many requests one car can keep, but here every request must be served.
  • Dropping the simultaneous-pickup assumption without saying so. Two windows [2,2][2,2] become infeasible if pickups need different instants.

What this question tests

Whether you settle the model before picking an algorithm. Sorting and comparing neighbors is standard. The signal is the model question, the endpoint convention, and a proof of the neighbor check. Likely follow-ups:

  • Solve it another way. Turn each commitment into a +1 event at its start and a -1 event at its end, sort the events, and reject if the running count exceeds 11. For half-open intervals, process ends before starts at the same time. For closed ones, starts first. It is also O(n log n), and the tie rule is where it usually breaks.
  • The minimum number of cars. The same event sweep returns the largest running count, which is the answer.
  • Requests arrive one at a time. Keep commitments in a balanced search tree and check only the predecessor and successor of each new interval, O(log n) per request.

Patterns to reuse

  • Sort so that a local check between neighbors implies a global property.
  • Prove feasibility by building a witness, such as a concrete schedule, instead of searching.
  • Write the endpoint convention into the code. Half-open intervals let adjacent pieces touch without overlapping.
Your progressLogin to save progress
Spot an issue or have a suggestion?