Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Stack

Asteroid Collision

MEDIUM✓ CHIP-TIMEDLC #735 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Simulate a row of asteroids drifting left or right — sign gives direction, magnitude gives size. Same-size collisions destroy both; unequal ones destroy the smaller. Return whatever survives, left to right.

A row of asteroids arrives as an array of nonzero integers. Each value's sign gives its direction — positive drifts right, negative drifts left — and its magnitude gives its size.

Two asteroids collide only when a right-moving one is immediately followed, later in the row, by a left-moving one closing the gap between them. On collision, the smaller one is destroyed; equal sizes destroy both.

Asteroids moving the same direction never catch each other and never collide. The drill hands back whatever asteroids remain once every possible collision has played out, left to right.

  • row holds up to a couple thousand asteroids
  • sizes range widely and are never zero
  • equal-size collisions destroy both asteroids
  • same-direction asteroids never collide with each other
EX 01
asteroids = [5, 10, -5]
→ [5, 10]
smaller asteroid destroyed on impact
EX 02
asteroids = [8, -8]
→ []
equal sizes mutually destroy
EX 03
asteroids = [10, 2, -5]
→ [10]
large survivor eats a chain of smaller right-movers

The hintstake only what you need

HINT 1 THE NUDGE

A collision only ever happens between a right-mover and the very next left-mover after it — same-direction asteroids never catch each other. What needs to be remembered as you scan?

HINT 2 THE STRUCTURE

A stack holds “survivors confirmed so far.” A new left-mover only has to fight the top of the stack if that top is moving right; keep resolving until it can't fight (or the stack empties), then push whatever's left of it.

HINT 3 ONE STEP FROM THE ANSWER

For each value: while the top is a smaller right-mover than an incoming left-mover, pop it (destroyed). If the top's magnitude equals the incomer's, pop it and the incomer too. Otherwise, if the top is a larger or equal right-mover, the incomer is destroyed and nothing pushes.

Coach's boardthe pattern, step by step

THE SURVIVORS STACKPATTERN · STACK OF SURVIVORSasteroids = [5, 10, -6, -10, -20]
5
10
-6
-10
-20
SURVIVOR STACK
— empty —
STEP 1

Right-movers push freely; a left-mover fights the top of the stack while that top is a smaller or equal right-mover.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/asteroid-collision.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        stack = []
        for a in asteroids:
            alive = True
            while alive and a < 0 and stack and stack[-1] > 0:
                if stack[-1] < -a:
                    stack.pop()
                    continue
                elif stack[-1] == -a:
                    stack.pop()
                alive = False
            if alive:
                stack.append(a)
        return stack
TIME O(N)SPACE O(N)PYTHON · RACE PACE · 15 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED