Asteroid Collision
Reported by candidates from Zscaler's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure carries this whole problem, and it's a stack. Zscaler reported Asteroid Collision in September 2024, and it's a clean array problem that looks like simulation until you spot the stack. If your OA invite lands in the next day or two, this is one to nail. Positive numbers go right, negative go left, and only a right-mover followed by a left-mover ever collides. StealthCoder is the safety net running invisibly during the live OA if your mind goes blank, but the idea is small enough to hold in your head.
The problem
You are given an integer array asteroids representing asteroids in a row. Absolute value is size, and sign is direction: positive moves right and negative moves left. All asteroids move at the same speed. When two asteroids moving toward each other collide, the smaller one explodes. If their sizes are equal, both explode. Asteroids moving in the same direction never collide. Return the asteroids remaining after every collision, in original left-to-right order. Function asteroidCollision(asteroids: int[]) → int[] Examples Example 1 asteroids = [5,10,-5] return = [5,10] Asteroid 10 destroys -5; 5 never collides. Example 2 asteroids = [8,-8] return = [] The approaching asteroids have equal size, so both explode. Example 3 asteroids = [10,2,-5] return = [10] -5 destroys 2, then 10 destroys -5. Constraints 1 <= asteroids.length <= 100000 -10^9 <= asteroids[i] <= 10^9 asteroids[i] != 0
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the array once and keep a stack of survivors. Push any positive asteroid. When you hit a negative one, it only fights if the stack top is positive. While the top is positive and smaller than the absolute value of the incoming one, pop it. If the top equals it, pop and destroy both. If the top is larger, the incoming one dies. If the stack is empty or the top is negative, push it. The common pitfall is using an if instead of a while, so one negative asteroid fails to chew through several smaller positives. Another is forgetting the equal-size case, or letting a negative collide with a negative below it. This runs in O(n) time because each asteroid is pushed and popped at most once. With 100000 elements, anything quadratic is risky. If you freeze during the live OA, StealthCoder can hand you this loop as a hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Asteroid Collision cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as asteroid collision. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Zscaler's OA.
Zscaler reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Asteroid Collision FAQ
What's the trick to Asteroid Collision?+
Use a stack of survivors. Positives get pushed. A negative asteroid pops smaller positives off the top, kills itself against a bigger one, or cancels with an equal one. Collisions only happen between a right-mover on the stack and an incoming left-mover.
How hard is this problem really?+
Medium, but it's short once you see the stack. The difficulty is the case handling, not the algorithm. Most failures come from using an if instead of a while, or from missing the equal-size case where both explode.
What's the time and space complexity?+
O(n) time and O(n) space. Each asteroid is pushed once and popped at most once, so the inner while loop doesn't make it quadratic. The stack doubles as the output array in the worst case where nothing collides.
Which edge cases should I test before submitting?+
Test [8,-8] for the equal case, [10,2,-5] for chained destruction, and all negatives like [-2,-1] where nothing collides. Also try [-2,1,-3] mixed with leading negatives. A negative on an empty stack or on a negative top must just be pushed.
Is this pattern still asked, and how do I prep in 48 hours?+
Zscaler reported it in September 2024, and stack simulation problems show up often. In 48 hours, write this solution from scratch twice, then do two related stack problems. Focus on explaining why the while loop is needed and why the complexity stays linear.