An array stores elements in contiguous memory (in languages like C). Index i is found in constant time: base address + i × element size. JavaScript arrays are objects with extra behaviour, but interview problems still treat them as lists with O(1) index access.
Costs you should be able to say out loud
- Access by index: O(1)
- Search unsorted: O(n)
- Insert/delete in the middle: O(n) because later elements shift
- Append at the end: amortised O(1) if the language grows capacity (JS
push)
Patterns
Two pointers. Sorted pair-sum: left at start, right at end; move the side that makes the sum closer to the target. Prefix sums. pref[i] = sum of a[0..i]; subarray i..j is pref[j]-pref[i-1] in O(1) after O(n) preprocess.
Practice on this site (JavaScript judge): Two Sum, Kadane, Move Zeroes.
Related pattern lessons: Two pointers, Prefix sum & Kadane.