You have 45 minutes and a whiteboard. Be ready.
← and someone watching every line you writePractice Java coding interviews with a repeatable method: clarify the problem, start with brute force, optimize, test edge cases, and explain complexity. Explore 48 worked questions and 117 answered follow-ups in The Code Interview, Second Edition.
Not sure yet? Read Chapter 1 free first →
Build confidence for your next Java coding interview.
A method you can use in the room
Knowing algorithms is half the job. The book teaches the six steps interviewers look for, then applies them to every problem.
- Listen and clarifyInput size, nulls, duplicates. Never code on assumptions.
- Work an exampleA small, non-trivial case on the board shows the traps.
- Say the brute forceA correct slow answer, with its Big O, is your baseline.
- OptimizeHash table, two pointers, stack, heap: remove the repeated work.
- Write clean codeGood names, small methods, and talk while you write.
- Test and analyzeTrace your example, then the edge cases, then state the complexity.
Look inside question 1.6
Items in closed compartments
Count the items (*) between walls (|) for up to 100,000 range queries. Scanning each range is O(n × m) and times out. The book shows why, then builds three arrays once, so every query is a subtraction.
each query: O(1) instead of O(n) ✓
| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| s | | | * | * | | | * | | | * |
| sum | 0 | 1 | 2 | 2 | 3 | 3 | 4 |
query (1, 6): sum[5] − sum[0] = 3
Get all of Chapter 1 free
Question 1.6 is one of eight in the first chapter. The free sample has the whole chapter: the fundamentals of arrays and strings, then eight interview questions worked with the full method, from clarifying questions and a brute-force solution to the optimal one and its complexity.
Try one on paper before you read the answer. If the method clicks, the other 40 questions are waiting in the book.
Liked it? Get the full PDF for $15, delivered to your inbox right after payment.
- 1.1Reverse a Text
- 1.2Validate If a String Has All Unique Characters
- 1.3Validate If a String Is a Palindrome
- 1.4Compare Application Version Numbers
- 1.5Rotate a Matrix by 90 Degrees
- 1.6Items in Closed Compartments← above
- 1.7DHL Locker Locations
- 1.8Largest Sibling of a Number
What's on the board
- 1Arrays and Strings8 questions
- 2Lists and Linked Lists4 questions
- 3Math and Logic Puzzles10 questions
- 4Recursion4 questions
- 5Sorting and Searching7 questions
- 6Stacks and Queues3 questions
- 7Hash Table3 questions
- 8Trees2 questions
- 9Graphs3 questions
- 10Coding Challenges4 questions
Who it's for
New in the Second Edition
- Every question now goes from brute force to optimal, with every variable explained.
- Walkthroughs traced step by step, with values checked by running the code.
- 117 follow-up questions, 48 clarifying-question sections, and 48 interview tips.
- More than 30 corrections, including overflow bugs and an infinite loop, fixed and retested.
The author
Quick answers
Which language are the solutions in?
Java, with JUnit tests. The ideas carry over to any language.
Paperback or PDF?
The paperback is on Amazon. The PDF is the same book as a file you can read and search on any screen, emailed to you right after payment.
How long does it take?
Pick a plan: one weekend for the essentials, two weeks for one chapter a day, or four weeks for everything including the follow-ups.
Is it for beginners or seniors?
New to coding interviews? Start with foundations and step-by-step walkthroughs.
Experienced but rusty? Refresh the patterns, optimization decisions, and follow-up questions.
Walk in prepared. Walk out with the offer.
Ready for the job after the interview too? Get this book and Software That Can Change as PDFs for $25, $5 less than buying them separately.