Arrays & Strings5 min read

Static Arrays

A row of numbered boxes — fast to grab, fixed in size
grab by index: Instant · \(O(1)\)search: Slow · \(O(n)\)resize: Not possible

Imagine you have a row of numbered boxes — like lockers at school. Each locker has a number on the door, and you can store exactly one thing inside.

That's all an array is. A row of boxes, each with a number, all lined up in order. Simple!

The boxes are called elements, and the numbers on them are called indexes (or indices). One important thing: computers start counting from 0, not 1. So the first box is box [0], the second is [1], and so on.

Click any box to explore it

← → arrow keys to step · click elements to interact

Array Basics

arr = [10, 20, 30, 40, 50]
print(arr[0]) # read index 0: 10
arr[2] = 99 # write index 2
print(arr[2]) # 99
for x in arr:
print(x, end=' ')
Output
10
99
10 20 99 40 50 

Why can you find any box instantly?

Here's the magic trick: all the boxes are exactly the same size and sit right next to each other. That means the computer can do simple arithmetic to jump straight to any box.

Want box number 5? The computer thinks: "Box 0 is at the start. Each box is one step wide. So box 5 is exactly 5 steps from the start." One calculation, done. It never has to look at boxes 1, 2, 3, or 4 first.

This is why grabbing any item from an array is instant, no matter how big the array is.

Neighbours come free

When the computer fetches a box, it also quietly grabs the boxes sitting right next to it — because they're so close it's basically free. So if you look at box [0], boxes [1], [2], and [3] are already ready and waiting. This makes looping through an array very fast.

Note: Arrays are like egg cartons — you pick your size (6-pack or 12-pack) when you buy it, and that's what you get. You can't tape extra eggs onto the side later!

The one catch: fixed size

When you create an array, you decide exactly how many boxes you need. You can't add more later — the size is locked in. That's why it's called a static array ("static" means "doesn't change").

If you need 8 boxes, you get 8. If you only use 3 of them, the other 5 still take up space. And if you fill all 8 and need a 9th — tough luck, there's no room!

(The fix for that is a dynamic array, which we'll cover next.)

What searching looks like

Finding a specific value (not a box number) is slower. Say you want to find the number 55. The computer has to open box [0], check if it's 55 — nope. Open box [1] — nope. Keep going until it finds it. In the worst case it checks every single box.

Complexity

🎯 Grab box number XAlways one step, no matter the size
Instant\(O(1)\)
🔍 Find a specific valueMust check boxes one by one
Slow\(O(n)\)
➕ Add to the endIf there's a free box at the end
Instant\(O(1)\)
🔀 Insert in the middleHave to shift other boxes over
Slow\(O(n)\)
🗑️ Delete from the middleHave to shift boxes back
Slow\(O(n)\)

When should you use an array?

  • You know exactly how many items you'll need
  • You'll be grabbing items by their spot number a lot
  • You want to loop through all of them quickly
  • You don't need to change the size later

The one tradeoff

The fixed size is the big constraint. If you don't know how many items you'll end up with, check out the dynamic array — it's like an array that can grow when you need more room.

Challenge

Linear Search

def search(arr, t):
for i, v in enumerate(arr):
if v == t:
return i
return -1
arr = [10, 20, 30, 40, 50]
print(search(arr, 30)) # 2
print(search(arr, 99)) # -1
Output
2
-1

Quick check

An array has 100 boxes. How many steps does it take to grab box [99]?

Continue reading

Dynamic Array
An array that grows automatically when it runs out of space
Two Pointers
Two fingers tracing an array to solve problems in one pass
Prefix Sums
Pre-calculate a running total so you can sum any subarray in instant \(O(1)\) time
ArraysJava
Fixed-size boxes — declare, fill, and loop