The Contiguous Advantage
If you want to store a single number, like a score of 10, you can just put it in a simple variable. But what if you have 1,000 scores to keep track of? Creating 1,000 separate variables would be a nightmare.
This is exactly the problem that Arrays solve. An array is simply a way to group a large list of items together under a single name. Under the hood, the computer finds a single chunk of memory and packs all of your items right next to each other in a sequential line.
Because they are lined up perfectly, we can retrieve any item instantly as long as we know its position (its "index").
A String is essentially the exact same concept, just exclusively designed for text! Instead of a list of numbers, a string is just an array where every item is a single letter or character. If you understand arrays, you already understand strings.
A Matrix (2D Array) is just an array where every item is another array! Instead of a 1D line of items, it forms a geometric grid with rows and columns. Matrices are incredibly common in grid-based problems (like mazes, game boards, or image processing), where you traverse or access elements using two coordinates: matrix[row][col].
Instant Memory Access
Accessing arr[i] jumps directly to the exact memory address in an instant O(1) operation.
This is the mechanism behind O(1): the CPU computes the exact memory address with one multiply and one add ā no searching required.
Insert & Delete: The Shifting Problem
Insert & Delete: The Shifting Problem
Accessing is instant, but inserting or removing at the front means every other element has to move to keep the array contiguous.
Press a button to see what actually happens in memory.
Speed vs. Memory
Time Metrics
*Why append is usually free: dynamic arrays over-allocate and double their capacity when full, so most pushes just write into already-reserved space. The occasional resize-and-copy is O(N), but averaged ("amortized") over many pushes, it washes out to O(1).
Space Footprint
Mastering the Basics
Common Mistakes
Off-by-One Errors
Looping with i <= arr.length instead of i < arr.length is the single most common array bug ā the valid indices are always 0 to length - 1.
Out-of-Bounds Access
In JS, arr[100] on a 5-element array silently returns undefined instead of erroring. In C++, it's undefined behavior ā it might read garbage memory instead of crashing. Always validate an index before trusting it.
String Concatenation in a Loop
In Java and Python, strings are immutable ā s += ch inside a loop allocates a brand-new string every iteration, copying everything so far. Over N iterations that's O(N²). Use a StringBuilder (Java) or build a list and "".join(...) (Python) instead.
Sparse Arrays in JS
arr[10] = 5 on a 3-element JS array doesn't error ā it creates a sparse array with holes, and arr.length silently becomes 11. Methods like forEach skip the holes, which can make bugs invisible until you least expect it.
Patterns Built On This Foundation
Sorted array, looking for a pair or triplet? Converge from both ends.
Contiguous subarray/substring with a size or sum constraint? Slide instead of re-scanning.
Repeated range-sum queries? Precompute once, answer in O(1).
Sorted data, or a monotonic yes/no answer? Halve the search space each step.
"Constant access for the known, linear search for the lost."