Every programmer makes these mistakes. The difference between a beginner and an experienced engineer is mostly that the experienced one expects them, and checks for them before they cause trouble. Here are the ones that bite most often with arrays.
An array of length n has indexes 0 to n - 1. Writing <= in the loop condition visits index n, which doesn't exist. What happens next depends on the language — and none of the outcomes are good:
undefined, and the bug surfaces later as NaN or a strange comparison.IndexError / ArrayIndexOutOfBoundsException — loud, but at least honest.Habit: when you write a loop bound or an index like i + 1, ask "what happens on the last element?" If you read nums[i + 1], the loop must stop at i < n - 1.
nums[0] on an empty array is the same out-of-bounds read. Code like let max = nums[0] assumes at least one element. Either check first, or confirm the problem guarantees n ≥ 1.
function maxOrNull(nums) { if (nums.length === 0) return null; // handle the edge case first let max = nums[0]; for (const x of nums) max = Math.max(max, x); return max;}In JavaScript, Python and Java, an array variable holds a reference to the array, not a copy of it. So b = a gives you a second name for the same array:
const a = [1, 2, 3];const b = a; // same array!b[0] = 99;console.log(a); // [99, 2, 3] const copy = [...a]; // a real copy (also: a.slice())C++ is the odd one out: assigning a vector copies it, and you ask for sharing explicitly with &. The same rule catches people in function calls — passing a vector by value copies it, which is slow for big arrays.
Removing elements during a forward loop shifts the rest of the array left, so the loop skips the element that slid into the current position:
const nums = [2, 4, 4, 5];for (let i = 0; i < nums.length; i++) { if (nums[i] === 4) nums.splice(i, 1); // ✗ the second 4 slides into i and is skipped}console.log(nums); // [2, 4, 5] const kept = nums.filter((x) => x !== 4); // ✓ build a new array insteadFixes: build a new array, loop backwards when deleting, or use the read/write pointer pattern from Move zeroes.
In Java and C++, an int holds values up to about 2.1 billion. Summing 100,000 values of up to 100,000 each gives 10 billion — silently wrong in an int. Use long (Java) or long long (C++) for sums and products. The classic middle-index calculation has the same trap:
int mid = (low + high) / 2; // can overflow when low + high > 2^31 - 1
int mid = low + (high - low) / 2; // safe[10, 9, 1].sort() gives [1, 10, 9]. Always pass a comparator for numbers: sort((a, b) => a - b).[[0] * cols] * rows creates `rows` references to the same** row — changing one cell changes a whole column. Use [[0] * cols for _ in range(rows)].Arrays.equals in Java, or == on lists in Python (which does compare contents).for (int i = 0; i < v.size() - 1; i++) misbehaves when v is empty, because 0 - 1 wraps to a huge number.< or <=? Any i + 1 or i - 1 reads?You'll find these checks repeated as Edge cases to test at the end of every problem in this course.