Meta | E4 | Onsite
Anonymous User
2572

I recently went through onsite loop at Meta and I was asked two questions in one of the coding rounds

  1. First question is exactly same as https://leetcode.com/problems/asteroid-collision/description/
  2. Second question is a similar to first question but when two asteroids with same size collide (i.e when the one with negative sign collides with positive sign) instead of canceling each other out( like in first question) they change the direction and move in opposite directions (positive one now becomes negative and moves back and negative one become positive). We need to return the resultant array. See examples below.

For example

  1. [3,8,-2,-8]
    Ans [-8,8]
    Explanation : Up until -2, the problem is similar to question #1, but when we encounter -8, because the number on the top of the stack is 8, when they collide, they both change directions. -8 becomes 8 and the number on the top of the stack becomes -8 and moves in the opposite direction and crushes everything with smaller weight(and moving in opposite direction). So 3 in the stack gets crushed, resulting in [-8,8]

  2. [6,2,4,-3,6,1,3,-5,-6, 4,-6]
    Ans : [-6,-6,6,6]

I couldn't solve the second question. Does any one know how to approach this ?

Comments (20)