Surviving Balls after Directional Collisions
Updated
Problem statement
Balls are arranged from left to right. Ball i has direction directions[i], where 1 means right and -1 means left, and positive strength strengths[i].
All balls move at the same speed. A collision occurs when a right-moving ball is immediately to the left of a left-moving ball among the balls that remain.
- The ball with greater strength survives with its strength unchanged.
- If the strengths are equal, both balls are destroyed.
Continue until no collision is possible. Return the original zero-based indexes of the surviving balls in increasing order.
Function
survivingBallIndexes(directions: int[], strengths: int[]) → int[]Examples
Example 1
directions = [1,1,-1,-1]strengths = [5,3,4,5]return = []Ball 2 destroys ball 1, then ball 0 destroys ball 2. Finally balls 0 and 3 have equal strength, so both are destroyed.
Example 2
directions = [-1,1,1]strengths = [2,7,1]return = [0,1,2]No right-moving ball has a left-moving ball to its right, so no collision occurs.
Constraints
1 <= directions.length = strengths.length <= 200000directions[i]is either-1or1.1 <= strengths[i] <= 10^9
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
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
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Asteroid CollisionZscaler · ONSITE INTERVIEWZscaler 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