Skip to content
My Subject Hub
Free class
Data Structures and Algorithms

How to learn Data Structures and Algorithms from scratch

Data Structures and Algorithms feels huge until you learn it in the right order. Here is what to know first, a topic sequence that builds on itself, and a way to practise that works.

By Ali Sher AbbasiPublished 4 min read

Data Structures and Algorithms (DSA) is the study of how to store data so it is quick to use, and how to write step-by-step methods that solve problems efficiently. It is a core university course, the basis of most technical job interviews, and the difference between code that works on ten records and code that works on ten million. It is also the course many students find hardest, usually because they start in the wrong place.

What you need before you start

You do not need advanced maths. You do need to be comfortable in one programming language, which means you can write the following without looking anything up:

  • loops, including a loop inside a loop
  • functions that take parameters and return values
  • arrays or lists, and how to go through them by index
  • a simple class, if you are using Java or C++ (see OOP concepts explained)
  • basic recursion: a function that calls itself, with a stopping case

If any of these feel shaky, spend a week or two on them first. Most DSA confusion is really programming confusion in disguise. Any language works; Python is the shortest to read, and C++ and Java are common in university courses.

Big-O: how to measure an algorithm

Big-O notation describes how the work an algorithm does grows as the input grows. It ignores the speed of your computer and asks: if the list doubles, does the work stay the same, double, or quadruple? Learn this first, because every later topic is judged by it.

  • O(1), constant: the same work whatever the size. Reading marks[5].
  • O(log n), logarithmic: the problem halves at each step. Binary search.
  • O(n), linear: look at every item once. Finding the largest mark.
  • O(n log n): efficient sorting such as merge sort.
  • O(n²), quadratic: a loop inside a loop over the same data. Comparing every pair.

Binary search shows why this matters. To find a value in a sorted list, check the middle; if your target is larger, throw away the left half, otherwise the right half, and repeat.

binary_search.py
def binary_search(items, target):
    """Index of target in a sorted list, or -1. O(log n)."""
    low, high = 0, len(items) - 1
    while low <= high:
        mid = (low + high) // 2
        if items[mid] == target:
            return mid
        if items[mid] < target:
            low = mid + 1       # target is in the right half
        else:
            high = mid - 1      # target is in the left half
    return -1


print(binary_search([3, 8, 15, 21, 42, 57, 90], 42))   # 4

Checking one item at a time would need up to a million comparisons in a list of a million items. Binary search needs about twenty, because a million can only be halved about twenty times before one item is left.

The order to learn topics in

Each step below uses the ones before it. Resist jumping to graphs or dynamic programming early; they are much easier once the foundations are automatic.

  1. Big-O and analysing loops. Count the work in your own code.
  2. Arrays and strings. Two pointers, sliding windows, prefix sums.
  3. Linked lists. Insert, delete and reverse. Your first taste of pointers or references.
  4. Stacks and queues. Matching brackets, undo, and processing tasks in order.
  5. Hash tables (dictionaries, maps). Counting and fast lookups: often the step that turns O(n²) into O(n).
  6. Recursion, properly. Draw the call tree and trust the stopping case.
  7. Sorting and searching. Bubble and insertion sort to understand them; merge sort and quicksort to use them; binary search everywhere.
  8. Trees. Binary trees, traversals and binary search trees.
  9. Heaps and priority queues. Always take the smallest (or largest) next.
  10. Graphs. Breadth-first and depth-first search, then shortest paths.
  11. Dynamic programming. Last, because it builds on recursion and careful analysis.

A practice method that works

Watching explanations feels productive but fades quickly. For each new topic, work through these four steps:

  1. Draw it. Sketch the structure on paper: boxes and arrows for a linked list, levels for a tree. Do one operation by hand.
  2. Build it yourself. Implement it from scratch once, even though your language has a built-in version. You will understand the built-in one much better.
  3. Solve three to five small problems that use it. Start easy. If you are stuck for more than about thirty minutes, read a hint, not the full solution.
  4. Come back after a few days and solve one problem again without looking at your old code. This is when it becomes yours.

Keep a short log: the problem, the idea that cracked it, and its Big-O. Before an exam or interview, rereading this log is worth more than starting new problems.

Common traps

  • Memorising solutions. Interviewers and examiners change the problem slightly. Learn the pattern behind each solution.
  • Skipping complexity. A correct answer that is O(n²) when O(n) is possible will often lose marks or time out.
  • Only reading code. If you have not typed it and broken it, you do not know it yet.
  • Comparing yourself to others' streaks. Steady practice a few times a week beats a burst followed by a month off.

Getting help

DSA is easier to learn with someone who can watch you solve a problem and spot exactly where your reasoning slips. Our Data Structures and Algorithms tutoring is one-to-one and live, in Python, Java or C++, for university courses and interview preparation. We explain and review; we do not complete graded assignments for you. The first class is free.

One-to-one live classes on this topic. The first class is free.

More guides

  • Programming for beginners

    Which programming language should you learn first?

    Your first language matters less than you think, but the right one for your goal saves weeks of frustration. Here is how to choose, by what you want to do next.

    4 min read

  • Object-oriented programming

    OOP concepts explained with one running example

    Object-oriented programming is easier when every idea is shown on the same example. We build a small library system step by step and name each OOP concept as it appears.

    4 min read

All guides