Questions to ask to find which linear pattern is needed
Source: Dev.to
Pattern‑selection cheat sheet
Below is a pattern‑selection cheat sheet.
For each pattern, ask yourself the listed questions while reading the problem.
If you answer YES, that pattern is likely the correct approach.
1. Sliding Window (Fixed Size)
Ask these questions:
- Is the problem asking about subarrays/substrings of exact length
k? - Do I need to compute something (sum, max, count) for every window of size
k? - Does brute force give O(n·k) and I need O(n)?
Trigger words: “window of size k”, “consecutive k elements”, “substring length k”
2. Sliding Window (Variable Size)
Ask these questions:
- Is the window size not fixed, but constrained by a condition?
- Am I trying to find the smallest / largest subarray satisfying a condition?
- Can I expand right and shrink left?
Trigger words: “at most k”, “at least k”, “minimum window”, “longest substring”
3. Two Pointers (Same Direction)
Ask these questions:
- Do I move through the array left → right with two indices?
- Is one pointer catching up to the other?
- Am I maintaining a range inside the array?
Trigger words: “subarray”, “range”, “continuous segment”
4. Two Pointers (Opposite Ends)
Ask these questions:
- Is the array sorted?
- Am I looking for a pair satisfying a condition?
- Can I move left++ or right— based on comparison?
Trigger words: “pair sum”, “target”, “sorted array”
5. Fast & Slow Pointers (Cycle Detection)
Ask these questions:
- Is there a linked list or pointer structure?
- Could there be a cycle / loop?
- Can one pointer move 2× faster than the other?
Trigger words: “cycle”, “loop”, “circular”, “duplicate via index jump”
6. Prefix Sum
Ask these questions:
- Do I need sum of ranges repeatedly?
- Does the problem involve subarray sum equals k?
- Can a running cumulative value help?
Trigger words: “range sum”, “subarray sum”, “sum between i and j”
7. Difference Array
Ask these questions:
- Are there multiple range updates?
- Am I asked to apply increments/decrements on intervals?
- Do final values matter, intermediate don’t?
Trigger words: “range update”, “apply operations”, “add x from l to r”
8. Hash Map Frequency Counting
Ask these questions:
- Am I counting frequency of elements/characters?
- Do I need constant‑time lookup?
- Is order less important than counts?
Trigger words: “frequency”, “count occurrences”, “anagram”
9. In‑place Modification
Ask these questions:
- Am I told not to use extra space?
- Can the array be modified?
- Is the output expected inside the same array?
Trigger words: “in‑place”, “O(1) space”, “modify input”
10. Sorting‑based Pattern
Ask these questions:
- Does sorting simplify the logic drastically?
- Am I matching pairs or intervals?
- Can order change without affecting correctness?
Trigger words: “after sorting”, “arrange”, “reorder”
11. Cyclic Sort
Ask these questions:
- Are numbers in the range 1…n?
- Is each number supposed to be at a specific index?
- Am I finding missing / duplicate elements?
Trigger words: “missing number”, “duplicate”, “1 to n”
12. Kadane’s Algorithm (Max Subarray)
Ask these questions:
- Am I asked for maximum sum subarray?
- Is negative sum harmful?
- Can I reset when sum n/2 times?
- Can I assume a majority exists?
- Do I need O(1) space?
Trigger words: “majority element”, “more than half”
20. Reservoir Sampling
Ask these questions:
- Is the data streaming / infinite?
- Do I not know
nbeforehand? - Do I need uniform random selection?
Trigger words: “stream”, “random sample”, “unknown size”