Compile Ready
Module 1 · Arrays Fundamentals

Common Array Interview Patterns

Most array problems become manageable once you identify whether order, contiguity, frequency, or index range is the real structure.

8 min readConcept
ArrayPatternsInterview Framework

Pattern Recognition Beats Memorization

Array questions often look different on the surface: pairs, subarrays, missing numbers, duplicates, intervals, products, or matrix updates. Underneath, the same few patterns repeat. The interview skill is to name the pattern from the constraint signal before writing code.

A good signal connects the problem statement to a cost you want to avoid. If brute force checks all pairs, two pointers or hashing may remove one loop. If brute force recomputes every range, prefix sums may summarize the repeated work. If extra space is restricted and values lie in a narrow range, the array itself may be usable as storage.

Order and Boundary Patterns

Two pointers is the signal when the array is sorted, can be sorted without losing meaning, or asks for a pair, partition, reversal, or merge. One pointer often starts left, the other right, and each comparison proves which side can move.

Sliding window is the signal when the answer is a contiguous subarray or substring and the window can grow and shrink while maintaining a constraint. It is strongest for positive numbers, counts, distinctness, or at most k conditions where moving a boundary has predictable effects.

Summaries and Marks

Prefix sums are the signal for repeated range sums, subarray sum targets, balance between counts, or anything where subtracting two cumulative totals gives a range answer. The question often says many queries or asks for subarray totals.

In-place marking is the signal when values are in a tight index range such as 1..n and the problem asks for missing, duplicate, or first positive information with O(1) extra space. Negating values, swapping into home positions, or using sign bits can turn the array into a visited structure.

Sorting and Index-as-Hash

Sorting then scanning is the signal when relative order in the original input is not important and adjacency after sorting exposes the answer. Duplicates, intervals, closest pairs, and greedy grouping often become simple after sorting.

Index-as-hash is the signal when values naturally map to array indices, such as lowercase letters, ASCII characters, small integers, or numbers from 0 to n - 1. A plain array count can beat a HashMap in speed and memory when the value range is bounded.

How to Explain the Choice

In interviews, do not just announce a technique. Say what the brute force repeats and what the pattern remembers. For two pointers, explain the elimination rule. For sliding window, explain the invariant. For prefix sums, explain which range quantity becomes subtraction. For in-place marking, explain why values can safely point back into indices.

This framing makes the solution feel derived rather than memorized, and it helps you adapt when the interviewer changes constraints.

Key Takeaways

  • Two pointers usually needs sorted order, pair logic, partitioning, reversal, or merging.
  • Sliding window targets contiguous ranges with a maintainable constraint.
  • Prefix sums replace repeated range work with cumulative summaries.
  • In-place marking, sorting-then-scanning, and index-as-hash exploit value range or order structure.