Skip to content
ReadMe Daily

Search articles, problems, patterns and every cheat sheet topic.

Data Structures and Algorithms: What They Are and Where to Start

What data structures and algorithms are, why they are learned together, and the order to study them in if you are starting from zero.

· Updated · 8 min read

A data structure is a way of organising data in memory. An algorithm is a sequence of steps that solves a problem. They are taught together because the structure you pick decides which algorithms are possible and how fast they run.

A data structure is a trade-off

Every data structure makes some operations cheap and others expensive. There is no best one, only the best one for the operations you need.

Structure Cheap Expensive
Array Reading the item at a position Inserting at the front
Linked list Inserting at the front Reading the item at a position
Hash table Finding an item by key Keeping items in sorted order

Choosing a structure means asking one question: which operations will this program do most often?

The main structures at a glance

Structure Picture it as Best at Read more
Array Numbered boxes in a row Reading by position Arrays
Linked list A chain, each link pointing to the next Adding and removing at the front Linked lists
Stack A pile of plates Handling the newest item first Stacks and queues
Queue A line at a counter Handling the oldest item first Stacks and queues
Hash table A coat check: a ticket finds your coat at once Finding an item by its key Hash tables
Tree A family tree Data with levels, and keeping items in order
Heap A tree with the smallest item always on top Repeatedly taking the smallest or largest
Graph A map of towns and roads Connections between things

The first five are the foundation and are covered on this site. The last three are built from the same parts, nodes and references, arranged differently.

An algorithm is a recipe with a cost

An algorithm takes some input and produces an output in a finite number of steps. Two algorithms can give the same answer at very different costs.

Suppose you need to find one name in a list of a million sorted names.

  • Linear search checks every name from the start. In the worst case that is a million checks.
  • Binary search opens the list in the middle, throws away the half that cannot contain the name, and repeats. That takes about 20 checks.

Both are correct. Only one is usable at scale, and it only works because the data is sorted. That link between how data is arranged and how fast you can work with it is the whole subject.

The main kinds of algorithm

Kind What it does Example on this site
Searching Finds an item Binary search
Sorting Puts items in order Used as a first step in 3Sum
Two pointers Covers a sequence with two indexes instead of a nested loop Two pointers
Sliding window Tracks a moving range of neighbouring items Sliding window
Hashing Replaces a search with a lookup Hashing
Prefix sums Stores running totals to answer range questions Prefix sums
Recursion Solves a problem using smaller copies of itself Reverse Linked List
Traversal Visits every node of a tree or graph The stack and queue example in Stacks and queues

Most problems are solved by recognising which of these fits, not by inventing something new.

One problem, three costs

The question "does this list contain a value twice?" has three well-known answers:

Approach Structure used Time Extra memory
Compare every pair None O(n²) None
Sort, then compare neighbours A sorted copy O(n log n) O(n)
Remember what has been seen A hash set O(n) O(n)

For a list of a million items, the first needs around five hundred thousand million comparisons, the second around twenty million steps and the third one million. All three are correct. Choosing between them is what this subject teaches, and Contains Duplicate works through each one in code.

Why it matters outside interviews

  • Performance. A slow page or a job that never finishes often comes down to a nested loop over a large list, where a hash table lookup would have done the same work in one pass.
  • Reading other code. Databases, caches, routers and compilers are built from these structures. Knowing them makes documentation and source code easier to follow.
  • Interviews. Most technical interviews test whether you can pick a suitable structure and explain the cost of your solution.

What you need before starting

Less than people expect.

  • One programming language, to the level of writing loops, conditions and functions and using arrays and objects. JavaScript and Python are both good choices, and the problems on this site give solutions in both.
  • Arithmetic and a little logic. No calculus and no advanced mathematics. The one idea worth getting comfortable with is the logarithm: how many times a number can be halved.
  • Patience with being stuck. Getting stuck, then seeing why the answer works, is how the patterns stick.

You do not need a computer science degree, a particular editor or a fast computer.

The order to learn things in

Each step uses the one before it.

  1. Big O notation. The language for describing cost. Everything else is described with it.
  2. Arrays. The simplest structure, and the one most problems start with.
  3. Linked lists. The opposite set of trade-offs to an array.
  4. Stacks and queues. Two restricted structures that appear inside many algorithms.
  5. Hash tables. The structure that turns many slow solutions into fast ones.
  6. Problem-solving patterns. Reusable techniques such as two pointers, sliding window and binary search.

Trees, graphs, recursion, sorting and dynamic programming come after these.

The roadmap lays the whole site out in this order, with every pattern and its problems.

A six-week plan

An hour a day is enough. Adjust the pace to suit you.

Week Study Practise
1 Big O notation, arrays Work out the cost of code you have already written
2 Hash tables, the hashing pattern Contains Duplicate, Two Sum
3 Two pointers, sliding window Valid Palindrome, Maximum Sum Subarray of Size K
4 Binary search, prefix sums Binary Search, Find Pivot Index
5 Stacks, queues, linked lists Valid Parentheses, Reverse Linked List
6 Revise, then the medium problems for each pattern One medium problem a day

Leave the hard problems until the medium ones feel routine.

How to practise

Reading is not enough; the skill is recognising which technique a new problem needs.

  1. Read the pattern explanation first, for example two pointers.
  2. Try a problem that uses it before reading the solution.
  3. Write the simplest solution that works, even if it is slow, and work out its cost.
  4. Improve it, and be able to say why the new version is faster.

The practice problems on this site follow those steps: each one starts with the brute-force approach and then shows the better one, with the cost of both.

Common mistakes when learning

  • Memorising solutions. A memorised answer fails as soon as the question changes slightly. Aim to recognise the pattern.
  • Skipping the slow solution. The brute-force version shows what is being repeated, which is exactly what the faster version removes.
  • Reading without typing. Following an explanation feels like understanding. Writing the code from a blank file shows whether it is.
  • Not working out the cost. A solution without its time and space complexity is half an answer.
  • Starting with hard problems. They combine several ideas. Learn each idea on an easy problem first.
  • Looking at the answer too soon. Give a problem a real attempt, around twenty to thirty minutes, before reading the solution. Then close it and write it yourself.

Common questions

Which language should I use? The one you know best. The ideas are the same in all of them. If you are choosing, Python has the least syntax to get in the way, and JavaScript runs in any browser with nothing to install.

How long does it take? The foundations here take four to eight weeks of steady practice. Being quick and reliable on unfamiliar problems takes a few months more.

Do I need to be good at maths? No. You need to count how many times loops run, and to be comfortable with the idea of halving.

Is this only for interviews? No. The same knowledge explains why a database query is slow, why a page freezes on a long list, and which library to pick for a job.

How many problems should I solve? Fewer than you think, done properly. Three problems per pattern, each one understood well enough to explain to someone else, beat thirty skimmed.

Summary

  • A data structure organises data. An algorithm is a series of steps that uses it.
  • Every structure is a trade-off: fast at some operations, slow at others.
  • The same problem can be solved at very different costs, and Big O is how those costs are compared.
  • Learn in order: Big O, arrays, linked lists, stacks and queues, hash tables, then the patterns.
  • Practise by attempting first, writing the slow solution, working out its cost, then improving it.