Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 225 ยท Getting Started

Introduction to Data Structures and Algorithms

Learn what data structures and algorithms are, why they matter, and how this Python tutorial takes you from arrays and Big O to graphs and dynamic programming.

Every program does two things: it stores data and it does work on that data. A contact app stores names and phone numbers and searches them. A map app stores roads and finds the shortest route. A game stores enemies and decides which one is closest. How you store the data and how you do the work decides whether your program answers instantly, takes a coffee break, or never finishes at all.

That is what this tutorial is about. A data structure is a way of organizing data in memory so that certain operations are fast. An algorithm is a step-by-step procedure that solves a problem. The two always travel together: the right structure makes the algorithm simple, and the right algorithm makes the structure worth having.

You don't need a computer science degree to follow along. If you can write a loop and a function in Python, you can start here. Every example runs in your browser, so you can change the code and see what happens.

A first taste: the same problem, two answers

Imagine a phone book with 1,000 names, sorted alphabetically. You want to know whether "Marcus" is in it. There are two natural ways to look.

  1. Read from the first name onward until you find it or run out of names. In the worst case you read all 1,000.
  2. Open the book in the middle, see whether "Marcus" comes before or after that name, throw away the wrong half, and repeat. You never need more than 10 looks.

Same input, same answer, wildly different effort. Run both and count the steps:

Python Two ways to search

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first approach grows with the size of the book. The second barely notices: doubling the book from 1,000 to 2,000 names adds just one more look. Learning to see and measure that difference is the heart of DSA. You will meet this halving idea properly in Binary Search, and the tool for talking about growth in Big O Notation.

Why it matters

Speed and scale. On 10 items, nearly any approach works. On 10 million items, the difference between a smart and a naive approach can be the difference between a fraction of a second and many years. The next example shows a tiny version of that gap: finding out whether a list contains a duplicate.

Python Duplicate check: brute force vs a set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the list is 100 times longer (10 to 1,000), the slow version does over 11,000 times more work, while the set version does 100 times more. The slow one compares every pair; the fast one remembers what it has seen. Remembering costs memory, so you traded space for time, one of the most common bargains in this field.

Interviews. Almost every software job interview asks you to solve problems with arrays, hash maps, trees and graphs, and to explain how fast your solution is. This tutorial teaches each pattern and the thinking behind it, so you can solve problems you have never seen.

Better everyday code. Knowing that x in some_list scans the whole list while x in some_set does not will make your ordinary programs faster, with no extra effort.

The toolbox map

The tutorial is organized like a toolbox. Each chapter adds tools, and later chapters use earlier ones.

TEXT
 Foundations        Analyzing Algorithms (Big O, space, amortized, recurrences)
      |
 Linear structures  Arrays -> Strings -> Linked Lists -> Stacks and Queues
      |
 Hashing            Hash tables, dictionaries, sets, caches
      |
 Techniques         Recursion -> Backtracking -> Divide and Conquer
      |
 Searching/Sorting  Binary search, merge sort, quick sort, heaps
      |
 Hierarchies        Trees, BSTs, balanced trees, heaps, tries
      |
 Networks           Graphs -> BFS/DFS -> shortest paths -> spanning trees
      |
 Optimization       Dynamic programming, greedy algorithms
      |
 Special topics     Math, bit tricks, projects and practice

Here is the one-sentence version of each major tool, so the names are not strangers later:

Tool What it is Best at
Array / list Items in a row, numbered from 0 Reading item i instantly
Linked list Items chained by references Inserting in the middle without shifting
Stack Last in, first out Undo, matching brackets, backtracking
Queue First in, first out Fair ordering, breadth-first search
Hash table Key to value lookup Finding things in about constant time
Tree Parent and children Hierarchies, ordered data, fast search
Heap Tree where the smallest sits on top Always getting the minimum or maximum
Graph Items and connections Maps, networks, dependencies
Dynamic programming Remember sub-answers Problems with overlapping sub-problems

There is a natural order, but you do not have to read strictly front to back. If you already know arrays and Big O, skip ahead. Each lesson links to what it needs.

