Appendix B — Java Interview Toolkit

The examples target Java 25 but intentionally use a small vocabulary. Know these operations and their semantics well enough that API recall does not consume interview attention.

B.1 Arrays and Arrays

API Use Typical cost
a.length fixed array length \(O(1)\)
Arrays.sort(int[]) ascending primitive sort \(O(n\log n)\)
Arrays.sort(T[], cmp) comparator sort \(O(n\log n)\)
Arrays.fill(a, value) initialize every cell \(O(n)\)
Arrays.asList(x,y,z) small fixed-size list view \(O(1)\) creation

int[][] is an array of int[] rows, not a special matrix type. Sorting it reorders row references. Primitive arrays compare by identity under ordinary equals, so JUnit uses assertArrayEquals.

B.2 List and ArrayList

List<E> is the interface; ArrayList<E> is a resizable array implementation.

API Meaning
add(x) append
get(i) indexed access
size() number of elements
isEmpty() no elements

Indexed access is \(O(1)\); appending is amortized \(O(1)\). Inserting/removing near the front is \(O(n)\).

B.3 Map, HashMap, and LinkedHashMap

Map<K,V> associates unique keys with values.

API Interview use
put(k,v) insert/replace
get(k) retrieve or null
containsKey(k) distinguish absent keys
getOrDefault(k,0) frequency counting
computeIfAbsent(k, f) create a collection-valued bucket
keySet() / values() iterate keys or values

HashMap has unspecified iteration order and expected \(O(1)\) lookup. LinkedHashMap adds predictable insertion order at modest overhead.

B.4 Deque, Queue, and ArrayDeque

One ArrayDeque<E> implements both stack and queue behavior.

Role Add Remove Inspect
stack push(x) pop() peek()
queue offer(x) poll() peek()

ArrayDeque rejects null. poll returns null when empty, while remove throws; pop also throws. Use an explicit emptiness check where needed.

stack (LIFO): push -> [top ... bottom] -> pop
queue (FIFO): poll <- [front ... back] <- offer

B.5 PriorityQueue

Java’s PriorityQueue exposes the smallest element under its comparator at peek()/poll(). It is not globally sorted when iterated.

API Cost
offer(x) \(O(\log n)\)
poll() \(O(\log n)\)
peek() \(O(1)\)

For a bounded top-\(k\) heap, design the comparator so the worst retained candidate is smallest and therefore removable at the root.

B.6 Comparators and lambdas

Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));

A negative comparator result means a comes before b; zero means tied for ordering; positive means after. Comparator bugs often appear only on ties, so test them explicitly.

B.7 Nested interview node classes

Neither a general ListNode nor TreeNode exists in the Java standard library. Interview platforms often supply them. Otherwise define the minimum inside the public class:

static class ListNode {
    int value;
    ListNode next;
    ListNode(int value) { this.value = value; }
}

static class TreeNode {
    int value;
    TreeNode left, right;
    TreeNode(int value) { this.value = value; }
}

Use == when the problem concerns node identity. Do not add equals/hashCode unless the contract requires value equality.

B.8 Graph representations

An adjacency list is usually the interview default for sparse graphs:

List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < vertices; i++) graph.add(new ArrayList<>());
graph.get(from).add(to);

It stores \(O(V+E)\) items and lets traversal examine only actual neighbors. A matrix uses \(O(V^2)\) memory and can be reasonable for dense graphs or constant-time edge queries.

B.9 JUnit in the examples

Each program embeds JUnit 4 tests and this entry point:

public static void main(String[] args) {
    JUnitCore.main("ClassName");
}

Frequently used assertions are assertEquals, assertArrayEquals, assertTrue, assertFalse, assertNull, and assertSame. assertSame tests object identity.

In a live interview, tests can be ordinary calls/prints if JUnit is unavailable. The important behavior is predicting cases and validating seams, not the framework.