Asteroid Collision (IMC Sydney π)
Updated
Problem statement
In this game, there are a number asteroids arranged in line. These asteroids can all move either left or right on the line and all asteroids move at the same speed. Each asteroid also has a size associated with it. If two asteroids that are next to each other move towards each other, they will collide. In this case, the larger asteroid will destroy the smaller one. If both asteroids have the same size, then both asteroids are destroyed. The purpose of this game is to determine which asteroids remain after all possible collisions occur.
Thus, given an array size of length n, where size[i] is the size of the ith asteroid and a second array direction of size n where direction[i] is the direction of the ithasteroid where 0 indicates moving left and 1 indicates moving right.
Given these inputs, find which asteroids remain after all collisions have taken place. Return an array containing the sizes of the remaining asteroids (in order). If no asteroids remain, then return an empty array.
Function
asteroidCollision(size: int[], direction: int[]) β int[]Examples
Example 1
size = [4, 5, 6, 7, 4]direction = [1, 1, 0, 1, 0]return = [6, 7]A direction value of 1 indicates moving to the right and a direction value of 0 indicates moving to the left. Collisions:
- size[1], size[2]: 5 < 6 so size[1] vanishes
- size[0], size[2]: 4 < 6 so size[0] vanishes and there are no more asteroids to the left of size[2]
- size[3], size[4]: 7 > 4 so size[4] vanishes and there are no more asteroids to the right of size[4]
Only size[2] and size[3] remain. Return them in the order they appear in size [6, 7].
Example 2
size = [3, 4, 2, 1, 6, 4, 5]direction = [1, 0, 1, 0, 1, 1, 0]return = [4, 2, 6]The initial size array = [3, 4, 2, 1, 6, 4, 5] with direction = [1, 0, 1, 0, 1, 1, 0] The collisions are between asteroids with sizes:
- (3, 4) 4 remains
- (2, 1) 2 remains
- (4, 5): size 5 survives this collision and then meets size 6
- (6, 5) 6 remains
The remaining asteroids are [4, 2, 6].
Example 3
size = [5, 5]direction = [1, 0]return = []The initial sizes are [5, 5] and directions are [1, 0]. The two asteroids move toward one another. Their sizes are equal, so both disappear and the result is [].
Constraints
- For this exercise, assume
1 <= n <= 10^5, wheren = size.length = direction.length. - For this exercise, assume
1 <= size[i] <= 10^9. direction[i]is0(left) or1(right).
More IMC Trading interview questions
- Earliest Meeting Start on a 24-Hour ClockOA Β· Seen Nov 2026
- Grid Paths with Override PassesOA Β· Seen Oct 2026
- Pro-Rata Order BookONSITE INTERVIEW Β· Seen Sep 2026
- Election District Budget Allocation AnalysisPHONE SCREEN Β· Seen Aug 2026
- Rock JumpingOA Β· Seen Aug 2026
- Stack Batch RemovalOA Β· Seen Aug 2026
- Weighted LFU CacheOA Β· Seen Aug 2026
- Chain of CommandOA Β· Seen Aug 2026
- Choose ContainersOA Β· Seen Aug 2026
- Sub-matrix SumsOA Β· Seen Jul 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit Β· OA Β· PHONE SCREEN Β· ONSITE INTERVIEW
- Sum of Subarray MinimumsNVIDIA Β· OANVIDIA interview questions
- Shopkeeper Final Price SummaryMicrosoft Β· OAMicrosoft interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox Β· OARoblox interview questions
- Maximum Capped Histogram Area After One RemovalMyntra Β· OAMyntra interview questions
- Steps to the Next Higher Value in a CircleRippling Β· OARippling interview questions
- Shopkeeper Final PriceUber Β· OAUber interview questions
- Predict AnswerAmazon Β· OA
- Retention Policy CollisionBox Β· OABox interview questions
- Longest Prefix-Valid Binary SubarrayHackerRank Β· OAHackerRank interview questions