Asteroid Collision
Updated
Problem statement
An array describes asteroids in a row. The absolute value of each integer is the asteroid's size, while its sign gives its direction: positive moves right and negative moves left. All asteroids move at the same speed.
When two asteroids meet, the smaller one is destroyed. If they have equal size, both are destroyed. Asteroids moving in the same direction never meet.
Return the surviving asteroids in their original order.
Function
asteroidCollision(asteroids: int[]) → int[]Examples
Example 1
asteroids = [5,10,-5]return = [5,10]The asteroid of size 10 destroys the left-moving asteroid of size 5.
Example 2
asteroids = [8,-8]return = []The two asteroids have equal size, so both disappear.
Example 3
asteroids = [-2,-1,1,2]return = [-2,-1,1,2]No right-moving asteroid appears to the left of a left-moving asteroid, so no collision occurs.
Constraints
1 <= asteroids.length <= 10^5-10^4 <= asteroids[i] <= 10^4asteroids[i] != 0
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Adding Stack 2.0IMC Trading · OAIMC Trading interview questions
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)