Skip to main content

DSA Example

javascript

// A Map replaces the nested loop.
function twoSum(nums, target) {
  const seen = new Map()
  for (let i = 0; i < nums.length; i++) {
    const need = target - nums[i]
    if (seen.has(need)) {
      return [seen.get(need), i]
    }
    seen.set(nums[i], i)
  }
}

Guided tutorial track

DSA Tutorial

Learn data structures and algorithms in JavaScript — arrays, hash maps, trees, graphs, recursion and the patterns interviews actually ask about.

Tutorial overview

Start here, then move through the chapters

This tutorial is designed to be followed in order. Read the chapter, practice with the editor, test your understanding, and move toward completion.

Lessons

54

Sections

25

Access

Free

Level

intermediate

Ready to begin?

Start with lesson one and follow the tutorial step by step.

Open first lesson

This tutorial teaches data structures and algorithms in plain JavaScript - from what Big O actually measures to the handful of patterns that most interview questions turn out to be. Every example runs in the editor on the page, and every chapter says when the technique is the wrong choice.

The same lookup, two structures

javascript

const users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Grace" },
]

// Array: check each entry until you find it. O(n).
function findInArray(id) {
  for (const user of users) {
    if (user.id === id) return user
  }
}

// Map: jump straight to it. O(1).
const byId = new Map(users.map((u) => [u.id, u]))

console.log(findInArray(2).name)  // "Grace"
console.log(byId.get(2).name)     // "Grace"

What DSA actually means

A data structure is how you arrange data - an array, a Map, a tree. A algorithm is what you do with it - search, sort, count, find a path.

They are not separate subjects. Choosing the structure is usually most of the work, and the algorithm falls out of that choice. The two functions beside this do the same job; one of them stops being usable at ten million records.

Why it is worth your time

Two reasons, and only one of them is interviews.

The first is that it is what interviews test, at almost every company that pays well. Not because you will implement a red-black tree at work, but because it is a fast way to see whether you can reason about cost.

The second matters more day to day: it is the difference between a page that loads instantly and one that times out. A nested loop over a list that grew from 200 rows to 200,000 is the single most common cause of code that worked fine last year and does not now.

What this tutorial covers

  • Complexity - Big O, time against space, and why amortised is a stronger promise than average.
  • Core structures - arrays, strings, hash maps, stacks, queues, linked lists, heaps, trees, tries, graphs.
  • Core techniques - recursion, backtracking, sorting, searching, dynamic programming, greedy.
  • Specialised topics - intervals, prefix sums and Fenwick trees, bit manipulation, number theory, matrices.
  • Interview patterns - how to recognise which technique a question is asking for, and how to talk through it.

Twenty-five sections and fifty-four chapters, in a deliberate order: nothing uses an idea you have not met yet.

Two pointers, in the editor

javascript

// Find the pair that adds to the target, in one pass over a sorted array.
function twoSumSorted(sorted, target) {
  let left = 0
  let right = sorted.length - 1

  while (left < right) {
    const sum = sorted[left] + sorted[right]
    if (sum === target) return [left, right]
    if (sum < target) left++
    else right--
  }

  return []
}

console.log(twoSumSorted([1, 3, 4, 6, 8, 11], 10))  // [2, 3]

Everything here runs

Every code block on this site has a Run button. Press it, change a number, run it again - that loop is worth more than reading three explanations.

Each chapter also carries ten practice problems: five to write from scratch and five where you are given working-looking code with one bug in it. The bug-fix half is closer to real work than the blank-page half.

What you need before you start

Comfortable JavaScript: functions, loops, arrays, objects, and the difference between const and let. If any of that is shaky, work through the JavaScript tutorial first - this one moves quickly and assumes it.

You do not need maths beyond arithmetic. Big O looks like maths and is really a way of describing shape: does the work double when the input doubles, or square?

How long this takes

The first third - complexity, arrays, strings, hash maps, two pointers - is a week of evenings, and it is the part that pays off immediately in ordinary code.

Trees, graphs and dynamic programming take longer, because the difficulty is not syntax but recognising the shape of a problem. That recognition only comes from doing the exercises, not from reading the chapters.

Nobody learns this once. You will forget how to write a heap, look it up, and remember it faster the second time. That is the normal path, not a sign you missed something.

Common questions

No. The ideas are the same in every language, and interviewers at almost every company let you answer in whichever one you know best. JavaScript's one genuine gap is that it ships no priority queue, so the heaps chapter writes one from scratch.

The learning path

Work through these in order. Each part builds on the one before it, and every part lists what it covers and roughly how long it takes.

Part 1Beginner

DSA Introduction

2 lessons12 min

What Are Data Structures and Algorithms · How to Approach a DSA Problem

Start this part
Part 2Beginner

Big O and Complexity

3 lessons18 min

Big O Notation Explained · Time vs Space Complexity · Amortised Analysis

Start this part
Part 3Beginner

Arrays

2 lessons12 min

Arrays in JavaScript for DSA · Array Traversal Patterns

