Chapter 1 · Foundations

What are
Data Structures?

Every program ever written was built on decisions about how to organise data. Those decisions — choosing the right structure for the job — are what separate engineers who write fast software from those who don't.

What is a data structure?

A data structure is a way of organising data in memory so that specific operations are fast. An array gives instant access by index, a hash map gives instant lookup by key, and a tree keeps data sorted for fast search. Choosing the right structure is usually the difference between code that takes milliseconds and code that takes minutes.

const list  = [3, 1, 4];               // array: index access O(1)
const prices = new Map([["tea", 2]]);   // hash map: key lookup O(1)
const seen  = new Set(["a", "b"]);      // set: membership test O(1)
const stack = []; stack.push(1); stack.pop();   // LIFO
const queue = []; queue.push(1); queue.shift(); // FIFO
0
Topics covered
0+
Hours of content
0%
Language-agnostic

The core idea

A data structure is a specific way of organising data in memory so that common operations — access, insert, delete, search — can be performed efficiently.

Think of it like physical storage. A numbered bookshelf lets you retrieve book #5 in O(1). A pile on the floor means scanning every book — O(n). The data is identical. The organisation is everything.

Array

Numbered Shelf (Array)

Every slot is numbered. Reach position 5 instantly. Insert in the middle and every item after it must shift.

Access: O(1) · Insert-middle: O(n)
Map

Labelled Cabinet (Hash Map)

Each drawer is labelled. Look up "alice" and the label points you directly to the right drawer — no searching required.

Lookup: O(1) avg · No ordering
Stack

LIFO Pile (Stack)

You can only take from or add to the top. Last item added is first removed. Perfect for undo history and recursion.

Push/Pop: O(1) · Random access: none
Tree

Branching Hierarchy (Tree)

Branching hierarchy. Following branches finds any node. Balanced trees keep every path short — O(log n).

Search: O(log n) in a balanced tree

Why the right choice matters

The difference between a good and bad data structure choice isn't academic. It directly translates to user experience, infrastructure cost, and whether your system scales to 10 users or 10 million.

scenario: find a user by ID in a list of 1,000,000 users

Unsorted array — scan every record~50 ms
Sorted array + binary search~0.002 ms
Hash map — direct key lookup~0.0001 ms
🔍The 500,000x difference
Switching from a linear scan to a hash map is a 500,000× speedup in this example — and the data set is only 1M records. Production databases have billions of rows. This is why data structure choices are the first thing senior engineers think about in every system design discussion.

The four families

Every data structure belongs to one of four broad families. Understanding the family immediately tells you the core trade-offs.

Linear

Elements in a sequence. Iteration is natural. Random access varies by implementation.

ArrayLinked ListStackQueueDeque

Tree-based

Hierarchical branching. Balanced trees give O(log n) search, insert, and delete.

Binary TreeBSTHeapTrieSegment TreeAVL Tree

Hash-based

Trade a little memory for speed. O(1) average for lookup. No inherent ordering.

Hash MapHash SetBloom Filter

Graph-based

Model arbitrary relationships between nodes. Foundation of routing, scheduling, dependency resolution.

Directed GraphUndirected GraphWeighted GraphDAG

Abstract type vs. concrete structure

This distinction trips beginners constantly. A Stack is an abstract data type (ADT) — a contract that specifies push, pop, and peek. The concrete structure is the internal implementation. You can back a stack with an array or a linked list and the interface stays identical.

abstract-data-type.pseudo
1ADT Stack:
2 push(item) → add item to top
3 pop() → remove and return top item
4 peek() → return top item without removing
5 isEmpty() → return true if no items
6 
7// Two valid concrete implementations:
8Array-backed Stack → contiguous block of memory
9Linked-list Stack → chain of nodes connected by pointers
10 
11// Same contract. Very different memory layouts.

Abstract Data Types

Define the contract — the interface you code against.

◆Stack
◆Queue
◆Map
◆Set
◆Priority Queue

Concrete Implementations

Specify memory layout and algorithm detail.

◆Array-backed stack
◆Linked-list queue
◆Hash table with chaining (Map)
◆Red-black tree (Set)
◆Binary heap (Priority Queue)

How to choose the right structure

Ask these three questions in order. The answers narrow the field quickly.

1 / 3
1

What operations do I need most?

Fast key lookup points to a hash map. Ordered traversal or range queries point to a BST. LIFO / FIFO semantics point to a stack or queue. Always-available minimum or maximum points to a heap.

1 of 3

Quick Reference — Big O

StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Linked ListO(n)O(n)O(1)O(1)
Hash Map—O(1)*O(1)*O(1)*
BST (balanced)O(log n)O(log n)O(log n)O(log n)
HeapO(n)O(n)O(log n)O(log n)

* Average case. Worst case with collisions: O(n).

Operation cost, visualised

Each bar represents how fast the cost grows as n increases. You'll study Big O formally in the next chapter.

Hash map lookup / array indexO(1)

Constant time

Binary search on sorted dataO(log n)

Logarithmic time

Linear scan / linked list searchO(n)

Linear time

Efficient sorting (merge sort)O(n log n)

Linearithmic time

Nested loops / bubble sortO(n²)

Quadratic time

Naive recursive FibonacciO(2ⁿ)

Exponential time

🎯 Quick Check

You need a leaderboard that always returns the top-10 highest scores instantly, with fast inserts of new scores. Which structure fits best?

🧠The one question to ask every time
Before writing any code, ask: “What operation do I need to perform most often — and which structure makes that cheapest?” That single question will guide 80% of your data structure decisions throughout your career.

Data Structures Introduction: summary

Key points

  • Every data structure is a trade-off: fast at some operations, slower at others.
  • Linear structures (arrays, linked lists, stacks, queues) store items in sequence; non-linear ones (trees, graphs) store relationships.
  • Big-O notation describes how an operation's cost grows with the amount of data.
  • JavaScript has arrays, objects, Map and Set built in; the rest you build from them.

Frequently asked questions

›Why should I learn data structures?

They make your programs fast and are the core of most technical interviews. Knowing when to use a Set instead of an array alone can turn an O(n²) loop into O(n).

›Can I learn data structures with JavaScript?

Yes. The concepts are language-independent, and JavaScript lets you run and visualise every example in the browser.

›What should I learn first?

Big-O notation, then arrays, hash maps, stacks and queues, then linked lists, trees and graphs.