The best learning loop is read, run, modify, predict. After each example, change a number, guess the output before you press Run, and see whether you were right. Predicting is what turns reading into understanding.

What a data structure really is

A data structure is more than a container. It is a container plus the operations it supports, plus the cost of each. Take four ways to hold the same four numbers:

Python Same data, different structures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each of these is "four numbers", yet each makes a different question cheap. Choosing a structure means asking: which operations will my program do most? That question drives every chapter that follows, and the lessons Memory and Data Layout and Abstract Data Types give you the vocabulary to answer it.

What an algorithm needs

An algorithm is not just "some code that works". A good definition has four properties:

  • Input and output are clearly defined.
  • Definiteness: every step is unambiguous.
  • Finiteness: it stops after a finite number of steps.
  • Correctness: it produces the right output for every valid input, not just the examples you tried.

When you compare algorithms, you compare two things: time (how many steps as the input grows) and space (how much extra memory). We measure both with the same tool, Big O notation, which you will meet in Big O Notation. A correct but slow algorithm is often acceptable for small inputs; a fast but wrong one never is.

Do not measure algorithms by stopwatch alone. Timings depend on your computer, the browser and what else is running. Throughout this tutorial we count operations (comparisons, steps, swaps) instead. Counts are the same on every machine, and they show how work grows.

How to use this tutorial

Every lesson follows the same shape, so you know where to look:

  1. Intuition: a picture or an everyday analogy.
  2. The idea in plain words, then code from scratch in Python 3.14.
  3. A trace of a small example, so you can see every step.
  4. Complexity: time and space, with the reason for each.
  5. Common mistakes and when to use or avoid the technique.
  6. A quiz and a summary.

For problem lessons we always go brute force first, then improve. The brute force proves you understand the problem and gives you a correct answer to compare against; the improvement shows why the clever idea works.

Implement things yourself. Reading code feels like understanding, but typing it, breaking it and fixing it is what makes it stick. We build structures from scratch with classes, and then show the equivalent Python built-in (list, dict, set, collections.deque, heapq, bisect), because in real projects you will usually use the built-in.

What you need from Python

You need the basics: variables, if, loops, functions, lists and dictionaries. Classes appear from the linked list chapter onward. If any of that feels shaky, the next lesson, Python You Need for DSA, is a compact review, and the full Python tutorial covers everything in depth.

One last demonstration to leave you with: the same problem, "sum the numbers from 1 to n", solved by looping and by a formula. Count the operations.

Python Looping vs a formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both give the same answer. One does work proportional to n; the other does the same amount of work no matter how big n is. Spotting the formula, the set, the halving or the memo table is what this whole subject trains you to do.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a data structure?

    A data structure organizes data in memory together with the operations it supports; different structures make different operations cheap.
  2. 2. In the phone-book example, why does binary search need only about 10 looks for 1,000 sorted names?

    Each look discards half of the remaining names. 1,000 names shrink to 500, 250, 125 and so on, reaching one name after about 10 halvings.
  3. 3. The set-based duplicate check used extra memory to run faster. What is this trade called?

    Keeping a set of values already seen costs memory, but it avoids comparing every pair of items, so the running time drops.
  4. 4. Why does this tutorial count operations instead of timing code with a stopwatch?

    Timings vary with hardware and background load. Operation counts are reproducible and reveal the growth pattern that Big O describes.
  5. 5. Which is NOT one of the properties of a proper algorithm?

    An algorithm is independent of any programming language. Finiteness, definiteness and correctness are the properties that matter.

Summary

  • A data structure organizes data so some operations are fast; an algorithm is a step-by-step procedure that solves a problem.
  • The right choice can change running time from "instant" to "never finishes" as the input grows.
  • We compare solutions by time and space, measured by counting operations and described with Big O.
  • Many speed-ups are a space-for-time trade, like remembering seen values in a set.
  • The tutorial builds a toolbox: arrays, linked lists, stacks, queues, hashing, recursion, searching, sorting, trees, heaps, graphs and dynamic programming.
  • Always try brute force first, then improve, and implement things yourself.

Next: Python You Need for DSA, a compact review of the Python features the rest of the tutorial relies on.