</>CodexBash
Learn
Arrays and hashingBeginner8 min

Where your data actually lives

Memory is one long numbered street. Almost every cost in this course comes from that one fact, so it is worth ten minutes before anything else.

Read these first

1 of 1 still to read
  1. Numbers: the digits inside them
An array in memorystep 1/5
1000
4
[0]
1004
9
[1]
1008
2
[2]
1012
7
[3]
1016
5
[4]

 

An array is a promise: these values sit in houses next to each other, starting at one address.

Before any data structure makes sense, it helps to know what the computer is actually doing when you write nums[3].

Memory is a numbered street#

Think of your computer's memory as one very long street of identical houses. Every house holds one small value, and every house has a number. That number is called an address.

The street has one useful property: house 5000 can be reached as fast as house 5. You do not walk down the street counting. The computer jumps straight to any address in the same amount of time, the way you can open a book at page 400 without turning 399 pages.

That single property is where almost every cost in this course comes from.

What an array is#

An array is a promise: these values sit in houses next to each other, starting here.

If an array of numbers starts at address 1000, and each number takes 4 houses, then:

you writethe computer works outaddress
nums[0]1000 + 0 x 41000
nums[1]1000 + 1 x 41004
nums[2]1000 + 2 x 41008
nums[7]1000 + 7 x 41028

One multiply and one add, whatever the index. That is why reading nums[7] costs the same as nums[0], and why we call it constant time, written O(1).

It is also why array indexes start at 0. nums[0] means "no houses along from the start", not "the first one".

The price of that promise#

Sitting next to each other is what makes indexing instant. It is also what makes two other things slow.

Inserting in the middle. To put a value at position 2 of [a, b, c, d, e], everything from position 2 onward has to shuffle one house along to make room. Five values, four moves. A million values, and you are moving most of a million houses. That is O(n).

Growing. The houses after your array might already belong to something else. When an array runs out of room, the usual answer is to find a bigger empty stretch somewhere else and copy everything across.

Why this matters later#

Every structure in this track is a different answer to one question: what are you willing to give up?

  • An array gives you instant indexing and charges you for inserting.
  • A linked list gives you instant inserting and charges you for indexing.
  • A hash table gives you instant lookup by key and charges you memory, plus giving up any sense of order.

None of them is better. They are trades, and knowing which trade you are making is most of choosing correctly.

The one idea to keep#

Next to each other means fast to reach and expensive to rearrange. Everything else follows from that.

Common mistakes#

  • Thinking nums[1000000] is slower than nums[0]. It is not.
  • Thinking inserting at the front of an array is cheap. It is the most expensive place.
  • Forgetting that "the array grew" may have quietly copied every value somewhere else.

Practise it

The same idea, in the order it is worth meeting it.

  1. Not startedRunning sumEasy
  2. Not startedMove the zeroes to the endEasy
  3. Not startedSqueeze out the duplicatesEasy
  4. Not startedTurn the square a quarter turnMedium
Create a free account to track your progress