FastPrepMax Points on a Line

Max Points on a Line

Uber logoUber● HardFULLTIMEPHONE SCREEN

Problem statement

Given an array points of distinct two-dimensional integer points, return the largest number of input points that lie on one straight line.

Each row is [x, y]. Vertical and horizontal lines are allowed. A single point gives an answer of 1.

Function

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

Examples

Example 1

points = [[0,0],[1,1],[-1,-1],[2,2],[-2,-2],[0,2]]return = 5

The points [-2,-2], [-1,-1], [0,0], [1,1], and [2,2] lie on y = x. The remaining point [0,2] is off that line. Thus the maximum is 5.

Example 2

points = [[2,-3],[2,0],[2,4],[0,0]]return = 3

The three points whose x coordinate is 2 lie on the vertical line x = 2. The fourth point is off it, so the maximum is 3.

Example 3

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

No straight line contains all three vertices of this triangle. Any two points share a line, so the maximum is 2.

Constraints

  • 1 <= points.length <= 300.
  • points[i].length == 2.
  • -10000 <= points[i][j] <= 10000.
  • No two points have the same pair of coordinates.
  • These numeric and distinct-point restrictions are explicit practice assumptions.

More Uber problems

See Uber hiring insights
Saved
public int maxPoints(int[][] points) {
    // Write your solution here.
}
points[[0,0],[1,1],[-1,-1],[2,2],[-2,-2],[0,2]]
expected5
Checking account…