Count Collisions with Ball X
Updated
Problem statement
Balls stand on a line: ball i starts at position i, for i = 0, 1, 2, .... All of them start moving in the same direction at the same moment. Ball i moves at constant speed speeds[i], and balls pass through each other without changing speed.
Count how many other balls ball x meets:
- a ball behind ball
x(smaller position) meets it if it is faster; - a ball ahead of ball
x(larger position) meets it if it is slower.
Balls with the same speed as ball x never meet it. Return the count.
Function
countCollisions(speeds: int[], x: int) → intExamples
Example 1
speeds = [3,7,5,2,6]x = 2return = 2Ball 2 has speed 5. Behind it, ball 1 (speed 7) is faster and catches it; ball 0 (speed 3) is slower and never does. Ahead of it, ball 3 (speed 2) is slower, so ball 2 catches it; ball 4 (speed 6) is faster and stays ahead. That is 2 collisions.
Example 2
speeds = [4,4,4]x = 1return = 0All balls have the same speed, so the gaps never change and nothing collides. The answer is 0.
Example 3
speeds = [9]x = 0return = 0There are no other balls, so the answer is 0.
Constraints
1 <= speeds.length <= 2 * 10^41 <= speeds[i] <= 10^50 <= x < speeds.length
More Microsoft Phone Screen questions
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
- Infer a Lexicographically Smallest Alien AlphabetPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Paginate Retained Conversation HistorySierra · PHONE SCREENSierra interview questions
- Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREENGlean interview questions
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Bounded IPv4 FilterAmazon · PHONE SCREEN
- Partition and Group Values by ParityEPAM · PHONE SCREENEPAM interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)