File: check-if-it-is-a-straight-line/solution.py

Date: 2026-06-06

Time: 15:39

check-if-it-is-a-straight-line/solution.py

Purpose

Solves LeetCode 1232 — Check If It Is a Straight Line. Given a list of 2D coordinate pairs, determines whether all points are collinear (lie on a single straight line). This is the sole solution module for this problem directory.

Key Components

Solution.checkStraightLine(coordinates) — The only public method. Takes a list of [x, y] pairs and returns a boolean.

The method uses the cross-product collinearity test. It computes a reference direction vector (dx, dy) from the first two points, then checks every subsequent point against it.

Patterns

Cross-product instead of slope division. The classic approach would compute slope as dy/dx and compare, but that breaks when dx == 0 (vertical lines) and introduces floating-point imprecision. This solution avoids both problems by using the cross-product formulation:


(x_i - x_0) * dy - (y_i - y_0) * dx == 0

This is an integer-only comparison (assuming integer coordinates, which LeetCode guarantees), so it's exact — no epsilon needed, no division-by-zero guard.

Anchor-point strategy. Rather than comparing consecutive pairs, all points are measured against a single anchor (coordinates[0]) and direction (coordinates[0] → coordinates[1]). This is simpler and avoids accumulated error.

Dependencies

Imports: typing.List — used only for the type annotation on the method signature. No runtime dependencies.

Imported by: check-if-it-is-a-straight-line/test_solution.py directly. The "Imported By" list in the prompt is misleading — it reflects test files across the entire repo that import Solution from their own local solution.py, not from this file.

Flow

1. Extract direction vector (dx, dy) from coordinates[0] to coordinates[1].

2. For each point coordinates[i] where i >= 2:

3. If the loop completes, all points are collinear — return True.

Invariants

Error Handling

None. The method trusts its caller to provide valid input per the LeetCode contract. No bounds checking, no type validation, no exception handling.

Topics to Explore

Beliefs