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

@Transactional: The 5 Traps That Silently Lose Your Data

@Transactional is the most trusted annotation in Spring Boot — and the most quietly misunderstood. It looks like one annotation doing one job: wrap this method in a transaction. In practice it is a proxy, a set of defaults inherited from EJB, and a handful of hints to Hibernate, and each of those has a sharp edge. None of the five traps below throws an error at the point where you make the mistake. They all fail silently, in production, weeks later. ...

2026-11-07 · 13 min · Ramesh

ScopedValue: The ThreadLocal Replacement Java 25 Finalized

In the ThreadLocal leak post, ScopedValue was the third fix — two paragraphs and a filter snippet. This is the full treatment, because JDK 25 changed its status: after an incubator round in JDK 20 and previews in JDK 21–24, JEP 506 made java.lang.ScopedValue final in JDK 25. No preview flag. No incubator module. It is now part of the platform. First, the honest framing. “Replacement” comes with an asterisk: JEP 506 explicitly does not deprecate ThreadLocal, and migration is not a goal for every use. ScopedValue replaces the dominant request-context pattern: bind an immutable value at an entry point, let callees read it, and guarantee it disappears when that operation ends. It does not replace per-thread mutable caches, accumulators that callees update for a caller, or values that must outlive the method that created them. That distinction is the whole article. Get it right and ScopedValue removes a bug class. Treat it as a nicer global variable and you will recreate the ThreadLocal mess with newer syntax. ...

2026-10-23 · 15 min · Ramesh