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 <= 1000coordinates[i].length == 2-10000 <= x, y <= 10000No two points are identical.
Hints for Check If It Is a Straight Line
Hint 1
Comparing slopes as
dy / dxbreaks on a vertical line and invites floating-point error.Hint 2
Cross-multiply instead: the points are collinear when
dx1 · dy2 == dy1 · dx2.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
- Take
(dx, dy)as the vector from point 0 to point 1. - For each later point
(x, y), comparedx · (y - y0)withdy · (x - x0). - Return
falseon the first mismatch,trueotherwise.
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 / dxdivides 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 TrueJava
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