Java Interview Practice Test: Idioms, Pitfalls and Theory
The final Java interview practice test: 15 questions and three classic programs — two-sum, a min-stack and anagram grouping — with pitfalls and theory review.
- Course: Java study plan
- Module: Interview idioms
- Kind: Checkpoint — cleared at 70%
- Reading time: 32 min
- Runtime: OpenJDK 18
Final checkpoint — Interview idioms is the checkpoint that closes the Interview idioms module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
The last checkpoint of the Java plan. It covers the interview template and complexity budget, the equals/hashCode/Comparable contracts, the thirty pitfalls, the collections idiom sheet, clean-solution habits, and the theory drill — and, because it is the last one, it reaches back across the whole track.
How it works. Fifteen questions and three programs; 70% on the questions and every program accepted clears the module — and, with every other module complete, the plan.
Before you start, make sure you can answer:
- What complexity
n ≤ 10⁵allows, and whereintoverflows in a typical solution. - Why overriding
equalswithouthashCodebreaksHashMap, and why keys must be immutable. -7 % 3,Integer.valueOf(128) == Integer.valueOf(128),"a.b".split("."), and theArrays.sort(int[])worst case.- The idiom for top-k frequent, sliding-window maximum, subarray sums and anagram groups.
- How to structure a solution so its correctness is visible, and what to say about complexity.
- Overload versus override resolution — and why fields are never polymorphic.
The programs are the classics interviewers actually set: two-sum with a hash map and pair counting, a min-stack with O(1) operations, and anagram grouping — each with the idiom sheet's structure and the pitfalls list applied.
Common questions
What time complexity does n ≤ 10⁵ allow?
O(n log n) or better. Java does about 10⁸ simple operations per second, so an O(n²) solution would need 10¹⁰ operations and miss the time limit; think sorting, hashing, heaps or binary search.
What does "a.b".split(".") return in Java?
An empty array. split treats its argument as a regular expression, . matches every character, and the empty strings between the separators are all dropped as trailing empties. Use split(Pattern.quote(".")) instead.
How do you implement a min-stack with O(1) operations in Java?
Keep a second stack beside the values: each push also pushes the smaller of the new value and the current minimum, and each pop removes from both. The minimum is then a peek on the second stack, so push, pop and get-minimum are all O(1).
Exercises
Two sum, both ways
Read an integer n, n integers and a target t. Print pair=<i> <j> for the first pair found scanning left to right with a hash map from value to its first index — that is, the smallest j such that t - a[j] appeared earlier, with i the first index of that value — or pair=none. Then print pairs=<number of index pairs i < j with a[i] + a[j] = t>, computed in O(n) from a frequency map (handle the case where both values are equal).
Example: 6 then 3 5 2 5 1 4 then 6 →
pair=1 4
pairs=3
(The 1 at index 4 completes the 5 first seen at index 1. The pairs summing to 6 are (1,4), (3,4) and (2,5).)
Min-stack
Implement a stack with O(1) push, pop, top and min using two ArrayDeques (values, and the running minimums). Read an integer n and n commands: push <v>; pop (prints nothing, or empty if there is nothing to pop); top prints the top value or empty; min prints the current minimum or empty.
Example: 7 then push 5, push 2, push 7, min, pop, pop, min →
2
5Group anagrams
Read an integer n and n lower-case words. Group words that are anagrams of each other (key: the word's characters sorted). Print the groups ordered by size descending, then by their first word alphabetically, one per line as <size>: <words in input order, space-separated>.
Example: 6 then eat tea tan ate nat bat →
3: eat tea ate
2: tan nat
1: batIn this module: Interview idioms
- The interview template — input, output, limits
- equals, hashCode and Comparable — the contracts that make collections work
- The pitfalls that fail interviews
- The collections idiom sheet
- Writing clean solutions under time pressure
- The Java theory drill — thirty questions, thirty answers
- Final checkpoint — Interview idioms (this lesson)