Data structures
Seven units on how programs store data: arrays and dynamic arrays, stacks, queues and ring buffers, linked lists, hash tables, binary search trees, and how to choose between them. Each unit has a lab that runs insert and remove one step at a time and counts the work, two JavaScript exercises checked in the page, and 10 practice questions with explanations.
What you will learn
- Tell an interface from the structure behind it
- See why shift makes a queue slow
- Build a list from nodes and next pointers
- Search and insert by going left or right
- List the operations a program needs most
Units
- 1Arrays20 pages29 minExplain3Explore4Problem10Practice2
- 2Stacks19 pages27 minExplain3Explore4Problem9Practice2
- 3Queues20 pages29 minExplain3Explore4Problem10Practice2
- 4Linked lists18 pages25 minExplain4Explore2Problem9Practice2
- 5Hash tables18 pages25 minExplain3Explore3Problem9Practice2
- 6Binary search trees21 pages30 minExplain5Explore3Problem10Practice2
- 7Choosing a data structure20 pages27 minExplain5Explore2Problem10Practice2
About this course
A program has to keep its data somewhere, and the choice decides how many steps each operation takes. Reading a[3] from an array is one step, because the address is found by arithmetic: start plus the index times the cell size. Taking the first of 1,000 values out of a JavaScript array with shift moves the other 999, while a ring buffer does the same job in one step. The course is about making that difference visible by counting.
The seven units are arrays, stacks, queues, linked lists, hash tables, binary search trees and choosing between them. Arrays include dynamic arrays, where doubling turns capacity 5 into 10 so the next four pushes cost 1 step each. Stacks give last in, first out, and queues give first in, first out, with the ring buffer as a fixed-size queue that must refuse a new value when it has no free cell. Linked lists pay off when you change the front: two pointer changes and nothing else moves. Hash tables are shown with 7 buckets holding 12, 25, 33, 47 and 70, using h(k) = k mod 7, and the step counter adds 1 for the hash and 1 for every key compared in the chain.
Each unit has a lab that runs insert and remove one step at a time and counts the work. One warning matters here. If you change the starting tree to sorted input, the same seven keys form a long chain instead of a balanced tree. A perfectly balanced tree with 1,023 nodes has height 9, so the worst search takes 10 comparisons, while a linear search for a missing key among 1,023 keys makes 1,023.
Before you start
The exercises are written in JavaScript, so JavaScript comes first, especially its arrays and objects. Algorithms helps too, since this course compares structures by counting the steps each operation takes and refers back to binary search and Big O throughout.
Course map
Programming · Courses: 4Sources for this course (11)
- MIT OpenCourseWare 6.006 Introduction to Algorithms, Spring 2020 (Demaine, Ku, Solomon)
- lecture notes 2 Data Structures (interface vs data structure, Sequence and Set interfaces, stack = insert_last/delete_last, queue = insert_last/delete_first, array, linked list, dynamic array, amortized Θ(1) append)
- 4 Hashing (chaining, pigeonhole collisions, chain length n/m)
- 6 Binary Trees Part 1 (height, traversal order, O(h) operations) and 7 Binary Trees Part 2: AVL (Adelson-Velsky and Landis, 1962) at ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/pages/lecture-notes
- MDN Web Docs, Array.prototype.shift (shifts all values left by 1)
- Array.prototype.unshift, Map (insertion order, Map vs Object, spec requires sublinear average access) and Set (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)
- ECMAScript 2025 Language Specification (tc39.es/ecma262)
- 24.1 Map Objects and 24.2 Set Objects ("must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements")
- Array.prototype.shift steps (Repeat, while k < len: move element k to k - 1)
- D. E. Knuth, The Art of Computer Programming vol. 3 (2nd ed. 1998) section 6.4 History (H. P. Luhn, IBM internal memorandum January 1953, hashing with chaining; digit pairs added mod 10, 31415926 -> 4548) via gwern.net copy, and IEEE Spectrum, "Hans Peter Luhn and the Birth of the Hashing Algorithm" (2018)
- "A brief history of the stack", SIGCIS (sigcis.org/files/A brief history.pdf: Bauer and Samelson applied for the patent in 1957, US 3,047,228 granted July 31, 1962; IPL list processing by Newell, Simon and Shaw at RAND 1956) and Wikipedia, Information Processing Language (Newell, Shaw, Simon, RAND and Carnegie Institute of Technology, about 1956).
Checked: 2026-10-18