Pattern Template
Every file in
patterns/(exceptREADME.md) MUST follow this 9-section shape — includes## Diagramwith at least one```mermaidblock for fast visual recall.
# NN. <Pattern Name>
## TL;DR
One paragraph: what the pattern is and the kind of problem it cracks.
## Recognition Cues
- Phrases in problem statement that scream this pattern.
- Input/output shapes (sorted array, contiguous subarray, "kth", etc.).
## Diagram
One Mermaid diagram: usual control flow, pointer movement, or decision tree for when to pick this pattern. **camelCase** node IDs.
```mermaid
flowchart LR
cueNode[Recognition cue] --> patternNode[Pattern applies]
patternNode --> recipeNode[Run generic recipe]Mental Model
Picture / invariant. Why it works. (e.g., "two pointers shrink the window when sum > target — sortedness gives us monotonicity.")
Generic Recipe
Numbered steps you'd code without thinking once you've matched the pattern.
- Initialize ...
- Loop while ...
- Update ...
- Return ...
Complexity
- Time: typical and worst case.
- Space: typical.
Generic Code Template
Go
func patternTemplate(input []int) int {
// skeleton
}JavaScript
function patternTemplate(input) {
// skeleton
}Variants
- Variant A — when to switch (e.g., "shrink-only" window vs "fixed-size" window).
- Variant B — common twists.
Representative Problems
At least 3 — link to real files under topics/<topic>/problems/NNN-kebab-name.md:
- Problem A (vanilla) —
../topics/<topic>/problems/NNN-kebab-name.md - Problem B (variant) — same pattern, different constraint
- Problem C (hard) — stretch application
Anti-patterns
- When this pattern looks tempting but isn't right.
- Common bugs (off-by-one, missed shrink condition, etc.).
Last updated on
Spotted something unclear or wrong on this page?