← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Amazon Applied Scientist interview with a coding question on stack-based simulation. Pretty straightforward problem but the edge cases are where things get dicey.

Questions Asked (1)

Q1

Given an array of integers representing asteroids moving in a row, where sign indicates direction and magnitude indicates size, return the state of the array after all collisions have resolved. Same-direction asteroids never collide; when two meet, the smaller one is destroyed; equal sizes both explode.

Algorithms & Data Structures
Author's notes

Stack problem once you see it, but I didn't see it right away.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to simulate collisions by iterating through the asteroids. For each asteroid, if it moves right, push it; if it moves left, resolve collisions with the stack top until it is destroyed or the stack is empty or the top moves left. Finally, the stack contains the surviving asteroids.

Pro tip: Clarify the collision rules upfront and walk through a small example to demonstrate your understanding. Emphasize that only right-moving asteroids followed by left-moving ones can collide, and handle edge cases like all asteroids moving in the same direction.

1. Understand the problem

Restate the problem to ensure clarity: asteroids move in a row, signs indicate direction, magnitudes indicate size. Collisions occur only between a right-moving asteroid and a left-moving asteroid that are adjacent after previous collisions.

2. Choose data structure

Select a stack to efficiently manage asteroids and resolve collisions. The stack will store asteroids that are moving right or have survived collisions.

3. Iterate and simulate

Traverse the array. For each asteroid, if it moves right, push onto stack. If it moves left, repeatedly compare with the top of the stack: if top is positive (right-moving), resolve collision based on sizes; if top is negative or stack empty, push the left-moving asteroid.

4. Handle collision outcomes

When a right-moving asteroid (top) and left-moving asteroid (current) collide: if top is smaller, pop and continue comparing; if equal, pop and discard current; if top is larger, discard current. If current survives all comparisons, push it.

5. Return result

After processing all asteroids, the stack contains the final state. Convert the stack to an array and return it.

Key Points to Mention

  • Use a stack to simulate collisions efficiently.
  • Only right-moving asteroids followed by left-moving ones can collide.
  • Time complexity: O(n) since each asteroid is pushed and popped at most once.
  • Space complexity: O(n) for the stack.
  • Edge cases: all asteroids moving same direction, no collisions, equal sizes exploding.
  • Walk through an example to validate the approach.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.