Check If It Is a Straight Line

Easy
555.

Check If It Is a Straight Line

Check If It Is a Straight Line is an easy coding problem on arrays, math and geometry, asked in Amazon, Google, Meta and Palantir interviews.

You are given an array coordinates where coordinates[i] = [x, y] is a point in the plane.

Return true if all the points lie on a single straight line.

Example 1

Input
coordinates = [[1,2],[2,3],[3,4],[4,5]]
Output
true

Example 2

Input
coordinates = [[1,1],[2,2],[3,4]]
Output
false

Example 3

Input
coordinates = [[0,0],[0,1],[0,5]]
Output
true
Explanation
A vertical line is still a line.

Constraints

  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -10000 <= x, y <= 10000
  • No two points are identical.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[][]} coordinates
 * @return {boolean}
 */
var checkStraightLine = function(coordinates) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Check If It Is a Straight Line — Problem & Solution

Check If It Is a Straight Line solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Check If It Is a Straight Line is an easy coding problem on arrays, math and geometry, asked in Amazon, Google, Meta and Palantir interviews.

  • Difficulty: Easy
  • Topics: Arrays, Math, Geometry
  • Asked at: Amazon, Google, Meta, Palantir
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given an array coordinates where coordinates[i] = [x, y] is a point in the plane.

Return true if all the points lie on a single straight line.

Example 1

Input: coordinates = [[1,2],[2,3],[3,4],[4,5]]
Output: true

Example 2

Input: coordinates = [[1,1],[2,2],[3,4]]
Output: false

Example 3

Input: coordinates = [[0,0],[0,1],[0,5]]
Output: true
Explanation: A vertical line is still a line.

Constraints

  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -10000 <= x, y <= 10000
  • No two points are identical.

Hints for Check If It Is a Straight Line

  1. Hint 1

    Comparing slopes as dy / dx breaks on a vertical line and invites floating-point error.

  2. Hint 2

    Cross-multiply instead: the points are collinear when dx1 · dy2 == dy1 · dx2.

  3. Hint 3

    Fix the direction vector from the first two points and test every remaining point against it.

How to solve Check If It Is a Straight Line

Collinearity is a cross-product test. Using multiplication rather than division keeps everything in exact integer arithmetic and handles vertical lines with no special case.

Approach

  1. Take (dx, dy) as the vector from point 0 to point 1.
  2. For each later point (x, y), compare dx · (y - y0) with dy · (x - x0).
  3. Return false on the first mismatch, true otherwise.

Why it works

The two expressions are the components of the cross product of (dx, dy) with (x - x0, y - y0). It is zero exactly when the vectors are parallel — that is, when the point lies on the line through the first two.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Using dy / dx divides by zero on a vertical line and loses precision elsewhere.
  • With coordinates up to 10,000 the products reach 4·10^8 — inside 32 bits, but widening to 64 costs nothing.
  • The reference direction must come from two distinct points, which the constraints guarantee.

Reference solution

Python

from typing import List

def checkStraightLine(coordinates: List[List[int]]) -> bool:
    x0, y0 = coordinates[0]
    x1, y1 = coordinates[1]
    dx, dy = x1 - x0, y1 - y0
    for x, y in coordinates[2:]:
        if dx * (y - y0) != dy * (x - x0):
            return False
    return True

Java

public static boolean checkStraightLine(int[][] coordinates) {
    long x0 = coordinates[0][0], y0 = coordinates[0][1];
    long dx = coordinates[1][0] - x0, dy = coordinates[1][1] - y0;
    for (int i = 2; i < coordinates.length; i++) {
        long x = coordinates[i][0], y = coordinates[i][1];
        if (dx * (y - y0) != dy * (x - x0)) return false;
    }
    return true;
}

C++

bool checkStraightLine(vector<vector<int>>& coordinates) {
    long long x0 = coordinates[0][0], y0 = coordinates[0][1];
    long long dx = coordinates[1][0] - x0, dy = coordinates[1][1] - y0;
    for (int i = 2; i < (int) coordinates.size(); i++) {
        long long x = coordinates[i][0], y = coordinates[i][1];
        if (dx * (y - y0) != dy * (x - x0)) return false;
    }
    return true;
}

JavaScript

var checkStraightLine = function(coordinates) {
    const x0 = coordinates[0][0], y0 = coordinates[0][1];
    const dx = coordinates[1][0] - x0, dy = coordinates[1][1] - y0;
    for (let i = 2; i < coordinates.length; i++) {
        const x = coordinates[i][0], y = coordinates[i][1];
        if (dx * (y - y0) !== dy * (x - x0)) return false;
    }
    return true;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays