Find maximum collinear points
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of planar geometry and algorithmic reasoning for identifying collinearity, including handling duplicate points and edge cases in integer coordinates.
Constraints
- 1 <= len(points) <= 2000
- -10^4 <= x_i, y_i <= 10^4
- Duplicate points may exist and must be counted in the result
Examples
Input: ([[1,1],[2,2],[3,3]],)
Expected Output: 3
Explanation: All three points lie on the line y = x.
Input: ([[1,1],[1,1],[2,2],[3,3]],)
Expected Output: 4
Explanation: The duplicate point [1,1] counts separately, and all four points lie on the same line.
Input: ([[2,3],[2,4],[2,5],[0,0]],)
Expected Output: 3
Explanation: The points [2,3], [2,4], and [2,5] lie on the vertical line x = 2.
Input: ([],)
Expected Output: 0
Explanation: There are no points, so the answer is 0.
Input: ([[0,0],[0,0],[0,0]],)
Expected Output: 3
Explanation: All three points are duplicates of the same location, so they all count on the same line.
Input: ([[0,0],[1,1],[1,-1],[2,2],[3,3],[2,-2]],)
Expected Output: 4
Explanation: The points [0,0], [1,1], [2,2], and [3,3] are collinear on y = x.
Hints
- Try fixing one point as an anchor and grouping all other points by the line they form with that anchor.
- Avoid floating-point slopes. Store each slope as a reduced `(dy, dx)` pair using `gcd`, and use a consistent sign convention. Count duplicate points separately.