Home / DSA / Lesson 1

Arrays — contiguous storage

Beginner 10 min read Lesson 1 of 9

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

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.