Skip to main content

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

StepTopicYou are ready to move on when…
1Complexity (Big-O), how to approach a problemYou can state the time and space of your own code
2Arrays and stringsYou solve easy array and string problems without hints
3Hashing (HashMap, HashSet)You reach for a map to remove a nested loop
4Two pointers and sliding windowYou recognise "subarray" and "substring" problems
5Prefix sums, binary search (also on the answer)You can write a bug-free binary search
6Recursion and backtrackingYou can draw the recursion tree for subsets and permutations
7Linked lists, stacks, queues, monotonic stackYou reverse a list and solve bracket problems easily
8Trees, BST, heapsYou can write DFS and BFS on a tree from memory
9Graphs: BFS, DFS, topological sort, Dijkstra, union-findYou can model a problem as a graph
10Greedy and intervalsYou can explain why a greedy choice is safe
11Dynamic 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 sumTwo pointers
Longest or shortest subarray or substring with a conditionSliding window
Count subarrays with sum k, or range sumsPrefix sums with a HashMap
Find a value or the smallest answer that worksBinary search (on the array or on the answer)
All combinations, subsets or permutationsBacktracking
Next greater or smaller elementMonotonic stack
Top K, K-th largest, merge K sortedHeap (PriorityQueue)
Shortest path in an unweighted grid or graphBFS
Order of tasks with dependenciesTopological sort
Count ways, or best value with choicesDynamic 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).

Sliding window with a HashMap
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

NeedJava classWatch out for
Dynamic arrayArrayListremove(int) versus remove(Object)
Counting, lookupsHashMap, HashSetUse getOrDefault and merge
Stack and queueArrayDequeDo not use the old Stack class
HeapPriorityQueueMin-heap by default; Collections.reverseOrder() for max
Sorted keys, floor and ceilingTreeMap, TreeSetO(log n) per operation
SortingArrays.sort, list.sortComparators with Integer.compare, never a - b
Big numberslong, BigIntegerint overflows past about 2.1 billion
Fast text buildingStringBuilderNever += in a loop

How to practise

  1. Learn a pattern, then solve 5 to 10 problems of that pattern in a row.
  2. Try for 20 to 30 minutes before looking at a hint. Then read the idea, close it and write the code yourself.
  3. Write down the pattern, the key idea and the complexity for every problem in a notebook or a file in your repository.
  4. Revisit problems you could not solve after a week, without looking at your old code.
  5. Mix patterns after the first pass, because in interviews nobody tells you which pattern it is.
  6. 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.

Try it yourself

Write and run Java code in the free online compiler, or press Visualize to watch it run line by line. Nothing to install.

Practise this, step by step

Short lessons with quizzes, practice and interview questions, from your first program to job-ready.

Data Structures and Algorithms course: syllabus and 1:1 mentoringComplexity, arrays, lists, trees, graphs, sorting, greedy and dynamic programming.