Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] Output: true
Example 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] Output: false
Constraints:
2 <= coordinates.length <= 1000coordinates[i].length == 2-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4coordinates contains no duplicate point.Problem summary: You are given an integer array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
[[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1232: Check If It Is a Straight Line
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x1 = coordinates[0][0], y1 = coordinates[0][1];
int x2 = coordinates[1][0], y2 = coordinates[1][1];
for (int i = 2; i < coordinates.length; ++i) {
int x = coordinates[i][0], y = coordinates[i][1];
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1232: Check If It Is a Straight Line
func checkStraightLine(coordinates [][]int) bool {
x1, y1 := coordinates[0][0], coordinates[0][1]
x2, y2 := coordinates[1][0], coordinates[1][1]
for i := 2; i < len(coordinates); i++ {
x, y := coordinates[i][0], coordinates[i][1]
if (x-x1)*(y2-y1) != (y-y1)*(x2-x1) {
return false
}
}
return true
}
# Accepted solution for LeetCode #1232: Check If It Is a Straight Line
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
x1, y1 = coordinates[0]
x2, y2 = coordinates[1]
for x, y in coordinates[2:]:
if (x - x1) * (y2 - y1) != (y - y1) * (x2 - x1):
return False
return True
// Accepted solution for LeetCode #1232: Check If It Is a Straight Line
struct Solution;
impl Solution {
fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
let n = coordinates.len();
if n == 2 {
return true;
}
let x1 = coordinates[1][0] - coordinates[0][0];
let y1 = coordinates[1][1] - coordinates[0][1];
for i in 1..n {
let xi = coordinates[i][0] - coordinates[0][0];
let yi = coordinates[i][1] - coordinates[0][1];
if x1 * yi != xi * y1 {
return false;
}
}
true
}
}
#[test]
fn test() {
let coordinates: Vec<Vec<i32>> = vec_vec_i32![[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]];
assert_eq!(Solution::check_straight_line(coordinates), true);
let coordinates: Vec<Vec<i32>> = vec_vec_i32![[1, 1], [2, 2], [3, 4], [4, 5], [5, 6], [7, 7]];
assert_eq!(Solution::check_straight_line(coordinates), false);
}
// Accepted solution for LeetCode #1232: Check If It Is a Straight Line
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1232: Check If It Is a Straight Line
// class Solution {
// public boolean checkStraightLine(int[][] coordinates) {
// int x1 = coordinates[0][0], y1 = coordinates[0][1];
// int x2 = coordinates[1][0], y2 = coordinates[1][1];
// for (int i = 2; i < coordinates.length; ++i) {
// int x = coordinates[i][0], y = coordinates[i][1];
// if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
// return false;
// }
// }
// return true;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.