Check If It Is a Straight Line (LeetCode 1232): Solution and Explanation

Easy
Array
Math
Geometry

Given a list of distinct points [x, y], decide whether all of them lie on one straight line.

Intuition

Two distinct points always fix a line, so the first two points tell you which line every other point has to be on. The task reduces to checking each remaining point against that one line.

The obvious test compares slopes, (y - y0) / (x - x0), but that divides by zero on a vertical line and brings floating point error. Cross-multiplying removes the division: the offset (x - x0, y - y0) is parallel to the direction (dx, dy) exactly when dx * (y - y0) == dy * (x - x0).

That product is the 2D cross product of the two vectors. It is zero only when they point along the same line, and with coordinates up to 10^4 it fits comfortably in a 32-bit integer.

Approach: Cross-product check

  1. Take the first point as the anchor (x0, y0).
  2. Compute the direction to the second point: dx = x1 - x0, dy = y1 - y0.
  3. For every later point (x, y), return false if dx * (y - y0) != dy * (x - x0).
  4. If no point failed, return true.

Step-by-step example

coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
  1. Anchor (1, 2). Direction to (2, 3) is (dx, dy) = (1, 1).
  2. (3, 4): 1 * (4 - 2) = 2 and 1 * (3 - 1) = 2. Equal, on the line.
  3. (4, 5): 1 * 3 = 3 and 1 * 3 = 3. Equal.
  4. (5, 6) and (6, 7) give 4 = 4 and 5 = 5.
  5. No point failed, so return true.

Code: Python, Java, C++

1class Solution:
2    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
3        x0, y0 = coordinates[0]
4        x1, y1 = coordinates[1]
5        dx, dy = x1 - x0, y1 - y0
6        for x, y in coordinates[2:]:
7            if dx * (y - y0) != dy * (x - x0):
8                return False
9        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Cross-product check O(n)
Compare slopes with division O(n)
Reduced slope fractions O(n log C)
Time

O(n)

Each point is checked once with two multiplications.

Space

O(1)

Only the anchor and the direction are stored.

Other approaches

Compare slopes with division (time O(n), space O(1))

Compute the slope from the first point to every other point and check they are equal. It needs a special case for vertical lines and floating point tolerance, so it is easy to get wrong.

Reduced slope fractions (time O(n log C), space O(1))

Store each slope as (dy / g, dx / g) with g = gcd, normalising the sign. Exact and division-safe, but more code than the cross product for the same result.

Common mistakes

  • Dividing to get slopes, which crashes or returns infinity on vertical lines like [[2,1],[2,5],[2,9]].
  • Comparing floating point slopes with ==, which can fail on points that really are collinear.
  • Comparing each point with its neighbour instead of a fixed anchor while also dividing, which doubles the chances of a zero denominator.
  • Mixing up the cross terms: the test is dx * (y - y0) against dy * (x - x0), not dx * (x - x0).

Frequently asked questions

Why use the first two points as the reference?

Any two distinct points define the line, and the input guarantees no duplicates, so the first two are always a valid reference.

Can the products overflow?

No. Each factor is at most 2 * 10^4 in absolute value, so a product is at most 4 * 10^8, well inside a 32-bit int.

What if there are only two points?

The loop never runs and the answer is true, since two points always lie on a line.