Start this part
Part 4Beginner

Strings

3 lessons18 min

Strings in JavaScript for DSA · Common String Problems · String Matching: KMP and Rabin-Karp

Start this part
Part 5Beginner

Hash Maps and Sets

2 lessons12 min

Hash Maps and Sets in JavaScript · When a Hash Map Is the Wrong Choice

Start this part
Part 6Beginner

Two Pointers and Sliding Window

2 lessons12 min

The Two Pointer Technique · The Sliding Window Technique

Start this part
Part 7Beginner

Stacks and Queues

2 lessons12 min

Stacks in JavaScript · Queues in JavaScript

Start this part
Part 8Beginner

Linked Lists

2 lessons12 min

Linked Lists Explained · Linked List Two Pointer Problems

Start this part
Part 9Beginner

Recursion

2 lessons12 min

Recursion Explained · Turning Recursion Into Iteration

Start this part
Part 10Intermediate

Backtracking

2 lessons12 min

Backtracking Explained · Classic Backtracking Problems

Start this part
Part 11Intermediate

Sorting

3 lessons18 min

Sorting in JavaScript · Sorting Algorithms You Should Know · QuickSelect: the Kth Element Without Sorting

Start this part
Part 12Intermediate

Searching

3 lessons18 min

Binary Search Explained · Linear Search and When to Use It · Binary Search on the Answer

Start this part
Part 13Intermediate

Intervals

1 lesson6 min

Merging and Sorting Intervals

Start this part
Part 14Intermediate

Range Queries

1 lesson6 min

Prefix Sums and Range Queries

Start this part
Part 15Intermediate

Math for DSA

1 lesson6 min

The Maths You Actually Need

Start this part
Part 16Intermediate

Heaps and Priority Queues

3 lessons18 min

Heaps and Priority Queues · Top K Problems · Heap Sort and Streaming Data

Start this part
Part 17Intermediate

Trees

3 lessons18 min

Binary Trees Explained · Binary Search Trees · Common Tree Problems

Start this part
Part 18Advanced

Tries

1 lesson6 min

Tries (Prefix Trees)

Start this part
Part 19Advanced

Graphs

4 lessons24 min

Graphs and How to Represent Them · Graph Traversal: BFS and DFS · Topological Sort

Start this part
Part 20Advanced

Union-Find

1 lesson6 min

Union-Find (Disjoint Sets)

Start this part
Part 21Advanced

Dynamic Programming

5 lessons30 min

Dynamic Programming Explained · Classic DP Problems · 1D Dynamic Programming

Start this part
Part 22Advanced

Greedy

2 lessons12 min

Greedy Algorithms Explained · Greedy vs Dynamic Programming

Start this part
Part 23Advanced

Bit Manipulation

1 lesson6 min

Bit Manipulation Basics

Start this part
Part 24Advanced

Matrix and 2D Arrays

1 lesson6 min

Matrix Traversal and Manipulation

Start this part
Part 25Advanced

Interview Patterns

2 lessons12 min

Recognising the Pattern · How to Answer in an Interview

Start this part

Your path

Learn it · Practise it · Prepare it · Prove it

  1. 01

    Learn it

    Work through the lessons

    54 lessons
  2. 02

    Practise it

    Build and debug from each lesson

    540 challenges
  3. Not available yet:
    03

    Prepare it

    Interview questions, coding and spoken

    Coming soon
  4. 04

    Prove it

    Pass the assessment, earn the certificate

    Certificate available

What You Will Learn

DSA Introduction

Big O and Complexity

Arrays

Strings

Hash Maps and Sets

Two Pointers and Sliding Window

Stacks and Queues

Linked Lists

Recursion

Practice Live

Read the lesson, edit code in the Try-It editor, and see the result instantly.

Tutorial Features

Code examples inside chapters

Each topic is supported with practical examples so the tutorial stays easy to follow.

Practice with the Try It Editor

Open the browser editor and test what you just learned without leaving the site.

Quiz for self-check

Use the assessment flow to test whether the chapters are really understood.

Certificate after completion

Complete the learning flow and move toward a verifiable completion certificate.

How to Use This Tutorial

Step 01

Learn the chapter

Follow the lessons in order so each new concept builds on the previous one.

Step 02

Practice the example

Open the Try It Editor and apply the idea immediately while it is fresh.

Step 03

Test your understanding

Use the quiz and progress flow to confirm the topic is really understood.

Step 04

Finish with proof

Complete the tutorial journey and move toward a verifiable certificate.

Complete the tutorial with confidence

Finish the lessons, take the assessment, and move toward a certificate that can be verified on the site. The structure is meant to help you learn, practice, and complete the topic properly.

Certificate verification

Publicly verifiable after successful completion.

Why Learn DSA

Easy to Learn

DSA is explained with beginner friendly chapters and examples.

Essential Skill

Understand the core building blocks before moving into advanced topics.

Build Real Websites

Use each lesson as a practical step toward real web development.