Practice: Car Pooling

Problem: https://leetcode.com/problems/car-pooling/

Recognition reminder: each trip is a demand that lives over a range, [num_passengers, start, end], and a fixed capacity has to hold every demand that is active at the same time. This is the interval pattern’s third common shape: not “merge the ranges” and not “place one new range”, but “how many of these ranges are active at once, and does that peak ever break a limit”. The cue is overlapping demands under a fixed capacity, asked as a yes-or-no feasibility question. Reading it as an interval-over-time question, rather than as a loop over trips, is the move this problem drills.

Before you start (the five-beat rhythm)

  1. Your Pattern Cards for this week are already written (tries and intervals).
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

The hard part is not the arithmetic; it is deciding what single quantity you must track to know whether the car is ever over capacity, and where along the route that quantity can change. Then ask what your running time becomes if you settle the question in one pass rather than comparing every trip against every other.

This is the week’s heap-of-end-times drill, so make that your required rep: solve it once with a min-heap of trip end-locations (as you reach each boarding, pop the trips that have already ended to free their seats). Once that passes, you may also write the difference-array sweep and compare the two; the heap version is the one this week exists to build.

The function you implement

Write a function car_pooling with this signature:

car_pooling(trips: list[list[int]], capacity: int) -> bool

Each trip is [num_passengers, start, end]: that many passengers board at location start and get off at location end. The car drives in one direction along increasing locations and seats at most capacity passengers at any moment. A passenger who gets off at a location frees the seat for someone boarding at that same location. Return True if you can carry every trip without ever exceeding capacity, and False otherwise.

For example, car_pooling([[2,1,5],[3,3,7]], 4) returns False (between locations 3 and 5 the car would need to hold 2 + 3 = 5 passengers, over the limit of 4), and car_pooling([[2,1,5],[3,3,7]], 5) returns True (the same peak of 5 now fits exactly).

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?