DEV Community

henry
henry

Posted on

Coding Interview Patterns and the Sliding Window Invariant

Recognizing a coding interview pattern is useful only when you can explain why the pattern applies. Sliding window is a good example. It is tempting to see an array or string and start moving two pointers, but the important decision is what those pointers represent.

Consider this practice question: find the length of the longest contiguous substring with no repeated characters. For the string abcaef, the answer is five, corresponding to bcaef. The problem gives you a window whose validity you can test and restore as you move through the input

Worked example using the string abcaef. The longest substring without repeated characters is bcaef.

State what must remain true

Let the left and right positions define the current substring. After processing each new character and repairing the window, every character inside that substring must be unique. That statement is the invariant: the property you preserve as the algorithm progresses.

Read the characters from left to right. The window grows from a to ab to abc. When the next a arrives, keeping the original a would create a duplicate. Move the left boundary past that earlier a. The valid window becomes bca. Adding e and f then produces bcaef.

Notice what you did not do. You did not restart the entire search after seeing a duplicate. Earlier positions that would still include the repeated a cannot produce a valid window ending at the current position. Moving the boundary discards those possibilities for a reason.

Choose an implementation you can explain

One approach uses a set of characters. When a new character is already present, repeatedly remove the leftmost character and advance the left boundary until the duplicate has been removed. Then add the new character and update the best length.

Another approach records the most recent index of each character. On a repeat, move the left boundary to the larger of its current value and one position after the previous occurrence. That maximum matters. A character that appeared before the current window must not pull the boundary backward.

Both approaches require assumptions about character comparison and indexing. For a basic practice problem, ordinary character units may be sufficient. If the question concerns user-visible Unicode characters, clarify the required unit instead of silently assuming that every language handles strings identically.

Derive the running time from movement

In the set-based approach, a nested loop does not automatically mean quadratic time. The right boundary advances across the string once. The left boundary also advances at most across the string once. With average constant-time hash-set operations, the total work is linear in the input length.

Space depends on the maximum number of distinct characters held in the window. State that relationship before claiming constant space. A fixed, bounded alphabet and an unrestricted character set are different assumptions.

The PhantomCodeAI coding interviews hub includes pattern-based preparation. When using a guide or an AI hint, ask it to explain the invariant before showing an implementation. That makes the exercise transferable to unfamiliar wording.

Learn when the pattern stops working

The same boundary movement does not automatically solve every contiguous-subarray problem. For example, reasoning that extending a window always increases its sum depends on the values being nonnegative. Negative numbers can break that reasoning.

Finish your practice by testing an empty string, a string of identical characters, a string with all unique characters, and a repeat that occurs outside the current window. Then explain the algorithm without naming the pattern. If you can justify each boundary movement, you understand more than its label.

Top comments (0)