← Questions

Max points on a line

0
MicrosoftCodingLast asked Feb 2026

A coding interview question asked in Microsoft's technical phone screen, last asked Feb 2026. It tests geometry, hashing and math.

Given points with integer coordinates on a plane, return the largest number of them that lie on one straight line. The list may contain the same point more than once; each copy counts.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • max_points(points: list[list[int]]) -> int

Example

max_points([[1, 1], [2, 2], [3, 3]])                              ->  3
max_points([[1, 1], [3, 2], [5, 3], [4, 1], [2, 3], [1, 4]])      ->  4   # (3,2), (4,1), (2,3), (1,4)
max_points([[0, 0], [0, 0], [1, 1], [2, 3]])                      ->  3   # both copies of (0,0) and (1,1)
max_points([[2, 2]])                                              ->  1

Constraints

  • 1 <= len(points) <= 300
  • -10^4 <= x, y <= 10^4

Clarifying questions

  • Can points repeat, and does each copy count?
  • Are coordinates integers?
  • What is the answer for one point?

What a strong answer covers

  • For each anchor point, group every other point by the direction to it; the best line through the anchor is the largest group plus the anchor and its duplicates.
  • Representing a direction exactly: reduce (dx, dy) by their gcd and normalise the sign, so vertical and horizontal lines need no special case and floating-point slopes never collide or split.
  • Counting duplicates of the anchor separately and adding them to every line through it.
  • O(n^2) time with a hash map per anchor, O(n) extra space.
  • Tests: all points identical, vertical lines, negative slopes, two points.

Follow-ups

  • Return the line (two points on it), not just the count.
  • Points have floating-point coordinates: how do you define "on a line"?
  • Points stream in one at a time; keep the answer up to date.

Where it's asked

What it tests

geometry · hashing · math

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding