Arrays

Arrays are the most fundamental data structure in programming. They store elements in contiguous memory locations, allowing fast access to any element using its index. Even when AI tools can generate array-manipulation code in seconds, understanding how arrays work is essential — it lets you spot bugs, reason about performance costs, and make confident design decisions.

What Is an Array?#

An array is a collection of elements stored at contiguous memory locations. Each element can be accessed directly using its index (its position in the sequence).

Think of an array like a row of numbered mailboxes: each mailbox is in a fixed, predictable slot, and you can reach any one of them directly without searching through the others first.

Rendering diagram...

Indices start at 0 in most languages (C, C++, Java, Python, JavaScript). The first element is at index 0, the second at index 1, and so on.

Key Characteristics#

PropertyDescription
Fixed SizeTraditional arrays have a fixed size determined at creation
Contiguous MemoryElements are stored next to each other in memory
Index-BasedEach element has a numeric index starting from 0
HomogeneousAll elements are typically of the same type
Random AccessAny element can be accessed directly in O(1) time

Memory Layout#

Understanding how arrays are stored in memory helps explain their performance characteristics. When an array is created, the system reserves a block of memory large enough to hold all its elements side by side.

Rendering diagram...

In the formula address = base + (index × size):

  • base is the memory address of the first element (e.g., 0x100)
  • index is the position you want to access (e.g., 2)
  • size is the number of bytes per element (e.g., 4 bytes for a 32-bit integer)

This direct address calculation is why array access is O(1) — the CPU computes the exact memory address in a single arithmetic step and jumps straight to it, regardless of how large the array is.

Basic Operations#

OperationTime ComplexityDescription
AccessO(1)Get element at index
UpdateO(1)Modify element at index
Insert at EndO(1)*Add element to the end
Insert at IndexO(n)Shift elements to make room
Delete at EndO(1)Remove last element
Delete at IndexO(n)Shift elements to fill gap
SearchO(n)Find element (unsorted array)

* Amortized O(1) for dynamic arrays

The * footnote refers to amortized cost: most appends are instant O(1), but occasionally the array must resize itself (allocating new memory and copying all elements). Averaged over many operations, this rare expensive step is negligible — each append costs O(1) on average.

Array Operations

Common operations on arrays with examples

Basic array operations in Python

Insertion and Deletion#

When you insert or delete an element in the middle of an array, elements to the right must shift to maintain contiguity. This shifting is what makes these operations O(n) — in the worst case (inserting at index 0), every element in the array must move.

Rendering diagram...

Deletion works the same way in reverse: the element is removed, and everything to its right shifts one position left to close the gap.

Static vs Dynamic Arrays#

TypeSizeMemoryLanguages
Static ArrayFixed at creationStack or heapC, C++, Java (primitive)
Dynamic ArrayGrows automaticallyHeap with resizingPython list, JavaScript array, ArrayList

Dynamic Array Resizing#

Dynamic arrays automatically grow when they run out of space. When full, they allocate a new, larger array (typically 2×) and copy all existing elements over before adding the new one.

Rendering diagram...

The resize is expensive (O(n) to copy all elements), but it happens infrequently. Because the capacity doubles each time, the total copy work across all appends is proportional to n, making each append amortized O(1) on average.

Common Patterns#

Two Pointers#

A technique that uses two indices to traverse the array, typically starting from opposite ends and moving toward each other. By doing this in a single pass, it avoids the need for nested loops and keeps the time complexity at O(n).

Reverse Array In-Place

Use two pointers to swap elements from both ends

Time:O(n)Space:O(1)

Sliding Window#

A technique for efficiently processing contiguous subarrays. Instead of recomputing the result for each subarray from scratch, the window slides forward by adding the incoming element on the right and removing the outgoing element on the left. This keeps the window computation O(1) per step, reducing the overall complexity from O(n×k) to O(n). The window can be a fixed size (as below) or variable.

Rendering diagram...
Rendering diagram...

Maximum Sum Subarray of Size K

Find the maximum sum of any contiguous subarray of size k

Time:O(n)Space:O(1)

When to Use Arrays#

Use CaseWhy Arrays Work Well
Random access neededO(1) access by index
Iterating through all elementsCache-friendly, fast iteration
Stack implementationO(1) push/pop at end
Known, fixed sizeNo resizing overhead
Sorting algorithmsIn-place sorting possible

When to Consider Alternatives#

ScenarioBetter Alternative
Frequent insertions/deletions at beginningLinked List or Deque
Fast lookup by valueHash Set or Hash Map
Maintaining sorted order with insertionsBinary Search Tree or Heap
FIFO operations (queue)Deque or Queue

Reviewing AI-Generated Code#

When AI generates array code, watch for these common issues:

IssueWhat to CheckCommon AI Mistake
Off-by-one errorsLoop bounds, array accessUsing <= instead of < in loops
Empty array handlingEdge case checksNot checking if len(arr) == 0
Index out of boundsArray access patternsAccessing arr[i+1] without bounds check
Mutation vs copyIn-place operationsModifying input when copy was needed
Inefficient operationsMethod complexityUsing list.insert(0, x) in a loop (O(n²))

Spotting AI Array Mistakes

Common patterns where AI-generated array code fails

Proper bounds checking for adjacent element access

Summary#

ConceptKey Takeaway
ArraysContiguous memory, O(1) random access
IndexingZero-based, direct address calculation
Trade-offsFast access, slow insertion/deletion in middle
Dynamic ArraysAuto-resize with amortized O(1) append
Two PointersEfficient technique for many array problems
Sliding WindowProcess subarrays in O(n) time

Arrays are the building blocks for many other data structures and algorithms. Master their operations and patterns before moving on to more complex structures!