Arrays and indexing
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Explain why array access by index is O(1).
- Describe the cost of searching, inserting and deleting in an array.
- Choose between in-place mutation and building a new array.
Overview
#A block of slots
An array is a contiguous block of slots, each holding one value. Because the slots are the same size and next to each other, the address of slot i is start + i * size. That arithmetic is why reading values[i] is O(1): no scanning is needed.
The cost of each operation
| Operation | Cost | Why |
|---|---|---|
| read values[i] | O(1) | address arithmetic |
| search for a value | O(n) | must scan in the worst case |
| insert at the end | O(1) amortised | append if capacity allows |
| insert in the middle | O(n) | shift later elements right |
| delete from the middle | O(n) | shift later elements left |
In place or a new array
Mutating an array in place keeps space at O(1) but changes the caller's data. Building a new array is clearer and avoids surprising the caller, at O(n) extra space. Interview answers should state which one you chose and why.
Two pointers over an array
Many array problems become simple once you keep two indices. For example, reversing in place:
let left = 0, right = values.length - 1;
while (left < right) {
[values[left], values[right]] = [values[right], values[left]];
left++;
right--;
}
Each element is touched once, so the reversal is O(n) time and O(1) space.
Practice next
- Reverse an array in place and prove it uses O(1) extra space.
- Explain why appending to a dynamic array is amortised O(1).
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this return?
starter.javascriptconst values = [10, 20, 30]; return values[values.length];Reveal the reference answer
reference.javascriptvalues.length is 3, and valid indices are 0, 1, 2. values[3] is undefined.Authored expected output: undefined
The last valid index is length - 1. Reading past it is undefined, not an error.
Exercise 2Implement
Implement reverseInPlace(values) that reverses the array using O(1) extra space.
starter.javascriptfunction reverseInPlace(values) { // your code here }Reveal the reference answer
reference.javascriptfunction reverseInPlace(values) { let left = 0; let right = values.length - 1; while (left < right) { const tmp = values[left]; values[left] = values[right]; values[right] = tmp; left++; right--; } return values; }Authored expected output: [3, 2, 1] for input [1, 2, 3]
Swapping from both ends inward touches each element once and uses only two index variables.
Exercise 3Explain
Explain why inserting an element at the front of an array is O(n) while reading values[0] is O(1).
Reveal the reference answer
reference.textReading index 0 needs only address arithmetic, so it is constant time. Inserting at the front must shift every existing element one slot to the right to make room, which touches all n elements, so it is O(n).Authored expected output: Front insertion shifts all n elements; index read needs only arithmetic.
Contiguity gives cheap random access but expensive middle insertion.
Common misconceptions
#- Indexing starts at 1. In JavaScript and most languages it starts at 0, so the last index is length - 1.
- Reading values[values.length] returns the last element. It returns undefined.
- Inserting at the front is cheap. It shifts every other element, so it is O(n).
Follow-up practice
#- dsa-from-zero-linear-search
- dsa-from-zero-strings-and-hashing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.