Static arrays
easy 15 minA static array is a fixed-size block of same-type slots stored side by side in memory.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the time complexity of accessing the element at index i?
What does contiguous memory mean?
Can you change the size of a static array after it is created?
What happens when you access an invalid index?
An int array starts at address 2000 and each int takes 4 bytes. What is the address of arr[6]?
An array holds 8 elements. How many elements must shift to insert a new value at index 3?
An array holds 8 elements. How many elements must shift to delete the element at index 3?
Why does an insert loop shift elements starting from the back of the array?
A 2D array of 4-byte ints has 5 columns, is stored in row-major order and starts at address 0. What is the address of cell (2, 3)?
You must add 10,000 items, each to the front of a static-array-backed list. What is the total cost?
Pick an answer to see why.
Not quite. The address is calculated directly as base + i × size. No traversal is needed, so access is O(1).
Not quite. Contiguous means the elements sit in consecutive memory locations, which is what makes the O(1) address calculation possible.
Not quite. A static array's size is fixed at creation. To get more room you create a new, bigger array and copy the elements across.
Not quite. Python raises IndexError and Java throws ArrayIndexOutOfBoundsException. C and C++ do not check, so it is undefined behaviour.
Not quite. addr(6) = 2000 + 6 × 4 = 2024.
Not quite. Insert at i shifts the n - i elements from i onwards: 8 - 3 = 5 (indexes 3 to 7 each move one slot right).
Not quite. Delete at i shifts the n - 1 - i elements after it: 8 - 1 - 3 = 4 (indexes 4 to 7 each move one slot left).
Not quite. Copying data[i] into data[i + 1] first destroys the old data[i + 1] before it has moved. Going back to front always copies into a slot that has already been saved.
Not quite. Flat index = 2 × 5 + 3 = 13, and 13 × 4 bytes = 52.
Not quite. Each front insert shifts every existing element, so the total is 0 + 1 + ... + (n - 1) = O(n²). Append and reverse once, or use a deque or linked list.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What is a static array?
A fixed-size, contiguous block of memory holding elements of the same type. The size is set at creation and never changes.
Say your answer, then reveal it.
What is the address formula for arr[i]?
addr(i) = base + i × s, where base is the start of the block and s is the size of one element in bytes.
Say your answer, then reveal it.
With base 1000 and 4-byte ints, where is arr[3]?
1000 + 3 × 4 = 1012.
Say your answer, then reveal it.
Why is access by index O(1)?
The address is computed with one multiply and one add, so no other element is touched, whatever i or n is.
Say your answer, then reveal it.
What are the valid indexes of an array of size n?
0 to n - 1. Index n is one past the end and out of bounds.
Say your answer, then reveal it.
How many elements shift when you insert at index i? When you delete at index i?
Insert shifts n - i elements right; delete shifts n - 1 - i elements left. Both are O(n - i), so O(n) at the front and O(1) at the end.
Say your answer, then reveal it.
In which direction do you loop when shifting for an insert, and for a delete?
Insert: from the back towards i, so nothing is overwritten. Delete: from the gap towards the end.
Say your answer, then reveal it.
What is the cost of search by value?
O(n) with linear search on unsorted data. O(log n) with binary search, but only if the array is sorted.
Say your answer, then reveal it.
How do you make delete O(1) when order does not matter?
Copy the last element into the gap and shrink the length by one.
Say your answer, then reveal it.
Where is cell (r, c) in a row-major 2D array with C columns?
base + (r × C + c) × s. Still O(1).
Say your answer, then reveal it.
Why are arrays cache-friendly?
Their elements are contiguous, so one cache line brings in several neighbours and the CPU can prefetch the next ones during a scan.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.