FastPrepCount Collisions with Ball X
Problems/Count Collisions with Ball X

Count Collisions with Ball X

Microsoft logoMicrosoft● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was an initial technical screen of a Microsoft software engineering internship process, per a review. The screen held a pair of coding problems. The other was a sorting task that makes a weighted total as good as possible. For this problem the review gives the goal and its rule: the chosen ball meets faster balls that start behind it and slower balls in front of it.

The review gives no input format or limits. Here balls sit at positions 0, 1, 2, and so on with positive speeds, pass through each other, equal speeds never meet, and you return the count for ball x. There are 1 to 2 * 10^4 balls with speeds from 1 to 10^5.

Our version is about 90% the same as the reported question.

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) → int

Examples

Example 1

speeds = [3,7,5,2,6]x = 2return = 2

Ball 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 = 0

All balls have the same speed, so the gaps never change and nothing collides. The answer is 0.

Example 3

speeds = [9]x = 0return = 0

There are no other balls, so the answer is 0.

Constraints

  • 1 <= speeds.length <= 2 * 10^4
  • 1 <= speeds[i] <= 10^5
  • 0 <= 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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int countCollisions(int[] speeds, int x) {
        // Write your code here.
    }
}
Checking account…