Skip to content
Data Structures & Algorithmsbeginner · 45 min

Arrays & Strings

Contiguous, index-addressable collections — the most common interview substrate. Master in-place manipulation, the two-pointer and sliding-window patterns, and the cost of resizing.

Memory model & complexity

Arrays store elements contiguously, so random access is O(1) but inserting/removing in the middle is O(n) because elements shift. Dynamic arrays (JS arrays, Python lists) amortize push to O(1) by doubling capacity. Strings are immutable in most languages, so building one in a loop with concatenation is O(n²) — use an array/builder and join once.

Two pointers & sliding window

Two pointers (one from each end, or fast/slow) solve pair-sum, palindrome, and partition problems in O(n) with O(1) space. The sliding window maintains a contiguous range and expands/contracts it — ideal for 'longest/shortest substring with property X' problems. Recognising these patterns is what most array questions test.

Common pitfalls

Off-by-one errors at boundaries, mutating an array while iterating it, and assuming sorted input. Clarify whether input is sorted, whether duplicates exist, and whether you may modify the input in place.

Resources

Practice & test yourself

Take the quiz →