Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Asteroid Collision Problem

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Asteroid Collision Problem. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Solve the Asteroid Collision problem.

Asked in the Coding and DevOps/SRE stage. Interviewed in Python; logic was explained first, followed by live coding on the shared terminal.

Input and Output

Given an integer array asteroids, positive values move right and negative values move left. Every asteroid moves at the same speed. Return the array after all possible collisions, preserving the original order of surviving asteroids. A collision occurs only when a right-moving asteroid is immediately followed by a left-moving asteroid. The smaller asteroid is destroyed; equal sizes destroy both.

Constraints

  • 2 <= len(asteroids) <= 10^4
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

Function Signature

def asteroid_collision(asteroids):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output