← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Apr 2026

Summary

Got an Applied Scientist screen at Microsoft that came down to a single geometry problem. Not the ML theory deep-dive I was expecting.

Questions Asked (1)

Q1

Given an array of 2D points, find the maximum number of points that all lie on the same straight line.

Algorithms & Data Structures
Author's notes

I knew the trick was slope comparison but fumbled the edge cases for a while.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying edge cases (e.g., duplicate points, fewer than 3 points) and then propose an O(N^2) solution: for each point, compute slopes to all other points and find the most frequent slope. Use a hash map to count slopes, handling vertical lines and duplicates separately.

Pro tip: Mention that using a reduced fraction (dx/gcd, dy/gcd) as the key avoids floating-point precision issues and is more robust than using doubles. Also, discuss how to handle duplicate points by counting them separately and adding to the max count.

1. Clarify requirements and edge cases

Ask about input size, duplicate points, and whether points are integers. Discuss handling of N < 3, all points collinear, and vertical lines.

2. Outline brute force and optimal approach

Mention that brute force O(N^3) is too slow, and propose the O(N^2) slope-based approach using a hash map.

3. Detail the algorithm

For each point i, compute slopes to all j > i, using reduced fractions (dx/gcd, dy/gcd) as keys. Track the maximum count of points with the same slope, including duplicates.

4. Handle special cases

Explain how to handle vertical lines (dx=0), horizontal lines (dy=0), and duplicate points (dx=0, dy=0) by counting them separately and adding to the max.

5. Analyze complexity and test

State time complexity O(N^2) and space O(N). Walk through a small example to verify correctness.

Key Points to Mention

  • Use of greatest common divisor (GCD) to reduce slope fractions and avoid floating-point errors.
  • Handling of duplicate points by counting them and adding to the maximum count for each slope.
  • Special cases: vertical lines (infinite slope) and horizontal lines (zero slope).
  • Time complexity O(N^2) and space complexity O(N) for the hash map.
  • Edge cases: N < 3, all points collinear, and points with same coordinates.
  • Alternative approaches: using a map of maps or sorting points, but slope-based is optimal.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.