1.
Hashing (Dictionary)
Hashing allows constant time lookup using key-value pairs. In Python, dictionaries are used.
Common use cases include counting frequency, checking duplicates, and solving pair problems like
Two Sum. Example: d[x] = [Link](x,0) + 1. This avoids key errors and updates counts efficiently.
2. Sliding Window
Sliding window is used for subarray or substring problems. Maintain a window using two pointers.
Expand the window and shrink when conditions break. Example pattern: while condition: left += 1.
Useful for longest substring problems.
3. Two Pointers
Used when array is sorted or when scanning from both ends. Move left and right pointers based on
condition. Example: if sum < target, move left; else move right.
4. Stack
Stack follows Last-In-First-Out. Implement using Python list. Used for problems like valid
parentheses and monotonic stack problems. Operations: append() and pop().
5. Heap (Priority Queue)
Heaps are used for Top K problems. Python provides heapq which is a min heap. For max heap,
push negative values. Operations: heappush and heappop.
6. BFS and DFS
BFS uses queue (deque) and explores level by level. DFS uses recursion or stack. Used in trees
and graphs like Number of Islands.