Loading…
Loading…
Charting your lesson…
Opening your lesson…
Array Fundamentals and Traversal
Lesson 1 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
The array is the first data structure every interviewer assumes you know cold, and it silently underpins strings, matrices, heaps and hash buckets. Understanding why an array is fast is the difference between guessing complexities and stating them with confidence.
Core idea: an array stores elements in a single contiguous block of memory. Because every element is the same size, the address of index i is just base + i × size. That one multiplication is why random access is O(1) — the machine never "walks" to the element, it computes where it lives.
Step 2/ 61 min
A zero-indexed array a of length n occupies slots a[0] … a[n-1]. Reading or writing a[i] is O(1). A full traversal touches each element once, so it is O(n) time, O(1) extra space.
for i in 0..n-1:
process(a[i])
The contiguity that makes access cheap makes structural edits expensive. Inserting at the front forces every later element to shift right — O(n). Deleting from the middle shifts everything after it left — O(n). Only appending at the end of a dynamic array is cheap.
Dynamic arrays (Java ArrayList, C++ vector, Python list) grow by doubling capacity when full. Copying on a resize is O(n), but because it happens rarely, appends are amortised O(1). Doubling gives a geometric series: n appends cost about 2n copies total.
Contiguous storage means a traversal reads memory the CPU has already prefetched into cache. This is why array traversal often beats a linked list of the same length in practice even when both are O(n): the constant factor is smaller.
Step 3/ 61 min
What is the amortised cost of appending N elements to an empty dynamic array that doubles?
Resizes happen at sizes 1, 2, 4, …, N, copying 1 + 2 + 4 + … + N ≈ 2N elements total. Add N cheap writes: total ≈ 3N operations over N appends → O(1) amortised per append. The trap answer "O(n) because resizing copies" ignores how rarely it happens.
Step 4/ 61 min
Reach for an array when you need indexed access, a fixed or append-only collection, or the input is already a list. Nearly every two-pointer, sliding-window, prefix-sum and sorting technique operates on arrays. Avoid arrays when you need frequent front/middle insertions — a linked list or deque fits better.
Step 5/ 61 min
len(a) in a hot loop is a common micro-slip.a[n] (out of bounds) and empty arrays (n == 0).Assuming insertion into an array is O(1) "because you just set a value". Setting a[i] is O(1), but making room at position i shifts up to n elements — that is O(n). Only end-appends on a dynamic array are amortised O(1).
a[i] is O(1) via address arithmetic.Step 6/ 6· Worked examples1 min
Problem
Trace the output of this code that sums the elements at even indices.
arr = [3, 1, 4, 1, 5]
total = 0
for i from 0 to length(arr)-1:
if i % 2 == 0:
total = total + arr[i]
print(total)
Try it on paper first.
Recall
Keep this
Usage: reach for arrays when random access dominates; avoid mid-array inserts.
Almost every coding-interview problem starts life as an array problem, and the first thing interviewers judge is whether you understand why arrays are fast. An array stores elements in one contiguous block of memory, so the address of index i is base + i × elementSize — a single arithmetic step. That is what gives you O(1) random access, and it is the property every array trick ultimately exploits.
Contiguity buys constant-time indexing but costs you at the edges: inserting or deleting in the middle forces every later element to shift. A dynamic array (Java ArrayList, C++ vector, Python list) hides resizing behind amortized O(1) append — it doubles capacity, so the occasional O(n) copy averages out.
| Operation | Time | Note |
|---|---|---|
| Access by index | O(1) | base + i × size |
| Search (unsorted) | O(n) | linear scan |
| Search (sorted) | O(log n) | binary search |
| Insert/delete at end | O(1) amortized | dynamic array |
| Insert/delete at middle | O(n) | shift elements |
| Space | O(n) | contiguous block |
Warm-up array questions test clean single-pass traversal, correct boundary handling, and in-place updates without an extra array. Expect "traverse and transform", "find max/second-max", or "count elements meeting a condition" as the opener before a harder follow-up.
Arrays = contiguous memory → O(1) access, O(n) middle insert/delete, amortized O(1) append. Master clean index-based traversal and boundary handling; they underpin the two-pointer, sliding-window, and prefix-sum patterns that follow.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.