Asteroid Collision
Problem Statement
You are given an array asteroids of asteroids flying in a row. For each one, the absolute value is its size and the sign is its direction: positive moves right, negative moves left. All of them move at the same speed.
When two asteroids meet, the smaller one explodes. If they are the same size, both explode. Two asteroids moving in the same direction never meet.
Return the asteroids that are left after every possible collision, in their original order.
An integer array asteroids (no zeros).
The surviving asteroids, left to right.
-5 crashes into 10 and explodes. 5 and 10 both move right, so they never meet.
Same size, so both explode.
-5 destroys 2, then crashes into 10 and explodes.
-6 destroys 5 and then 3. -1 explodes against 2. -6 moves left and 2, 4 move right, so nothing else meets.
- • 2 ≤ asteroids.length ≤ 10^4
- • −1000 ≤ asteroids[i] ≤ 1000
- • asteroids[i] ≠ 0
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Repeatedly find the first adjacent (+, −) pair, resolve it, start over
Push right-movers; a left-mover pops smaller right-movers off the top in a loop
Simulate it literally. Scan for the first place where a right-mover is directly followed by a left-mover. Resolve that crash by erasing the smaller one (or both if equal). Then scan again from the start. Stop when no such pair exists.
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 Stack translate into production architectures and low-level memory mechanics.