Asteroid Collision Problem Description
Given an array/list ASTEROIDS
representing asteroids aligned in a row, each element's absolute value identifies the asteroid's size, while its sign indicates the direction (+ve means moving right, -ve means moving left). An asteroid with a value of 0 is massless and moves to the right. Your task is to determine the state of the asteroids after all possible collisions have occurred.
Example:
Input:
ASTEROIDS = {3, -2, 4}
Output:
{3, 4}
Explanation:
The first asteroid will destroy the second asteroid. Thus, the remaining asteroids are {3, 4}.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
-10^5 <= ASTEROIDS[i] <= 10^5
- Where
ASTEROIDS[i]
denotes the i-th asteroid. - Time Limit: 1 sec
Input:
The first line contains an integer ‘T’ for the number of test cases.
The second line of each test case contains an integer ‘N’ for the number of elements.
The third line contains ‘N’ space-separated integers denoting the arrayASTEROIDS
.
Output:
For each test case, print all remaining asteroids separated by a space.
Note:
Implementation is needed for the collision logic; printing is handled elsewhere.

AnswerBot
4mo
Determine the state of asteroids after collisions occur.
Iterate through the array of asteroids and simulate collisions between adjacent asteroids.
Use a stack to keep track of remaining asteroids after...read more
Help your peers!
Add answer anonymously...
Dunzo Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Asteroid Collision Problem Description Given an array/list ASTEROIDS representin...read more
A Software Developer Intern was asked Q. Word Break II Problem Statement Given a non-empty string 'S' containing no space...read more
A Software Developer Intern was asked Q. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Asteroid Collision Problem Description Given an array/list ASTEROIDS representin...read more
A Software Developer Intern was asked Q2. Word Break II Problem Statement Given a non-empty string 'S' containing no space...read more
A Software Developer Intern was asked Q3. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

