Car Fleet
Problem Statement
n cars drive along a one-lane road towards the same destination at mile target. Car i starts at position[i] and drives at speed[i] miles per hour.
A car can never pass the car in front of it. If it catches up, it slows down and drives right behind it at the slower speed. Cars driving together like this form a fleet. A single car on its own also counts as a fleet.
If a car catches up with a fleet exactly at the destination, it still counts as part of that fleet.
Return how many fleets arrive at the destination.
An integer target, and two integer arrays position and speed of length n. All positions are different.
The number of car fleets that reach target.
The cars at 10 and 8 both need 1 hour, so they meet exactly at 12: one fleet. The car at 3 (3 hours) catches the car at 5 (7 hours): one fleet arriving at 7 hours. The car at 0 needs 12 hours and catches nobody.
One car is one fleet.
The car at 4 needs 96 hours. The others would be faster alone (25 and 49 hours), so they catch up and all arrive together.
- • n == position.length == speed.length
- • 1 ≤ n ≤ 10^5
- • 0 < target ≤ 10^6
- • 0 ≤ position[i] < target
- • All values of position are unique
- • 0 < speed[i] ≤ 10^6
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
A car leads its own fleet only if no car ahead arrives at the same time or later
Sort front-to-back; push a car's time only if it is slower than the fleet ahead
Compute each car's time to the target if it drove alone. A car ends up behind someone if any car in front of it would arrive no earlier: it catches that car at or before the target. So count the cars for which no car ahead has time ≥ their time. Each such car leads its own fleet.
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 Monotonic Stack translate into production architectures and low-level memory mechanics.