Frontend

Asteroid Collision (Tekion Frontend Interview) | Problem Solving Round

A classic stack-based problem asked in interviews at Tekion. You are given an array of integers where each value represents an asteroid’s direction and weight—positive moves left to right, negative moves right to left. When two asteroids collide, the heavier one survives. The goal is to determine which asteroids remain after all collisions are resolved.

ByteAndBites·Jul 04, 2026
Asteroid Collision (Tekion Frontend Interview) | Problem Solving Round
A classic stack-based problem asked in interviews at Tekion involves resolving collisions between asteroids represented by an array of integers. Each integer represents an asteroid's direction and weight: positive integers move from left to right, while negative integers move from right to left. When two asteroids collide, the heavier one survives, while the smaller one is destroyed. The challenge is to determine which asteroids remain after all collisions are resolved.

Concepts

This problem can be effectively solved using a stack data structure. The stack allows us to simulate the collision process by pushing and popping asteroids as they collide. The intuition behind using a stack is that it operates in a Last In First Out (LIFO) manner, which is ideal for this problem where we need to evaluate asteroids as they encounter each other sequentially.


Problem Breakdown

  • Initialization: We start with an empty stack which will hold the surviving asteroids.
  • Iteration: For each asteroid in the input array, we need to check if it can safely be added to the stack or if it will collide with the asteroid on the top of the stack.
  • Collision Resolution: If a collision occurs (i.e., the current asteroid is negative and the top of the stack is positive), we compare the sizes (absolute values) and pop the smaller asteroid from the stack. This process continues until no more collisions occur between the current asteroid and the top of the stack.
  • Final State: Asteroids that remain in the stack after processing the entire array represent the final state of asteroids after all collisions.

Solution for input [5, 8, -10, -7]
Solution for input [5, 8, -10, -7]


Solution

Below is a JavaScript implementation of the algorithm described:


JavaScript(collision.js)
function asteroidCollision(asteroids) {
    const stack = [];

    for (const asteroid of asteroids) {
        let alive = true;

        while (alive && asteroid < 0 && stack.length > 0 && stack[stack.length - 1] > 0) {
            const top = stack[stack.length - 1];

            if (Math.abs(asteroid) > top) {
                // The current asteroid is larger, pop the top
                stack.pop();
            } else if (Math.abs(asteroid) === top) {
                // Both asteroids are equal, pop and do not push
                stack.pop();
                alive = false; // Current asteroid is destroyed
            } else {
                // The asteroid on the stack is larger, do not add current asteroid
                alive = false; 
            }
        }

        if (alive) {
            stack.push(asteroid); // Push the current asteroid if it's still alive
        }
    }

    return stack; // Return the final array of asteroids
}

// Example usage:
const asteroids = [5, 8, -10, -7];
const result = asteroidCollision(asteroids);
console.log(result); // Output: [-10, -7]

Conclusion

The stack-based approach effectively handles the asteroid collision problem by maintaining the state of surviving asteroids throughout the process. By carefully evaluating each asteroid against the last one added to the stack, we can efficiently resolve collisions and return the final array. This method not only solves the problem neatly but also demonstrates the power of using stacks for specific algorithmic challenges.


Similar Questions

  • Evaluate Reverse Polish Notation
  • Implement a Queue using Stacks
  • Parentheses Matching Problem
  • Next Greater Element using a Stack


InterviewTekionArrayStack
Frontend Interview Playbook 🚀
Series
Frontend Interview Playbook 🚀
View series
A curated series of real frontend interview questions from top companies like Google, Amazon, Uber, Atlassian, Roku, etc. Each blog breaks down problems, thought processes, and optimal solutions to help you master concepts, improve problem-solving, and confidently crack frontend interviews at leading tech companies.