How to learn DSA with this course

What data structures and algorithms are, why interviews ask about them, and how to attack a new problem.

Pattern: Problem solving20 min read

A college portal imports 100,000 student roll numbers from a spreadsheet. Before it saves them, it must check that no roll number appears twice.

That check is a data structures and algorithms problem. The first answer most people write works, but it is far too slow at this size. This course teaches you to see why, and to find the fast answer.

What DSA means

  • are ways to store data.
  • are the steps that work on that data.

Together, people call them DSA. You already use them every day:

You useThe structureWhat it makes fast
Contacts, sorted A to Zsorted arrayfind one name in a few steps
The browser's back buttonstackgo back to the last page
A printer's job listqueueprint in the order sent
A maps appgraphfind a short route

The difference in an interview: you choose the structure, and you explain why it is fast.

Why interviews test it

Most large tech companies start with one or two coding rounds. You get a problem and about 45 minutes. The interviewer watches how you think, not only whether the code runs:

  • Do you understand the problem before you type?
  • Can you find a slow answer, then a faster one?
  • Can you say how the time grows when the input grows?

The problems change from company to company. The patterns behind them repeat. Learn the patterns, and new problems start to look familiar.

Six steps for any problem

Use the same six steps on every problem in this course:

  1. Read it. Say the problem in your own words. Note the input, the output and the limits.
  2. Try small examples by hand. Include the edge cases: one item, all items the same, negative values.
  3. Write the brute force. Try every possibility. It is slow, but it is correct, and it shows you the work.
  4. Find the waste. Look for work that repeats. Pick a data structure that removes it.
  5. Code it. Short functions, clear names.
  6. Dry run it. Walk a small input through your code by hand, before you run it.

Steps 3 and 4 are where most learners get stuck. The next section walks all six steps on the roll number check.

One problem, start to finish

Read it

Steps 1 and 2. Here is the roll number check as a problem card. Every lesson starts with one.

Problem: Contains Duplicaten ≤ 10⁵
it asks
Return true if any value appears at least twice in the array. Return false if every value is different.
example 1
in nums = [4, 9, 2, 7, 9] out true

9 appears at index 1 and at index 4.

example 2
in nums = [1, 2, 3] out false

Every value is different.

limits
  • 1 ≤ n ≤ 10⁵
  • −10⁹ ≤ nums[i] ≤ 10⁹
budget
About 10⁸ steps run in one second. With n up to 10⁵, aim for O(n log n) or faster.
Which pattern? Decide first, then open.

“any value appears at least twice”remember the values you have seen

“n up to 10⁵”no pair check: you need about n or n log n steps

pattern: Hashing

Read the limits first. The array can hold 100,000 numbers.

The "budget" row turns that size into a speed target. The next lesson shows how. The fold at the bottom hides the pattern: guess it first, then open it.

Questions to ask before you code:

  • Can values be negative? Yes. The answer must not depend on the sign.
  • What if there is one item? Then nothing repeats, so return false.
  • What do I return? Only true or false, not where the duplicate is.

Brute force: compare every pair

Step 3. The first idea anyone tries. Pointer i picks one item. Pointer j checks it against every item after it.

brute force: every paircomparisons 1
4
0
9
1
2
2
7
3
9
4

1/7Compare 4 with 9. Different.

Two loops: for each item, check every item after it.
boolean hasDuplicate(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] == nums[j]) return true;
        }
    }
    return false;
}

Count the cost out loud:

  • The first item is compared with n − 1 others. The next with n − 2. And so on.
  • That adds up to about n × n / 2 pairs.
  • For 100,000 numbers with no duplicate, that is about 5 billion comparisons.

The code is correct. It is far over the budget.

Find the waste

Step 4. For every item, the brute force asks: "does this value appear again?" To answer, it walks the rest of the array. Those walks cover the same items over and over.

There are two ways to stop that repeated walk:

  • Sort first, so equal values sit side by side.
  • Remember what you have seen, in a structure that answers in one step.

Better: sort first

