Lesson · Arrays
Array classics: the patterns every interview reuses
13 min readCommon Problems
Max and min, deduplication, rotation, pair summing, frequency counting: the small array algorithms that recur, with the technique each one teaches.
Single-pass scans: max, min, average
The max-finding loop is the first algorithm with a state variable: initialise to the first element (not zero, which breaks all-negative input), then scan comparing. The subtle part is the initialisation choice, and it generalises: whenever a reduction starts from an accumulated value, think about the empty input and the adversarial input.
The average scan has its own classic pitfall: integer division. sum / count with both ints truncates; either cast ((double) sum / count) or compute as a stream (Arrays.stream(a).average()). This bug is so common it deserves its own mental category next to overflow.
Two pointers: pairs in sorted arrays
Finding a pair summing to a target is the flagship two-pointer exercise: sort first, then walk left and right toward each other, moving left when the sum is too small and right when too large. Linear after the sort, and the movement logic is a small proof: the current pair is eliminated as too small, so no pair including left remains.
Reversal and palindrome checks are the same skeleton. while (left < right) with left++ and right-- is the shape; the boundary left < right versus left <= right decides whether the middle element of odd-length arrays is visited, which for swaps is irrelevant and for checks usually is too. When in doubt, trace n equals 5.
Frequency counting and deduplication
Counting occurrences is a scan with an int[] counter, or a HashMap when the values are not small integers. For ASCII text it is counts[ch]++ with an int[128]; for arbitrary ints it is map.merge(value, 1, Integer::sum). The counter pattern answers in one pass what naive nested loops answer in n squared.
Deduplication splits by tool: with a sorted array it is a compaction scan (write-ahead pointer for unique elements), with unsorted data it is a HashSet and a second pass or a stream distinct. Interviewers accept either when the complexity story is right, linear after sorting versus linear with extra memory.
Rotation and subarray sums
Rotation by k: the elegant solution is three reversals (reverse all, reverse first k, reverse the rest). The reusable lesson is that array problems often decompose into reversals and transposes, which is why the reverse skeleton matters more than any single rotation answer.
Largest subarray sum is the classic finale: Kadane's algorithm keeps a running sum, restarting at the current element whenever the running sum goes negative, tracking the best seen. One pass, one state variable pair, and the type of insight (local restarts versus global best) that later reappears in dynamic programming.
Initialise reductions from the data, not from zero
max starting at 0 returns 0 for an array of negative numbers; also a min starting at 0 returns 0. Start from the first element, or from the appropriate extreme (Integer.MIN_VALUE for a non-empty max). The first element is the safer habit: it needs no assumption about the data.
Interview angles
How do you find a pair summing to a target in a sorted array?+
Two pointers outer ends inward: too-small moves left rightward, too-large moves right leftward, equal returns the pair. Linear. Starting from a sort makes the total O(n log n), and the elimination argument proves no pair is missed.
Why is the running-sum Kadane algorithm correct?+
Any best subarray ending at i either extends the best one ending at i - 1 or starts fresh at i, which is exactly the restart-when-negative rule. Tracking the best over all ends gives the global maximum in one pass.
How do you deduplicate while preserving order?+
HashSet plus a results list scanning once: add to results only when the set did not contain the value. Linear with linear extra space. Sorting first permits a linear compaction scan instead, at the cost of the array's original order.