Sliding Window: The Pattern Behind Every Substring Question

The two-pointer playbook introduced the sliding window as one pattern among five. This post gives it the full treatment, because it is the pattern interviewers escalate through: the Easy fixed-size version, the Medium variable-size version, and the Hard frequency-constrained version are the same loop with a different constraint. Learn the loop once and the escalation is just a harder rule to maintain. The loop is: a window [l, r] where r expands every step and l shrinks only until the window satisfies the constraint again. The window is always valid when the shrink loop finishes — that invariant is the whole technique. And it is O(n) for a reason you can state in one sentence: l and r only ever move right, so each index enters the window once and leaves it once. No index is ever revisited, no matter how nested the code looks. I checked the bound empirically: across 2,000 random strings, the total shrink steps in the longest-substring loop never exceeded n. ...

2026-11-15 · 11 min · Ramesh

Cyclic Sort: The O(n) Trick Behind "Find the Missing Number"

Cyclic sort is the highest-ROI pattern you’ve never been taught by name. The entire technique is one invariant: when the values come from a known range, every value belongs at its own index — so put it there. Value v goes to index v (for ranges starting at 0) or index v - 1 (for ranges starting at 1). After one placement pass, whatever sits at the wrong index is the answer: the missing number, the duplicate, the first gap. ...

2026-11-13 · 11 min · Ramesh

Minimum Spanning Trees in Java Interviews: Kruskal vs Prim, and When the Interviewer Asks for the Second-Best

The setup is four cities and six possible roads, each with a cost. Connect all four cities so that every city is reachable from every other, and spend as little as possible. The answer is always exactly n-1 roads — fewer can’t connect everything, more must contain a cycle, and a cycle means you’re paying for a road you don’t need. A minimum spanning tree is the cheapest such set. ...

2026-11-09 · 13 min · Ramesh

Floyd's Tortoise and Hare: Linked List Cycle Detection in Java, With the Proof

“Detect a cycle in a linked list.” Everyone recites it: slow moves one step, fast moves two, if they meet there’s a cycle. Then the interviewer says “why can’t fast skip over slow?” and the room goes quiet. Then “find where the cycle starts” — and the memorized answer runs out. Floyd’s tortoise and hare is a two-minute algorithm with a twenty-minute understanding. Here’s the understanding. The shallow answer (and why interviewers hate it) The HashSet version works and every candidate knows it: ...

2026-10-17 · 6 min · Ramesh

HashMap Internals: The Questions That Expose Shallow Knowledge

“How does HashMap work?” opens half of all Java interviews. Most candidates answer “array of linked lists, O(1) lookup” — and then the follow-ups start, and the room gets quiet. The interviewers aren’t testing whether you’ve memorized the structure. They’re testing whether you know why it’s built the way it is. Here are the questions that expose shallow knowledge, with the answers that don’t. Question 1: Why does HashMap mangle your hashCode? Everyone knows hashCode() feeds the map. Almost nobody asks what HashMap does to it first: ...

2026-10-13 · 8 min · Ramesh