Question
Given passenger pickup requests, each with an interval , 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:
- Fixed commitments: the car is occupied for each entire supplied interval. Decide whether any two commitments overlap. State the difference between half-open intervals
and closed intervals. - Flexible pickup windows: choose one pickup instant in each closed window
. 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
andshare the instant. Half-openanddo 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:
- Show that the same two requests conflict in one model and not in the other.
- Fixed commitments: sort by start, compare with the previous end, state the endpoint rule.
- Prove that neighbor checks are enough. Most candidates lose points here by asserting it, or by never saying which endpoint rule they used.
- Flexible windows: build the schedule at the window starts and name the only way it fails.
- 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, . 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 and B .

As fixed commitments, both need the car during , so they conflict. As pickup windows, picking up A at and B at works, because a pickup occupies no time.
Model 1: fixed commitments
Sort the intervals by start time, , and scan them in that order, keeping the end of the previous interval. For half-open intervals, interval is compatible when
Equality is allowed because has ended at the instant . For closed intervals the test is strict, , because sharing one endpoint is an overlap. A half-open is empty and is dropped before the scan. A closed occupies one instant and stays.
Why neighbors are enough: suppose intervals to passed the scan. In the half-open case each passing check gives , and each nonempty interval has , so the ends form a chain:
So every earlier end is at most . If , then for every , and interval misses all earlier intervals. In the closed case the chain has and swapped, and the strict test clears every earlier end in the same way.
If the check fails, the conflict is real. Sorting gives , and failure gives , so the instant lies in both interval and interval . With the closed rule, failure means , and again lies in both.
Number check: then passes because . The closed pair , fails because 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 at . This is allowed because whenever , pickups and travel take no time, and two pickups may share an instant. So every collection of nonempty windows is feasible. For two windows , both pickups happen at .
The only failure is an empty window: if , no instant lies in . 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 before pickup , take each pickup as early as possible:
The order is feasible exactly when every . Picking up earlier never hurts, because a later can only push 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
,although pickups atandwork. - Using the wrong endpoint rule. Switching
<and<=changes the model, not the code style. - Comparing neighbors without sorting. In the input order
,, the scan seesand 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
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
+1event at its start and a-1event at its end, sort the events, and reject if the running count exceeds. 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.