After a sort, equal values touch. One walk that compares neighbours finds them.

Sorting puts the two 9s next to each other.
boolean hasDuplicate(int[] nums) {
    int[] sorted = nums.clone();
    Arrays.sort(sorted);
    for (int i = 1; i < sorted.length; i++) {
        if (sorted[i] == sorted[i - 1]) return true;
    }
    return false;
}
  • Each version sorts a copy, so the input keeps its order: clone() in Java, sorted() in Python, toSorted() in TypeScript.
  • In TypeScript, always pass (a, b) => a - b for numbers. Without it, JavaScript sorts numbers as text, and [10, 9, 1] becomes [1, 10, 9].
  • A sort costs about n × log n steps. For 100,000 numbers, that is about 1.7 million. It fits.

Best: remember what you have seen

Steps 5 and 6. Flip the question to "have I seen this value before?" Keep the values you have read in a . For each new value, ask the set first.

one walk with a Setlookups 1
4
0
9
1
2
2
7
3
9
4
seen{4}

1/5Read 4. It is not in the set, so add it.

One pointer, one pass. The set grows as the pointer moves.
boolean hasDuplicate(int[] nums) {
    Set<Integer> seen = new HashSet<>();
    for (int x : nums) {
        if (seen.contains(x)) return true;
        seen.add(x);
    }
    return false;
}
  • seen holds every value before the current one (a HashSet in Java, a set in Python, a Set in TypeScript). That stays true at every step.
  • So when seen.has(x) is true, x appeared earlier. That is the duplicate.
  • Each value costs one lookup. For 100,000 numbers, that is at most 100,000 lookups.

You may know the one-line version: len(set(nums)) != len(nums) in Python, or new Set(nums).size !== nums.length in TypeScript. It works too, but it always reads the whole array, even when the duplicate is at the start.

Side by side

ApproachThe ideaTime grows likeExtra memoryn = 100,000
Compare every pairtwo loopsn × nnoneabout 5 billion comparisons
Sort, check neighboursequal values touchn × log na sorted copyabout 1.7 million steps
Remember in a Setone passnthe set100,000 lookups

In the next lesson, the "time grows like" column gets its real name: .

How this course works

Three parts, 106 lessons

PartLessonsWhat you learn
Beginner31read problems, measure answers, the basic moves
Moderate32core patterns: two pointers, sliding window
Advanced43heaps, trees, graphs, dynamic programming

Beginner covers arrays, sorting, binary search, recursion and linked lists. The problems come from Striver's A2Z sheet and his 180 patterns sheet.

One path in every lesson

Every lesson walks the same path, from slow to fast.

This is the path you walked above. After the optimal answer, each lesson shows how to spot the pattern in new problems, then ends with Striver's video and a practice list.

Practice

The practice list sits under each lesson. Tick a problem when you solve it.

  • "must do" marks a problem that is also on Striver's 180 patterns sheet. Solve those first.
  • Give each problem 20 minutes before you look at the answer. The struggle is where the pattern sticks.

For each problem, Striver suggests this loop:

  1. Write your own brute force, even a slow one, and submit it.
  2. If you are stuck, read the solution or watch the video.
  3. Write a short note: the pattern and the key insight.
  4. A few days later, solve it again without looking.

Striver's videos

Each lesson links one video from Striver's channel, take U forward. Watch it when a step is unclear. In this one, he explains how to get the most from his sheet.

take U forwardYouTube
Don't watch my A2Z DSA Course: Striver introduces the sheet
Remember4 points
  1. 01

    DSA means how you store data, and the steps that work on it. Interviews test whether you can pick the right one and say why.

  2. 02

    Attack every problem in six steps: read it, try examples, brute force, find the waste, code, dry run.

  3. 03

    Write the brute force first. Then find the work it repeats, and pick a structure that removes it.

  4. 04

    A Set answers "have I seen this?" in one step, so the duplicate check drops from about n × n / 2 comparisons to n lookups.

Read more

Next: time and space complexity, the yardstick for every answer in this course.