Asteroid Collision
Updated
Problem statement
You are given an integer array asteroids representing asteroids in a row. Absolute value is size, and sign is direction: positive moves right and negative moves left. All asteroids move at the same speed.
When two asteroids moving toward each other collide, the smaller one explodes. If their sizes are equal, both explode. Asteroids moving in the same direction never collide.
Return the asteroids remaining after every collision, in original left-to-right order.
Function
asteroidCollision(asteroids: int[]) → int[]Examples
Example 1
asteroids = [5,10,-5]return = [5,10]Asteroid 10 destroys -5; 5 never collides.
Example 2
asteroids = [8,-8]return = []The approaching asteroids have equal size, so both explode.
Example 3
asteroids = [10,2,-5]return = [10]-5 destroys 2, then 10 destroys -5.
Constraints
1 <= asteroids.length <= 100000-10^9 <= asteroids[i] <= 10^9asteroids[i] != 0
More Zscaler interview questions
- Check Whether a Byte Is NonzeroONSITE INTERVIEW · Seen Sep 2024
- Count Vowel PermutationsONSITE INTERVIEW · Seen Sep 2024
- Longest Common Subsequence LengthONSITE INTERVIEW · Seen Jun 2024
- Run-Length EncodingONSITE INTERVIEW · Seen Jun 2024
- Rate Limiter with Hourly and Daily Effort QuotasPHONE SCREEN · Seen Jul 2026
- Merging PalindromesPHONE SCREEN · Seen Sep 2024
- Find the Element With Odd FrequencyPHONE SCREEN · Seen Jun 2024
- Merge IntervalsPHONE SCREEN · Seen Jun 2024
- Sum of Divisors of the Array GCDPHONE SCREEN · Seen Jun 2024
- How Many Sentences? 🌿OA · Seen Mar 2024
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Asteroid CollisionBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Exclusive Time of FunctionsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Implement Stack Push and Pop OperationsLSEG · ONSITE INTERVIEWLSEG interview questions