FastPrepMax Points on a Line

Max Points on a Line

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an array of two-dimensional integer points, return the maximum number of input points that lie on one straight line. Repeated coordinates are separate input points and each copy counts.

Use exact integer geometry. Do not compare floating-point slopes: nearby rational slopes may round to the same value, and vertical lines have no finite slope.

Function

maxPointsOnLine(points: int[][]) → int

Examples

Example 1

points = [[1,1],[2,2],[3,3]]return = 3

All three distinct points are on the same diagonal line.

Example 2

points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]return = 4

Four points lie on the descending line through [1,4] and [4,1].

Example 3

points = [[0,0],[0,0],[1,1],[2,2],[1,0]]return = 4

Both copies of [0,0] count on the line through [1,1] and [2,2].

Constraints

  • 1 <= points.length <= 300.
  • Each row contains exactly two integer coordinates [x, y].
  • -1000000 <= x, y <= 1000000.
  • Repeated coordinates are allowed; each occurrence counts separately.

More Microsoft problems

See Microsoft hiring insights
public int maxPointsOnLine(int[][] points) {
    // Write your code here.
}
points[[1,1],[2,2],[3,3]]
expected3
Checking account…