Hello! Welcome to your personalized interview preparation course.
Given your goal of targeting Staff and Senior Staff software engineering roles at companies like Google and Uber, this course is designed to go beyond basic problem-solving. We'll focus on recognizing algorithmic patterns and system design principles at a level that demonstrates deep technical expertise and strategic thinking.
This first module, "Advanced Algorithm Patterns," will help you refresh and deepen your understanding of common patterns that unlock entire categories of interview problems.
Introduction
In this first lesson, we'll dive into the Sliding Window pattern. This is a powerful technique for solving problems that involve finding an optimal subarray or substring within a larger array or string. Mastering this pattern allows you to transform an inefficient brute-force solution, which typically has a time complexity of , into a much more efficient linear-time solution, . For a senior candidate, demonstrating this kind of optimization is crucial.
Our goal today is to:
- Understand the core concept of a sliding window.
- Differentiate between fixed-size and variable-size windows.
- Learn a systematic template for solving variable-size window problems.
- Apply this pattern to several classic interview questions.
Let's begin.
What is the Sliding Window Pattern?
At its heart, the sliding window is a "mental model" for processing linear data structures like arrays and strings. Instead of generating every possible subarray and checking it against a condition (the brute-force approach), we maintain a single "window" (a sub-array or sub-string) and slide it across the data. By cleverly adding elements to one end and removing them from the other, we can evaluate all relevant windows in a single pass.
To get a high-level overview of this concept and why it's so much more efficient than brute force, please watch the first few minutes of the following video.
Sliding Window Technique - Algorithmic Mental Models
This video from the channel Ryan Schachte introduces the sliding window as an 'algorithmic mental model' and explains its core benefit in reducing time complexity.
Please watch from the beginning until 06:49. Focus on the conceptual difference between the sliding window and the brute-force approach, and the explanation of how it achieves O(N) complexity.
As the video explained, the key is avoiding redundant work. We don't re-calculate sums or properties for the entire window on each slide; we just update the state based on the element that enters and the one that leaves.
There are two primary flavors of this pattern.
1. Fixed-Size Sliding Window
This is the simplest variant, where the window size, , is fixed. These problems often ask for something like the "maximum sum of a subarray of size k" or the "average of all subarrays of size k."
The logic is straightforward:
- Initialize the window with the first elements and calculate the initial value (e.g., sum).
- Iterate from the -th element to the end of the array.
- In each step, "slide" the window by one position:
- Add the new element to your window's state.
- Subtract the element that just fell off the left side of the window.
- Update your global answer (e.g., the maximum sum seen so far).
The following diagram provides a clear visual for this process.

