FastPrepCollision
Problems/Collision

Collision

MathWorks logoMathWorks● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

There are n toy cars on a road. Their indices describe their initial order from left to right.

  • direction[i] = 1 means car i moves right.
  • direction[i] = -1 means car i moves left.
  • strength[i] is the strength of car i.

When two cars collide, the car with lower strength is destroyed. If their strengths are equal, both cars are destroyed. A surviving car continues moving in its original direction, so it may collide again.

Return the zero based indices of all surviving cars in increasing order after every possible collision.

Function

carsLeft(n: int, direction: int[], strength: int[]) → int[]

Examples

Example 1

n = 3direction = [1, -1, 1]strength = [5, 3, 2]return = [0, 2]

Car 0 moves right and car 1 moves left. Since strength[0] = 5 is greater than strength[1] = 3, car 1 is destroyed. Car 2 moves right and does not face another car, so the survivors are [0, 2].

Example 2

n = 6direction = [1, -1, -1, 1, -1, 1]strength = [7, 2, 3, 5, 5, 6]return = [0, 5]

Car 0 destroys cars 1 and 2 because its strength is greater than both of theirs. Cars 3 and 4 have equal strength, so both are destroyed. Car 5 moves right and survives. The surviving indices are [0, 5].

Constraints

  • n <= 2 * 10^5
  • direction[i] = 1 or direction[i] = -1
  • strength[i] <= 10^9

More MathWorks interview questions

  • Earliest Time to Schedule a MeetingOA · Seen Nov 2026
  • Minimum Cost to Serve Requests on a RingOA · Seen Nov 2026
  • Minimum Tree Diameter After Removing K LeavesOA · Seen Nov 2026
  • Cities on Shortest Delivery RoutesOA · Seen Nov 2026
  • Repeated Mirror XOROA · Seen Nov 2026
  • Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
  • Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
  • Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
  • Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
  • Balancing TeamsOA · Seen Nov 2025

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

MathWorks interview experiences

  • MathWorks SWE Intern Phone Screens: 1 Experience
  • MathWorks Software Engineer in Test OA and HireVue: 1 Experience
All MathWorks interview questionsSee MathWorks hiring insights
Saved
class Solution {
    public int[] carsLeft(int n, int[] direction, int[] strength) {
      // write your code here
    }

}
Checking account…