Learning Outcome
Revise Sliding Window Template quickly before interviews.
Pattern Recognition
| Item | Detail |
|---|---|
| Core signal | You need fast syntax or template recall. |
| Use when | Use after learning the concept, not as a substitute for understanding. |
| Avoid when | The required invariant is not monotonic or the input constraints point to a simpler direct scan. |
Intuition
A compact template protects attention for the actual problem invariant.
Exact Practice Question Names
- expand
- shrink while invalid
- update answer
- frequency cleanup
Interview Approach
- expand
- shrink while invalid
- update answer
- frequency cleanup
- Dry-run once before finalizing code.
Pseudocode
- expand\n- shrink while invalid\n- update answer\n- frequency cleanup
Sample Dry Run
Pick one known problem and map each template line to a real state change.
Edge Cases
- Empty containers
- Boundary indices
- Overflow
- Tie-breaking
Common Mistakes
- Copying syntax without adapting state
- Skipping empty checks
- Ignoring language-specific defaults
Complexity
| Item | Detail |
|---|---|
| Expected time | Template dependent. |
| Expected space | Template dependent. |
Java, C++ and Python Notes
- Java: prefer explicit classes and clear helper methods over clever one-liners.
- C++: use vector, unordered_map, set, priority_queue, and long long when sums can grow.
- Python: keep state readable with dict, set, deque, heapq, and lru_cache where appropriate.
Quick Revision Checklist
- Name the pattern before coding.
- State the invariant or DP state in one sentence.
- Dry-run the smallest non-trivial example.
- Close with time and space complexity.