ai.page · learn

Arrays

A row of items you can reach by position in one step.

Explanation

An array is a row of boxes, side by side, each holding one item. Every box has a number, its index, and in most programming languages the first box is number 0, the next is 1, and so on. An array of 5 items has indexes 0 to 4.

index:   0    1    2    3    4
value:  [7]  [3]  [9]  [1]  [5]

Reading is instant

Because the boxes sit next to each other in memory and are all the same size, the computer can work out where box i is with one sum: start of the array, plus i times the size of a box. So reading a[0] and reading a[999999] take the same time. We say reading by index is O(1): constant time, however long the array.

Inserting is not

To put a new item at the front, every item already there has to move one box to the right to make room. In an array of n items that's n moves. Inserting in the middle moves everything after that point. Adding at the end is cheap, as long as there's a free box there.

Operation Cost
Read or change a[i] O(1)
Add at the end (when there's room) O(1)
Insert or remove at the front or middle O(n): the rest shift
Find a value without knowing where it is O(n): look box by box

That last row is why sorted arrays matter: in a sorted array you can find a value with binary search instead of looking box by box.

In code

const a = [7, 3, 9, 1, 5];
a[2];          // 9: reading box 2
a[2] = 4;      // changing box 2
a.push(8);     // adding at the end: cheap
a.unshift(0);  // adding at the front: every item shifts right

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

See it

Insert and watch the shift
Described

A row of numbered boxes holding values, with spare boxes at the end. Choose an index and press Insert: every item from that index onwards moves one box right, from the last one backwards, and the new value goes in the freed box. A counter says how many items moved. Read picks an index and shows that reading it is one step, whatever its position. Inserting at index 0 moves every item; inserting at the end moves none. The arrow keys move the chosen index. With reduced motion, items move without animation.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · based on 29fad5e633ad · new, not checked yet · Markdown

1 more: See it

beginner · visual

Insert and watch the shift
Described

A row of numbered boxes holding values, with spare boxes at the end. Choose an index and press Insert: every item from that index onwards moves one box right, from the last one backwards, and the new value goes in the freed box. A counter says how many items moved. Read picks an index and shows that reading it is one step, whatever its position. Inserting at index 0 moves every item; inserting at the end moves none. The arrow keys move the chosen index. With reduced motion, items move without animation.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Worked example

Insert 6 at index 2

The array is [4, 8, 15, 16, 23] (indexes 0 to 4), with room for one more. Insert 6 so it sits at index 2.

  1. Step 1

    Everything from index 2 onwards has to move one box right. Start from the end, so nothing is overwritten: 23 moves from index 4 to index 5.

  2. Step 2

    16 moves from index 3 to 4, then 15 moves from index 2 to 3. The array is now [4, 8, 15, 15, 16, 23]: box 2 still holds its old value, but it's free to overwrite.

  3. Step 3

    Put 6 in box 2: [4, 8, 6, 15, 16, 23]. That took 3 moves (the 3 items after the insertion point) and 1 write. Inserting at index 0 would have moved all 5.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Practice

An array holds 10 items in boxes 0 to 9, with room for more. How many items must move to insert a new item at index 3?

Hint

Count the items in boxes 3 to 9: each moves one box right.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Put the steps of inserting into the middle of an array in order.

Put these in order with the buttons.

  1. Write the new item into the freed box
  2. Move the last item one box right
  3. Check there's a free box at the end
  4. Keep moving items right, back to the insertion point

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

In the array [10, 20, 30, 40], what is a[1]?

Choose one

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Fill the gaps.

In most languages the first item of an array is at index . Reading an item by its index takes the same time however long the array is, which we write as O().

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Check yourself

An array holds 1,000 items in boxes 0 to 999, with room for more. How many items must move to insert a new item at the front?

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Which of these take the same time however long the array is? Choose all that do.

Choose all that are right

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Common mistakes

The first item is at index 1

In everyday counting the first thing is number 1, so a[1] feels like it should be the first item. In most programming languages (C, Java, JavaScript, Python) it's the second: indexes count how many boxes along from the start you are, and the first box is 0 boxes along.

Fix: read a[i] as "the item i boxes after the first". An array of n items runs from index 0 to n−1.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown

Inserting is as cheap as reading

Reading a[i] is instant, so it's natural to think putting a new item at a[i] is too. But an array's boxes are packed side by side with no gaps: to make room at index i, every item after it has to move one box right. At the front, that's all n of them.

Fix: picture the row of boxes and count what has to move. Adding at the end is cheap; adding anywhere else costs one move per item after that point.

@si / anthropic (says) / claude-opus 5.5 (says) / high (says) · 2026-10-14 · new, not checked yet · Markdown