AlgoViz

Static arrays

easy 15 min

A static array is a fixed-size block of same-type slots stored side by side in memory.

See it run

Watch the address arithmetic behind O(1) access, then see why inserting and deleting in the middle means shifting every element after the gap.

Operations

Create and fill

Reserve one block of 5 slots, then write a value into each slot by index.

Step 1 of 7

Reserve 5 slots in one block

  1. —0
  2. —1
  3. —2
  4. —3
  5. —4
size = 5base = 1000

[None] * 5 reserves 5 slots side by side in memory. The size is fixed from now on. The slots exist but hold nothing useful yet.

Step 1: Reserve 5 slots in one block

arr = [None] * 5    # reserve 5 slots in one blockarr[0] = 10         # address 1000 + 0*4arr[1] = 20         # address 1000 + 1*4arr[2] = 30         # address 1000 + 2*4arr[3] = 40         # address 1000 + 3*4arr[4] = 50         # address 1000 + 4*4print(len(arr))     # 5, and it stays 5
Time
O(n)
Space
O(n)
each write
O(1)
memory
5 × 4 = 20 bytes

The size is decided once, when the block is reserved. Everything after that is reading and writing slots.

In the real world

The same static arrays doing real jobs. Pick one and step through it.

Five exam grades in a fixed array: the class size is known before the exam, so the size never changes.

Step 1 of 4

One slot per student

  1. 850
  2. 921
  3. 782
  4. 953
  5. 884
students = 5

A class of 5 sat the exam. Student number k lives at index k. The size is known up front, which is exactly when a static array shines.

Step 1: One slot per student

grades = [85, 92, 78, 95, 88]print(grades[3])               # 95, O(1)grades[2] = 81                 # regrade, O(1)print(sum(grades) / len(grades))   # 88.2, O(n)

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Off-by-one loop bound

    critical

    An array of length n has indexes 0 to n - 1. Looping up to and including n reads one slot past the end and crashes on the last pass.

    Wrong
    for i in range(len(arr) + 1):   # goes to n    print(arr[i])                # IndexError at i = n
    Right
    for i in range(len(arr)):       # 0 .. n - 1    print(arr[i])
  • Using arr[n] as the last element

    critical

    If the size is 5, the valid indexes are 0, 1, 2, 3, 4. Index 5 is out of bounds; in C it silently reads someone else's memory.

    Wrong
    arr = [0] * 5arr[5] = 10          # IndexError
    Right
    arr = [0] * 5arr[4] = 10          # last valid index, n - 1arr[-1] = 10         # Python shorthand for the same slot
  • Treating insert at the front as O(1)

    major

    Inserting at index 0 shifts every element, so doing it in a loop costs O(n²). Append and reverse once, or use a deque (or a linked list) when you really need fast front inserts.

    Wrong
    out = []for x in data:    out.insert(0, x)     # O(n) each, O(n^2) total
    Right
    out = []for x in data:    out.append(x)        # O(1) eachout.reverse()            # O(n) once
  • Deleting while looping forward

    major

    When you delete arri, arr[i + 1] slides into position i and the loop skips it; the loop also runs past the new end. Loop backwards, or build a new list.

    Wrong
    for i in range(len(arr)):    if arr[i] == target:        del arr[i]       # skips the next item, then IndexError
    Right
    for i in range(len(arr) - 1, -1, -1):    if arr[i] == target:        del arr[i]       # earlier indexes are unaffected# or: arr = [x for x in arr if x != target]
  • Reading arr[0] of an empty array

    major

    Starting a max or min with arr[0] assumes there is a first element. An empty input has none.

    Wrong
    def biggest(arr):    best = arr[0]        # IndexError on []    for x in arr[1:]:        best = max(best, x)    return best
    Right
    def biggest(arr):    if not arr:        return None    best = arr[0]    for x in arr[1:]:        best = max(best, x)    return best
  • Thinking assignment copies the array

    major

    b = a copies the reference, not the data. Both names point at the same block, so writing through one changes the other.

    Wrong
    a = [1, 2, 3]b = ab[0] = 99            # a is now [99, 2, 3] too
    Right
    a = [1, 2, 3]b = a.copy()         # or a[:] or list(a)b[0] = 99            # a is still [1, 2, 3]

Interview tips

  • Perfect for arrays

    • You need fast access by index and the size is known or fixed.
    • Lookup tables and precomputed results: O(1) retrieval.
    • Fixed-size buffers, matrices and grids (2D arrays).
    • Two-pointer and sliding-window problems over contiguous data.
  • Avoid arrays when

    • You insert or delete at the front often: use a linked list or deque.
    • The size is unknown or keeps changing: use a dynamic array.
    • You look things up by value: use a hash set or hash map.
    • You must keep sorted order under many inserts: use a BST or heap.
  • Patterns that solve most array problems

    • Two pointers: start at both ends and move inward.
    • Sliding window: grow and shrink a range as you scan.
    • Prefix sums: precompute running totals for O(1) range sums.
    • Sort first, then two pointers; or a hash map of complements for O(1) lookups.
  • In the interview room

    • State time and space for every solution, before and after optimising.
    • Ask about constraints: size, value range, sorted or not, must it be in place.
    • Cover edge cases first: empty, one element, duplicates, negatives.
    • Start with brute force, then optimise; double-check loop bounds for off-by-one.