FastPrepAre All Points Collinear?

Are All Points Collinear?

Google logoGoogle● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

You are given a matrix points, where each row [x, y] is one point in the plane. Return true if all points lie on one straight line, and false otherwise.

Repeated coordinates are allowed. Zero, one, or two points are considered collinear. A vertical line is a valid straight line. Use exact integer geometry rather than floating-point slopes.

Function

arePointsCollinear(points: int[][]) → boolean

Examples

Example 1

points = [[1,2],[2,4],[3,6]]return = true

All points lie on y = 2x.

Example 2

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

The last point does not lie on the line through the first two.

Example 3

points = [[4,-1],[4,-1],[4,3]]return = true

The duplicated point and the distinct point lie on the vertical line x = 4.

Constraints

  • 0 <= points.length <= 10000.
  • Every row contains exactly two integer coordinates.
  • -1000000 <= x, y <= 1000000.

More Google problems

See Google hiring insights
public boolean arePointsCollinear(int[][] points) {
    // Write your code here
}
points[[1,2],[2,4],[3,6]]
expectedtrue
Checking account…