Problem 25 of 75

Asteroid Collision

MediumNot solved yetInterview me on this
Before you code · which pattern fits?

The problem

We are given an array asteroids: the absolute value is its size, the sign its direction (positive = right, negative = left). All move at the same speed. When two asteroids meet, the smaller explodes (both explode if equal). Asteroids moving the same direction never meet.

Return the state of the asteroids after all collisions.

[5,10,-5] → [5,10] [8,-8] → [] [10,2,-5] → [10]

Your solution runs against 4 unit tests, including edge and large-value cases.

Loading...

Test console · 4 unit tests

Submit your code to run it against all 4 unit tests. Results appear here.

JavaScript, TypeScript & Python run sandboxed in your browser; other languages run on the execution server. Your code is saved locally as you type.

Hint

Reference solution

Video explanation

Asteroid Collision, explainedNeetCode