Lecture notes · Algorithms · Lecture 2

Data Structures and Dynamic Arrays

MIT 6.006 Introduction to Algorithms, lecture 2, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part explains the difference between what a data structure promises to do and how it actually does it, then starts on the simplest version: a fixed-length sequence of items stored in an array.

Key ideas

Words to know

interface
The list of things you are allowed to do with some data, and what each action should do. It is the promise, not the method behind it.
data structure
A way of storing data in memory, along with the steps (algorithms) for doing each action on it quickly.
static array
A fixed-size row of storage spots, each holding one item and numbered from 0, so any item can be reached by its number.
word RAM
A simple model of computer memory where memory is split into equal-sized words, and any word can be read or written directly by its number.

Example

Think of a row of numbered lockers at a gym. The interface is the rule: you can put a bag in locker 5, take a bag out of locker 5, or count how many lockers there are. The data structure is how the lockers are actually built and labeled so that finding locker 5 takes almost no time. Many different locker designs could follow the same rule.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains how an array stores items side by side in memory so any item can be found quickly, and why inserting into the middle of a sequence changes the positions of the items after it.

Key ideas

Words to know

index
The position number of an item in a sequence, starting from 0 for the first item.
consecutive
Following one after another with no gaps, like houses on one street with no empty lots between them.
constant time
A task whose time does not get longer as the amount of data gets bigger.
word size
How many bits a computer handles at once as one unit. The lesson says it has to grow at least as fast as log n so that every item in memory can be given an address.

Example

Think of a row of numbered mailboxes built one after another on the same street. If you know where box 0 is and you want box 7, you just walk 7 boxes past it, so you go straight there without checking each box. If a new box is added at position 2, every box after it has to be renumbered by one.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains why some ways of storing a list are fast for adding and removing items and others are slow, and it introduces the linked list as a first answer.

Key ideas

Words to know

node
A small container that holds one value and a pointer to the next container.
pointer
A stored address that tells the computer where something else lives in memory.
head
The pointer to the first node in a linked list. It is how you get into the list.
static array
A fixed-size block of slots in memory. Its size cannot be changed once it is made.

Example

Picture a scavenger hunt where each clue tells you where to find the next clue. If you want to add a new first clue, you only need to write it so that it points to the old first clue, and then hand it out as the start. You do not have to move or rewrite any of the other clues. A numbered row of lockers works differently: adding a locker at the front means relabeling every locker behind it.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains why linked lists and static arrays each fall short, and how a dynamic array keeps spare room so it can grow without slowing down every insert.

Key ideas

Words to know

pointer
A stored address that tells the computer where another piece of data lives, like a note saying which shelf to check next.
constant time
A task takes about the same amount of time no matter how big the collection is.
dynamic array
A list-like container that can grow or shrink, because it keeps some empty spare room behind the items it holds.
data structure augmentation
Adding an extra stored value to a data structure, such as a tail pointer, so some task becomes faster, and then keeping that value updated every time the structure changes.

Example

Think of a bookshelf with some empty slots at the right end. You add new books in the next empty slot and keep a note of how many books are on it. When the shelf is full, you buy a bigger one, but you do not do this every time you add a single book.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:11

This part explains why a dynamic array that doubles in size when full can still add items to the end quickly on average.

Key ideas

Words to know

amortized
Averaged out over a whole series of operations, so that one slow step is balanced by many fast ones.
allocate
To set aside space in memory for a new array.
geometric series
A list of numbers where each number is a fixed multiple of the one before, like 1, 2, 4, 8. Its total is dominated by the last, biggest term.
constant factor
A fixed number that multiplies the running time. It changes how fast something is in practice but not its overall pattern of growth.

Example

Think of a bookshelf that fills up. If you buy a shelf just one slot bigger each time, you have to move all your books again and again. If you buy one twice as big each time, you move books only rarely, so the moving cost spreads out over all the books you add.

3 checked practice questions for this part are in the app.

← Lecture 1: Algorithms and Computation · Lecture 3: Sets and Sorting →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York