DSA in Java roadmap: what to learn, in what order
11 min readUpdated
Data structures and algorithms decide most coding rounds, and Java is one of the best languages to learn them in. This roadmap gives you an order that builds each topic on the last, the patterns that solve most problems, and a practice routine that works.
Before you start
You need core Java first: loops, methods, arrays, strings, classes and the main collections (ArrayList, HashMap, HashSet, ArrayDeque, PriorityQueue). If those still feel new, finish them first; DSA practice goes much faster afterwards.
The order to learn in
| Step | Topic | You are ready to move on when… |
|---|---|---|
| 1 | Complexity (Big-O), how to approach a problem | You can state the time and space of your own code |
| 2 | Arrays and strings | You solve easy array and string problems without hints |
| 3 | Hashing (HashMap, HashSet) | You reach for a map to remove a nested loop |
| 4 | Two pointers and sliding window | You recognise "subarray" and "substring" problems |
| 5 | Prefix sums, binary search (also on the answer) | You can write a bug-free binary search |
| 6 | Recursion and backtracking | You can draw the recursion tree for subsets and permutations |
| 7 | Linked lists, stacks, queues, monotonic stack | You reverse a list and solve bracket problems easily |
| 8 | Trees, BST, heaps | You can write DFS and BFS on a tree from memory |
| 9 | Graphs: BFS, DFS, topological sort, Dijkstra, union-find | You can model a problem as a graph |
| 10 | Greedy and intervals | You can explain why a greedy choice is safe |
| 11 | Dynamic programming (1D, then 2D) | You can go from recursion to memoisation to a table |
Patterns that solve most problems
Most interview problems are variations of a small number of patterns. Learn to spot the pattern from the problem statement, and new problems stop feeling new.
| If the problem says… | Try |
|---|---|
| Sorted array, pair or triplet with a sum | Two pointers |
| Longest or shortest subarray or substring with a condition | Sliding window |
| Count subarrays with sum k, or range sums | Prefix sums with a HashMap |
| Find a value or the smallest answer that works | Binary search (on the array or on the answer) |
| All combinations, subsets or permutations | Backtracking |
| Next greater or smaller element | Monotonic stack |
| Top K, K-th largest, merge K sorted | Heap (PriorityQueue) |
| Shortest path in an unweighted grid or graph | BFS |
| Order of tasks with dependencies | Topological sort |
| Count ways, or best value with choices | Dynamic programming |
A pattern in Java: sliding window
The longest substring without repeating characters is a classic. A window grows to the right and shrinks from the left whenever a character repeats; a map remembers where each character was last seen, so the whole string is scanned once, O(n).
import java.util.HashMap;
import java.util.Map;
public class LongestUniqueSubstring {
static int longest(String s) {
Map<Character, Integer> lastSeen = new HashMap<>();
int best = 0;
int left = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (lastSeen.containsKey(c) && lastSeen.get(c) >= left) {
left = lastSeen.get(c) + 1; // jump past the repeated character
}
lastSeen.put(c, right);
best = Math.max(best, right - left + 1);
}
return best;
}
public static void main(String[] args) {
System.out.println(longest("abcabcbb"));
System.out.println(longest("bbbbb"));
System.out.println(longest("pwwkew"));
System.out.println(longest(""));
}
}Output
3 1 3 0
Java tools you will use every day
| Need | Java class | Watch out for |
|---|---|---|
| Dynamic array | ArrayList | remove(int) versus remove(Object) |
| Counting, lookups | HashMap, HashSet | Use getOrDefault and merge |
| Stack and queue | ArrayDeque | Do not use the old Stack class |
| Heap | PriorityQueue | Min-heap by default; Collections.reverseOrder() for max |
| Sorted keys, floor and ceiling | TreeMap, TreeSet | O(log n) per operation |
| Sorting | Arrays.sort, list.sort | Comparators with Integer.compare, never a - b |
| Big numbers | long, BigInteger | int overflows past about 2.1 billion |
| Fast text building | StringBuilder | Never += in a loop |
How to practise
- Learn a pattern, then solve 5 to 10 problems of that pattern in a row.
- Try for 20 to 30 minutes before looking at a hint. Then read the idea, close it and write the code yourself.
- Write down the pattern, the key idea and the complexity for every problem in a notebook or a file in your repository.
- Revisit problems you could not solve after a week, without looking at your old code.
- Mix patterns after the first pass, because in interviews nobody tells you which pattern it is.
- Time yourself in the last weeks: 20 minutes for easy, 35 to 40 for medium.
Two or three problems a day for four to six months beats ten a day for two weeks. Consistency is what builds pattern recognition.
Common mistakes
- Reading solutions instead of writing code. You only learn when you type it, run it and fix it.
- Skipping complexity. Every interviewer asks "what is the time complexity?"
- Jumping to dynamic programming before recursion is comfortable.
- Solving only easy problems. Most coding rounds ask at least one medium.
- Ignoring edge cases: empty input, one element, duplicates, negative numbers, overflow.
Frequently asked questions
How long does it take to learn DSA in Java?
With one to two hours a day, most learners cover the core topics in four to six months. Graphs and dynamic programming take the longest; keep practising them after the first pass.
Is Java good for DSA?
Yes. Its collections (HashMap, ArrayDeque, PriorityQueue, TreeMap) cover almost every data structure you need, and it is accepted in nearly every coding round.
How many problems should I solve?
Quality matters more than count. Around 250 to 300 well-understood problems across all patterns is enough for most interviews.
Should I learn DSA before Spring Boot?
Start DSA right after core Java and keep practising while you learn Spring Boot. Coding rounds come first in most hiring processes.
Learn it step by step
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.