Array Fundamentals
An array is the most fundamental data structure in computer science โ a contiguous block of memory that stores elements sequentially, enabling instant access to any element by its index. Every other data structure either builds on arrays or exists to compensate for their limitations.
What is Array?
An array stores elements in a single contiguous block of memory. Every element occupies the same fixed number of bytes โ no gaps, no pointers in between. Because the start address and element size are both known, the CPU can jump to any element in one step using address = base + (index ร element_size). This is why array access is O(1) โ it doesn't matter if the array has 10 elements or 10 million.
Arrays come in two flavours:
Fixed Size
Size is set at creation and cannot change. Memory is allocated once on the stack or heap.
- Fast โ no resize overhead
- Predictable memory footprint
- Typical in C, Java
int[], C#int[]
Auto-Resize
Grows automatically when full by allocating a larger backing array and copying elements over.
- Append is O(1) amortized
- May over-allocate 1.5รโ2ร capacity
- Python
list, JavaArrayList, JSArray
Core Operations
0 to length - 1. Accessing arr[length] is out of bounds and causes a runtime crash. Always use i < arr.length (not i <= arr.length) in loop conditions.
Diagrams & Memory Layout
Java Quick Reference
The most common array operations using Java's built-in ArrayList โ the dynamic array you'll use in real code and interviews.
Time & Space Complexity
| Operation | Best Case | Average Case | Worst Case | Space |
|---|---|---|---|---|
| Access / Update by index | O(1) | O(1) | O(1) | O(1) |
| Append (dynamic array) | O(1) | O(1) amortized | O(n) on resize | O(1) |
| Insert / Delete at index | O(1) at end | O(n) | O(n) | O(1) |
| Linear Search | O(1) | O(n) | O(n) | O(1) |
An array of n elements uses exactly O(n) space for the data itself. There is zero per-element overhead โ no pointers, no metadata per node. Dynamic arrays over-allocate by a constant factor (typically 1.5โ2ร) to amortize resize cost, so actual memory may be up to twice the logical size. Auxiliary space for all operations is O(1) since shifts happen in-place.
When to Use Array
Use This When
- Fast reads by position โ dominant operation is index lookup; O(1) access beats every linked structure.
- Data size is known or stable โ no frequent mid-sequence inserts; cache-friendly layout wins.
- Building another structure โ stacks, queues, heaps, and hash tables all use arrays as their backing store.
Avoid When
- Frequent mid-list inserts/deletes โ every operation shifts O(n) elements; use a linked list or deque instead.
- Size is highly unpredictable โ large add/remove bursts cause repeated resize overhead; a linked list never wastes capacity.
| Structure | Access | Insert | Delete | Best For |
|---|---|---|---|---|
| Array โ this one | O(1) | O(n) | O(n) | Random access, cache performance |
| Linked List | O(n) | O(1) w/ pointer | O(1) w/ pointer | Frequent mid insertions/deletions |
| Hash Map | O(1) avg | O(1) avg | O(1) avg | Key-value lookup, no ordering needed |
LeetCode Problems
Array problems in interviews almost always test one of three patterns: two pointers (converging from both ends), sliding window (a moving subarray of fixed or variable size), or prefix sums (precomputed running totals for O(1) range queries). Recognising which pattern applies is more valuable than memorising solutions.
- Easy Two Sum โ Use a HashMap to store each element's index as you scan โ transforms O(nยฒ) brute force into a single O(n) pass.
- Easy Best Time to Buy and Sell Stock โ Track the minimum price seen so far and the maximum profit at each index โ classic one-pass O(n) greedy.
- Medium Maximum Subarray โ Kadane's algorithm: maintain a running sum and reset to 0 when it goes negative โ local decisions yield a global optimum.
- Medium Product of Array Except Self โ Build a left-products prefix array and a right-products suffix array; multiply them โ achieves O(n) with no division.
- Hard Trapping Rain Water โ Two-pointer approach: water at each position is bounded by the shorter of the tallest bars to its left and right โ scan from both ends.