Programming Fundamentals · Linear Structures: You Use Them Every Day

Arrays: Every Message You Chat Lies in One

Last lesson we locked it in: a data structure = a way of organizing. The first way of organizing is one you feed data into every day — every exchange you have with an AI is, to a program, a message list, and a message list's real form is the plainest way of organizing: a row of numbered cells, called an array. This lesson spreads your chat out on the table, then casually answers an old question: why does it forget when you've talked for a while.

Play first · your chat is lying in a row of cells

Below is a message list: each message owns a cell, and above the cell is its index (the number, counting from 0 — a programmer habit). Tap “Send a message” and watch where the new one lands; then drag the “Context Window” slider and notice which cells go gray — and which one never does.

system(📌 pinned) user(you) assistant(AI) Blue bar underneath = inside the Context Window
4 msgs
That's the whole truth behind “it forgets when chats get long.” A model can only read so much at once (the Context Window); when it doesn't fit, something has to go. Which end? Cut the head, keep the tail — drop the oldest small talk first; the latest few lines must stay, or it won't even know what you just said. Only the system prompt in cell [0] is pinned: it's the persona and the rules (“You're a thoughtful assistant”). Toss that, and the AI forgets who it is. So “forgetting” isn't mysticism — it's an array slice: keep system + the latest K messages; everything else never reaches the model.
Round two · how expensive is inserting in the middle?

An array's cells sit tightly packed in memory — no gaps allowed in the middle. That brings a headache: to squeeze a new element into the middle, every element to its right has to shift one slot right to make room. Tap any cell to insert a ⭐ there, watch “Moves”; then tap “Append at the end” and compare.

👇 Tap a cell = insert ⭐ at that spot (further left → more neighbors on the right have to move) Moves 0
Try tapping a cell on the left first, like [1]
See the pattern? Appending at the end is always 1 step; inserting in the middle moves a whole stretch — more cells, and further forward you insert, the worse the move. That's why chat history is designed to only grow backward (append-only): every new message is pushed to the end, nobody relocates, fast and cheap. You've never seen a chat app let you “slip a line into ten minutes ago” — not because PMs never thought of it, but because doing that is genuinely expensive.
An array's signature move (and its soft spot)
🎯

Signature move: jump straight by index

Want message #3? messages[3]no counting from the start, one hop. Because cells sit packed, the index is the address — that's O(1), or in plain words “no matter how long the array is, the cost is the same.” For fetch-by-position, the array is the fastest way of organizing, full stop.

⚖️

Soft spot: middle inserts are expensive

You just moved things yourself. Meet a relative while you're at it: the linked list — middle inserts are cheap (tweak two “who's next” pointers), but you lose jump-by-index; finding the 100th means walking from the head one by one. There's no all-purpose way of organizing, only trade-offs. For chat — “append only, often read in chunks” — arrays win, so the message list uses one.

✅ What this lesson wants to share