This repository contains Java implementations and explanations for the most common coding interview patterns. To master them effectively, I recommend following the learning path below. This order is designed to build your intuition gradually, starting from simple array manipulations and moving toward complex recursive and optimization problems.
Start here to get comfortable with array indices and basic iteration logic.
- Prefix Sum
- Why: It’s the simplest optimization technique. It teaches you how to pre-process data to answer queries in O(1).
- Two Pointers
- Why: Fundamental for searching pairs in sorted arrays and in-place modifications.
- Sliding Window
- Why: A natural evolution of Two Pointers. Essential for subarray and substring problems.
These patterns improve your ability to visualize and manipulate pointers without using extra space.
- Fast & Slow Pointers
- Why: Teaches cycle detection and finding midpoints, which are specific but common tricks.
- Linked List In-place Reversal
- Why: Mastering
prev,curr, andnextpointer manipulation is a rite of passage.
- Why: Mastering
Moving from linear O(N) thinking to logarithmic O(log N) and sorting-based solutions.
- Modified Binary Search
- Why: You must know how to search in O(log N). Handling rotated arrays is a classic interview twist.
- Overlapping Intervals
- Why: Teaches you to sort data first to simplify the problem (often O(N log N)).
- Top 'K' Elements
- Why: Introduces Heaps (Priority Queues), a crucial data structure for "largest/smallest" problems.
The core of many interviews. Understand the difference between going "wide" (BFS) and "deep" (DFS).
- Binary Tree Traversal
- Why: Trees are easier than graphs. Master Pre-order (DFS) and Level-order (BFS) here first.
- Matrix Traversal
- Why: Learn how to move in a 2D grid (directions arrays, boundary checks) before applying complex algorithms.
- Depth-First Search (DFS)
- Why: The go-to for "island" problems and exhaustive pathfinding.
- Breadth-First Search (BFS)
- Why: The only way to find the shortest path in unweighted graphs/grids.
- Monotonic Stack
- Why: A niche but powerful pattern for "Next Greater Element" problems that are hard to solve otherwise.
These are the hardest to master. Backtracking builds the recursion skills needed for DP.
- Backtracking
- Why: Brute-force approach to generate all permutations/subsets. It’s the foundation for understanding the "decision tree."
- Dynamic Programming
- Why: The ultimate optimization. It takes the recursive logic from Backtracking and optimizes it using Memoization or Tabulation.
- Read the Pattern File: Understand the Explanation and When to use sections.
- Type the Code: Don't just copy-paste. Type the examples to build muscle memory.
- Solve LeetCode Problems: Search for problems tagged with these keywords (e.g., "Sliding Window") and try to apply the template.
- Repeat: Spaced repetition is key. Re-visit a pattern a week later.
Happy Coding!