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 array ASTEROIDS.
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 in a row.

  • Iterate through the array of asteroids and simulate collisions between adjacent asteroids moving in opposite directions.

  • If two asteroid...read more

Help your peers!
Select
Add answer anonymously...
Wayground (formerly Quizizz) Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits