Jul 15, 2024

- Solved over 500 Leetcode problems to realize effectiveness of recognizing patterns.
- Focus on eight key patterns to improve problem-solving skills.

**Use Case**: Process series of data elements in lists or strings.**Mechanism**: Look at a portion of data (window) which slides through the list/string.**When to Use**: Find subsets of elements satisfying a given condition.- Typical input: array, string, linked list.
- Example: Find longest substring with K unique characters.

**Use Case**: Find all possible combinations of elements from a set.**Mechanism**: Explore all possible arrangements (similar to BFS).**When to Use**: Problems requiring all subsets permutations.- Example: Generate permutations of a set.

**Use Case**: Search within a divided search space.**Mechanism**: Adjust standard binary search logic as needed.**When to Use**: Special cases of binary search.- Example: Search in rotated sorted array.
- Tip: Understanding core binary search is crucial.
**Practice Tip**: Implement`bisect_left`

and`bisect_right`

to improve understanding.

**Use Case**: Select top K elements from a larger set.**Mechanism**: Use Heap data structure for efficient tracking.**When to Use**: Finding top elements by ranking (e.g. K largest elements).- Example: Find Kth largest number in an array.

**Use Case**: Explore nodes of a tree thoroughly before backtracking.**Mechanism**: Use recursion to deep dive into branches.**When to Use**: Problems involving binary trees and path findings.- Example: Find maximum depth of binary tree.

**Use Case**: Arrange elements with dependencies (DAGs).**Mechanism**: Sequence elements based on dependency order.**When to Use**: Dependency resolution problems.- Example: Course schedule problem (prerequisites).

**Use Case**: Explore nodes level by level.**Mechanism**: Use Queue data structure.**When to Use**: Level-wise tree traversal problems.- Example: Level order traversal of a binary tree.

**Use Case**: Iterate through a sorted array efficiently.**Mechanism**: Use two pointers from different ends of array.**When to Use**: Problems involving summation or sub-arrays in sorted arrays.- Example: Two sum problem in a sorted array.
- Variation: Finding triplets that sum to zero.

- Understanding and practicing these patterns is crucial for effectively solving coding problems.
- Seek out problems across different patterns and practice.

- Sign up for the free email crash course on DSA at interview.io for mastering data structures and algorithms.
- Presenter: Sahil