DSA Cheat Sheet & Problem Patterns
Sliding Window Pattern
Used in problems involving subarrays or substrings.
Fixed Window:
- Maximum sum subarray of size K
- Max/Min of all subarrays of size K
- First negative number in every window of size K
Variable Window:
- Longest substring with K unique characters
- Longest subarray with sum at most K
- Minimum window substring
Key Idea: Expand and shrink window with two pointers.
Two Pointer Pattern
Used when array or string is sorted or needs linear pass from both ends.
Problems:
- Two sum (sorted)
- Container with most water
- Remove duplicates from sorted array
- 3Sum problem
Key Idea: Move left/right pointers based on condition.
Prefix Sum Pattern
DSA Cheat Sheet & Problem Patterns
Useful for range sum problems.
Problems:
- Subarray sum equals K
- Equilibrium index
- Count number of subarrays with given sum
Key Idea: Precompute prefix sum array or use hashmap to store running sums.
Binary Search Pattern
Used on sorted arrays or answers.
Problems:
- Binary search in array
- Find peak element
- Search in rotated sorted array
- Koko Eating Bananas (search on answers)
Key Idea: Divide and conquer. Adjust low/high pointers.
Hashing / Frequency Maps
Used to count or track elements.
Problems:
- Two sum (unsorted)
- Longest substring without repeating characters
- Group anagrams
DSA Cheat Sheet & Problem Patterns
Key Idea: Use unordered_map to track frequency or index.