It’s been a year, maybe three, since you last solved a coding puzzle. Your day job is Spring Boot, Kafka and pull requests. Now there’s an interview on the calendar, and the coding round is the part that worries you.
So you open a problem site, pick something that looks easy, and stare at it. You know Java. You’ve used HashMap a thousand times. But turning a puzzle into code under a timer feels like a different skill, and in a way it is.
The fix is not to grind random problems. It’s to solve the right problems in the right order, so that each one hands you a tool you need for the next. That’s what this series is.
Why the order matters
Most interview problems are built from a small set of moves:
- walk an array with a loop, or with two pointers
- count things with an array or a
HashMap - remember what you’ve seen with a
HashSet - sort with a
Comparator - use a stack or queue when order of processing matters
- do all of the above in one line with streams
If you jump straight to “group anagrams” or “top K frequent elements”, you need three of those moves at once, and you get stuck. If you’ve already practised each move on its own, the hard problem is just a combination of things you’ve done.
“Hard interview problems are easy problems stacked on top of each other. Learn the easy ones properly first.”
The same goes for streams. Streams are much easier once you’ve solved the same problem with a loop. You already know what the code must do, so you only have to learn how to say it in stream form. That’s why streams come last in this plan, not first.
The plan: 8 phases, 50 problems
| Phase | Topic | Problems | What you practise |
|---|---|---|---|
| 1 | Arrays and core Java | 1–7 | loops, two pointers, swap, modulo, sum and XOR tricks |
| 2 | Strings | 8–11 | counting arrays, HashMap, HashSet, LinkedHashMap |
| 3 | ArrayList and Set | 12–15 | List vs Set, retainAll, addAll, sorting a list |
| 4 | HashMap | 16–18 | getOrDefault, merge, entrySet, computeIfAbsent |
| 5 | Sorting with Comparator | 19–22 | Comparator.comparing, reversed, thenComparing, sort a map by value |
| 6 | Stack, queue and deque | 23–25 | ArrayDeque as a stack and as a queue |
| 7 | Streams on an Employee list | 26–32 | filter, map, sorted, limit, average, groupingBy |
| 8 | Stream puzzles | 33–50 | counting, partitioningBy, toMap, flatMap, joining and the traps |
Phase 1 to 6 are the “core” of a coding round. Phase 7 and 8 are what Java interviewers in India and elsewhere love to ask in the first 15 minutes: “Here’s a list of employees. Give me the highest salary in each department, using streams.”
The 50 problems at a glance
Phase 1, arrays: 1. max and min · 2. reverse an array · 3. second largest element · 4. remove duplicates from a sorted array · 5. move zeros to the end · 6. rotate an array by K · 7. find the missing number
Phase 2, strings: 8. anagram check · 9. first non-repeating character · 10. first repeating character · 11. character frequency
Phase 3, lists and sets: 12. remove duplicates from a list · 13. common elements of two lists · 14. union of two lists · 15. sort a list (and reverse it)
Phase 4, HashMap: 16. word frequency · 17. most frequent element · 18. group anagrams
Phase 5, Comparator: 19. sort employees by salary · 20. by salary, descending · 21. by salary, then age · 22. sort a map by value
Phase 6, stack and queue: 23. valid parentheses · 24. reverse a queue · 25. binary numbers from 1 to N
Phase 7, streams: 26. salary above 10 lakh · 27. employee names · 28. sort by salary · 29. top 3 earners · 30. average salary · 31. group by department · 32. highest salary per department
Phase 8, stream puzzles: 33. find duplicates · 34. first non-repeating character · 35. word count sorted by frequency · 36. top K frequent · 37. second and Nth highest salary · 38. count per department · 39. average salary per department · 40. biggest department · 41. top earner’s name per department · 42. names per department as text · 43. partition by salary · 44. flatten a list of lists · 45. toMap with duplicate keys · 46. reverse words · 47. group anagrams with streams · 48. sum of squares with reduce · 49. palindrome with IntStream · 50. longest word
How to practise each problem
Reading a solution feels like learning, but it isn’t. Use this loop for every problem:
- Read the problem and say the plan out loud in one or two sentences, before any code. “I’ll count each character in a map, then walk the string again and return the first one with count 1.” Interviewers grade this part as much as the code.
- Write it by hand or in a plain editor, without autocomplete. In many interviews you get a shared doc, not an IDE.
- Run it on the example, then on the edge cases: an empty array, one element, all duplicates, negative numbers.
- Say the time and space cost. “One pass, so O(n) time; the map holds at most 26 keys, so O(1) extra space.”
- Only then read the solution in the article, and compare. Did you miss an edge case? Was there a simpler move?
A daily schedule that fits around a job
If you have one to two hours a day, this plan takes about two weeks:
| Day | Do |
|---|---|
| 1–2 | Phase 1, arrays (7 problems) |
| 3 | Phase 2, strings (4 problems) |
| 4 | Phase 3, lists and sets (4 problems) |
| 5 | Phase 4, HashMap (3 problems, group anagrams twice) |
| 6 | Phase 5, Comparator (4 problems) |
| 7 | Phase 6, stack and queue (3 problems), then redo any problem from days 1–6 that you got wrong |
| 8–9 | Phase 7, streams on employees (7 problems) |
| 10–12 | Phase 8, stream puzzles (18 problems, about 6 a day) |
| 13–14 | Redo the whole list from memory, timing yourself at 10 minutes a problem |
The last two days matter most. Solving a problem once is learning. Solving it again from a blank page three days later is what sticks.
What you need to know before you start
Just basic Java: variables, if, for loops, methods, arrays and what a class is. Every collection and stream method is explained the first time it’s used.
The code uses Java 17 or newer (Java 21 is fine). The only “new” things you’ll see are var in a couple of places, record for the Employee type, and .toList() on streams. If your interview is on Java 8, the articles show the older form too.
Start here
Open Phase 1: Java array interview programs and solve problem 1 before you read its solution. It’s the easiest problem in the series, on purpose. You’re not warming up your knowledge of arrays. You’re warming up the habit of plan, code, test, explain, and that’s the habit the interview is really checking.
Comments