Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Heap / Priority Queue

Car Pooling

MEDIUM✓ CHIP-TIMEDLC #1094 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: A car makes a single loop of pickups and drop-offs — each trip lists its passenger count plus the mile it boards and the mile it gets off — decide whether the car's fixed capacity is ever exceeded along the route.

A car drives a single straight route and picks up or drops off groups of passengers at specific trips, each trip listing a passenger count, a boarding mile, and a drop-off mile.

Passengers from a trip are aboard from their boarding mile up to, but not including, their drop-off mile — the moment a group's drop-off mile is reached, those seats are free again.

The car has a fixed seat capacity, and the task is a single yes-or-no verdict: whether the passenger count ever exceeds that capacity anywhere along the route.

  • trip counts run up to a few thousand
  • passenger counts per trip and total capacity are positive
  • a drop-off mile frees seats before same-mile pickups fill them
  • the answer is one boolean covering the whole route
EX 01
trips = [[2, 1, 5], [3, 3, 7]] · capacity = 4
→ false
overlapping trips exceed capacity by one
EX 02
trips = [[2, 1, 5], [3, 3, 7]] · capacity = 5
→ true
same trips, just enough capacity
EX 03
trips = [[2, 1, 5], [3, 5, 7]] · capacity = 3
→ true
second trip starts exactly as the first ends

The hintstake only what you need

HINT 1 THE NUDGE

Only two moments matter for each trip: passengers get added at the pickup mile and removed at the drop-off mile — the miles in between never need to be touched individually.

HINT 2 THE STRUCTURE

Sorting trips by pickup mile and sweeping forward turns this into a single pass: at each pickup, first free up any capacity from trips that have already ended.

HINT 3 ONE STEP FROM THE ANSWER

A min-heap of active trips keyed by drop-off mile tells you instantly which passengers to remove before adding new ones — pop everyone whose drop-off is at or before the current pickup, then check if the running total still fits.

Coach's boardthe pattern, step by step

THE MILE-BY-MILE SWEEPPATTERN · MIN-HEAP OF ACTIVE TRIPStrips [2 riders 1→5] [3 riders 3→7] [4 riders 4→6] · capacity 8
2: 1→5
3: 3→7
4: 4→6
ACTIVE (END, RIDERS) · RUNNING TOTAL
total → 0
STEP 1

Three trips, capacity 8. Sorted by pickup mile: trip 0 boards 2 at mile 1, trip 1 boards 3 at mile 3, trip 2 boards 4 at mile 4.

STEP 1 / 6 · ← → WORK TOO

The splitstwo paces, two languages

grind/car-pooling.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
        ordered = sorted(trips, key=lambda t: t[1])
        heap = []  # (endMile, passengers) for trips currently aboard
        current = 0
        for num, start, end in ordered:
            while heap and heap[0][0] <= start:
                _, dropped = heapq.heappop(heap)
                current -= dropped
            current += num
            if current > capacity:
                return False
            heapq.heappush(heap, (end, num))
        return True
TIME O(N LOG N)SPACE O(N)PYTHON · RACE PACE · 14 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED