Car Pooling
Problem Statement
A car drives in one direction along a road, starting at location 0. It has capacity empty seats and never turns back.
Each trip trips[i] = [numPassengers, from, to] means numPassengers people get in when the car reaches location from and get out when it reaches location to.
Return true if the car can do every trip without ever carrying more than capacity passengers at once, and false otherwise.
People getting out at a location leave before new people get in there. So passengers dropped at 5 never sit next to passengers picked up at 5.
A list trips of [numPassengers, from, to] triples, and an integer capacity.
true if all trips fit, false if the car is ever over capacity.
Between locations 3 and 5, both groups are in the car: 2 + 3 = 5 people, but only 4 seats.
The busiest stretch has 5 people, which fits exactly.
The first group gets out at 5 before the second group gets in at 5, so there are never more than 3 people in the car.
- • 1 ≤ trips.length ≤ 1000
- • 1 ≤ numPassengers ≤ 100
- • 0 ≤ from < to ≤ 1000
- • 1 ≤ capacity ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each location, sum the passengers of all trips covering it
Turn each trip into +p and −p events, sort by location, sweep a running load
+p at from, −p at to in an array of size 1001; prefix-sum it and check capacity
For every location x from 0 to 999, look at the stretch [x, x+1) of road. Add up p for every trip with from ≤ x < to. If any sum is more than capacity, return false.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.