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 use | The structure | What it makes fast |
|---|---|---|
| Contacts, sorted A to Z | sorted array | find one name in a few steps |
| The browser's back button | stack | go back to the last page |
| A printer's job list | queue | print in the order sent |
| A maps app | graph | find 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:
- Read it. Say the problem in your own words. Note the input, the output and the limits.
- Try small examples by hand. Include the edge cases: one item, all items the same, negative values.
- Write the brute force. Try every possibility. It is slow, but it is correct, and it shows you the work.
- Find the waste. Look for work that repeats. Pick a data structure that removes it.
- Code it. Short functions, clear names.
- 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.
9 appears at index 1 and at index 4.
Every value is different.
- 1 ≤ n ≤ 10⁵
- −10⁹ ≤ nums[i] ≤ 10⁹
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
trueorfalse, 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.
1/7Compare 4 with 9. Different.
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.
┌────────────────────────────────────────────┐ │ │ │ before 4 9 2 7 9 │ │ ───────────── │ │ sorted 2 4 7 9 9 │ │ └──┘ │ │ same │ │ │ └────────────────────────────────────────────┘
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 - bfor 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.
1/5Read 4. It is not in the set, so add it.
boolean hasDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) {
if (seen.contains(x)) return true;
seen.add(x);
}
return false;
}seenholds every value before the current one (aHashSetin Java, asetin Python, aSetin TypeScript). That stays true at every step.- So when
seen.has(x)is true,xappeared 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
| Approach | The idea | Time grows like | Extra memory | n = 100,000 |
|---|---|---|---|---|
| Compare every pair | two loops | n × n | none | about 5 billion comparisons |
| Sort, check neighbours | equal values touch | n × log n | a sorted copy | about 1.7 million steps |
| Remember in a Set | one pass | n | the set | 100,000 lookups |
In the next lesson, the "time grows like" column gets its real name: .
How this course works
Three parts, 106 lessons
| Part | Lessons | What you learn |
|---|---|---|
| Beginner | 31 | read problems, measure answers, the basic moves |
| Moderate | 32 | core patterns: two pointers, sliding window |
| Advanced | 43 | heaps, 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
┌──────────────────────────────────────────────┐ │ │ │ 1 read the problem what it asks │ │ ▼ │ │ 2 spot the pattern which idea fits │ │ ▼ │ │ 3 brute force slow but correct │ │ ▼ │ │ 4 key insight the repeated work │ │ ▼ │ │ 5 optimal fast and correct │ │ │ └──────────────────────────────────────────────┘
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:
- Write your own brute force, even a slow one, and submit it.
- If you are stuck, read the solution or watch the video.
- Write a short note: the pattern and the key insight.
- 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.
- 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.
- 02
Attack every problem in six steps: read it, try examples, brute force, find the waste, code, dry run.
- 03
Write the brute force first. Then find the work it repeats, and pick a structure that removes it.
- 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
- Striver's A2Z DSA sheet and 180 patterns sheet, the sources of the practice lists and the "must do" marks
- How I cut GTA Online loading times by 70%, the full story of the slow duplicate check
- Coding interview prep from the Tech Interview Handbook
- Set on MDN, the structure that won in this lesson
Next: time and space complexity, the yardstick for every answer in this course.