Interview Questions/Coding/Asteroid Crash Simulator

Asteroid Crash Simulator

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Asteroid Crash Simulator

A line of asteroids is moving through space. Each asteroid is represented by an integer. The absolute value represents its size. A positive value means it moves to the right, and a negative value means it moves to the left. When two asteroids moving in opposite directions meet, the smaller one explodes. If both have the same size, both explode. Asteroids moving in the same direction never collide. Return the final state of the asteroids after all possible collisions have happened.

Examples

Example 1
Input:
5 10 -5
Output:
5 10

Approach hint

Only a right-moving asteroid followed later by a left-moving asteroid can collide.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp