Code RoomCar capacity trips feasible
MediumPrep Room Coding #1230

Car capacity trips feasible

CodingAlgorithms & data structuresMid–Senior~18 min

A car drives east along a number line and can carry at most `capacity` passengers. You are given `trips` where trips[i] = [num_passengers, from, to] (the car picks up num_passengers at `from` and drops them at `to`, with from < to). Return True if it is possible to complete every trip without ever exceeding capacity. There are up to 1000 trips and locations up to 1000; use a difference array over locations and a prefix sweep, not per-trip recomputation.

Implement
car_pooling(trips: list[list[int]], capacity: int) → bool
Examples
in[[[2,1,5],[3,3,7]],4]outfalse
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 18 min
InputExpectedGot
[[[2,1,5],[3,3,7]],4]falsenot run yetsample