For a narrated walkthrough of this exact problem type, the video below is a great resource.
Coding Interview Patterns - Sliding Window | 10 different problems in a single video
This video from Nikhil Lohia demonstrates the fixed-size window pattern with several clear examples.
Watch the segment from 00:00 to 05:19. It covers the 'maximum sum of a subarray of size K' and shows how the same logic applies to similar problems involving averages or character counts.
2. Variable-Size Sliding Window
This is where the pattern becomes more dynamic and powerful. Here, the window size changes—it expands and shrinks based on conditions defined by the problem. These problems often involve finding the "longest," "shortest," or "smallest" subarray that satisfies a certain property (e.g., "longest substring with no more than k distinct characters").
This variant uses two pointers, left and right, to define the window's boundaries. The general algorithm is:
- Expand the window by moving the
rightpointer. - Update the window's state based on the new element.
- Once a certain condition is met (or violated), shrink the window from the left by moving the
leftpointer until the window is valid again. - Update the result during the expansion or shrinking phase, depending on the problem.
This expand-and-shrink process can feel tricky to implement correctly. For an experienced engineer like yourself, having a robust, reusable template is invaluable.
The following article provides an excellent, structured framework for solving almost any variable-size sliding window problem.
Sliding Window Algorithm Code Template - Labuladong Algo Notes
This article from 'Labuladong Algo Notes' presents a universal code template for the sliding window pattern. It's a fantastic resource for turning the abstract idea into concrete, debuggable code.
Please read the sections 'Sliding Window Framework Overview' and the pseudocode framework that follows it. Pay close attention to the roles of the left and right pointers, the nested while loop structure, and the three key questions to ask when applying the template.
The core takeaway from that article is the template and the three guiding questions:
- When to expand
right? (And what data to update?) - When to shrink
left? (And what data to update?) - When to update the final answer?
Let's apply this template to a couple of problems.
Applying the Pattern: Worked Examples
Your primary language is Go. While many online resources use Java or Python, the algorithmic logic is universal. We'll focus on the logic and also look at a Go-specific implementation.
Example 1: Longest Substring Without Repeating Characters (LeetCode 3)
Problem: Given a string s, find the length of the longest substring without repeating characters.
Let's apply the Labuladong template:
- Window State: We need to track the characters currently in our window and their frequencies. A hash map (or
map[rune]intin Go) is perfect for this. - 1. Expand
right: We always expand. In each step, we moveright, take the characters[right], and increment its count in our frequency map. - 2. Shrink
left: We need to shrink if the window contains a duplicate. The condition iswindow[s[right]] > 1. We moveleft, take the characters[left], and decrement its count in the map. We keep doing this until the duplicate is no longer present. - 3. Update Answer: After each potential shrink, the window
[left, right)is guaranteed to be valid (no repeating characters). This is the perfect time to update our answer:maxLength = max(maxLength, right - left).
To see this logic animated, watch the following segment.
Coding Interview Patterns - Sliding Window | 10 different problems in a single video
Let's return to the Nikhil Lohia video, which demonstrates solving 'longest substring without repeating characters'.
Watch the segment from 07:54 to 10:48. Notice how the window expands and then shrinks as soon as a repeating character ('i') is encountered.
Example 2: Minimum Size Subarray Sum (LeetCode 209)
Problem: Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is greater than or equal to target. If there is no such subarray, return 0.
Let's use the template again:
- Window State: We just need an integer to track the
currentSumof the window. - 1. Expand
right: Always. Moveright, and addnums[right]tocurrentSum. - 2. Shrink
left: The condition to shrink iscurrentSum >= target. This means we have a potential answer! - 3. Update Answer: Inside the shrink loop. As soon as we have a valid window (
currentSum >= target), we update our answer:minLength = min(minLength, right - left). Then, we shrink the window by removingnums[left]fromcurrentSumand incrementingleftto see if we can find an even shorter valid subarray.
This video segment walks through this exact logic.
Coding Interview Patterns - Sliding Window | 10 different problems in a single video
The following clip demonstrates the 'shrinking window' strategy to find a minimum-sized subarray.
Watch from 16:51 to 19:15. Focus on how the answer is updated inside the shrinking loop, as this is a key difference from the 'longest substring' problem.
A Go-Specific Example
Finally, let's look at a slightly more complex problem implemented in Go. This will help you see how these concepts translate directly to your preferred language.
Algorithms in Go: Sliding Window Pattern (Part II) / Habr
This article from Habr discusses a more advanced sliding window problem: finding word concatenations. It's a great example of how the same pattern applies when your 'items' are whole words instead of single characters.
Please read from the paragraph starting 'However, a single match counter is not enough...' through to the end of the section with the Go code snippets. Notice how the freqs map and matches counter in Go serve the exact same purpose as the need map and valid counter from the Labuladong template.
Conclusion
In this lesson, we established a solid foundation for the sliding window pattern. You should now be equipped to recognize and solve a wide variety of substring and subarray problems efficiently.
Key Takeaways:
- The sliding window pattern optimizes brute-force checks on contiguous subarrays/substrings from to .
- Problems can be categorized into fixed-size windows (simpler logic) and variable-size windows (expand/shrink logic).
- For variable-size windows, using a structured template (expand, check shrink condition, shrink, update result) is a reliable strategy for avoiding bugs and writing clean code.
- Identifying the precise conditions for shrinking the window and updating the answer is the key to adapting the template to any given problem.
In our next lesson, we will explore the Two-Pointer technique. While the sliding window is a form of two-pointers, we will look at other variations, such as pointers moving from opposite ends of an array, which are useful for different kinds of problems.