LeetCode 14 Patterns Mastery: 130 Questions with Company Insights💻🚀

14 LeetCode Patterns Guide with 130 Questions 🚀

By Hardik B Vanza

This take time to cook up, so if it's good, drop an upvote!😎

This guide organizes 130 popular LeetCode questions into 14 essential patterns to help you master coding interviews. Each pattern includes a brief explanation for better understanding, and a list of questions with their topics, company frequencies, and direct LeetCode links. Questions that don’t fit into these patterns are listed under "Other Techniques." Let’s dive in!


Table of Contents

  1. Sliding Window
  2. Two Pointers
  3. Fast and Slow Pointers
  4. Merge Intervals
  5. Cyclic Sort
  6. In-place Reversal of Linked List
  7. Tree BFS
  8. Tree DFS
  9. Two Heaps
  10. Subsets
  11. Modified Binary Search
  12. Top K Elements
  13. K-way Merge
  14. Topological Sort
  15. Other Techniques

1. Sliding Window

Explanation:
The Sliding Window pattern processes a specific "window" of elements in an array or string. The window slides right, performing operations like finding the longest/shortest subarray or substring. The window size can be fixed or dynamic.

When to Use?

  • Linear data structures (arrays, strings, linked lists).
  • Finding longest/shortest substrings or subarrays.

Questions :


2. Two Pointers

Explanation:
The Two Pointers pattern uses two pointers moving through a data structure, often in sorted arrays or linked lists, to optimize solutions. It reduces time complexity from O(n²) to O(n) or O(n log n).

When to Use?

  • Sorted arrays or linked lists.
  • Finding pairs, triplets, or subarrays.

Questions :


3. Fast and Slow Pointers

Explanation:
The Fast and Slow Pointers (Hare & Tortoise) pattern uses two pointers moving at different speeds (fast at 2x, slow at 1x). It’s ideal for detecting loops in linked lists or arrays.

When to Use?

  • Detecting loops in linked lists or arrays.
  • Checking palindromes or cycle lengths.

Questions :


4. Merge Intervals

Explanation:
The Merge Intervals pattern handles overlapping intervals by merging or checking them. Understanding six cases of interval relations is key.

When to Use?

  • Producing mutually exclusive intervals.
  • Problems involving overlapping intervals.

Questions :

  • 1. Insert Interval (Interval)

    Companies: Google - 15, LinkedIn - 8, Facebook - 6, Amazon - 4, Microsoft - 2

  • 2. Merge Intervals (Interval)

    Companies: Facebook - 87, Amazon - 57, Google - 31, Apple - 22, Microsoft - 17

  • 3. Non-overlapping Intervals (Interval)

    Companies: Facebook - 6, Amazon - 5, Microsoft - 3, Apple - 5, Google - 5

  • 4. Meeting Rooms (Interval)

    Companies: Google - 3, Amazon - 2, Microsoft - 6, Facebook - 2, Bloomberg - 2

  • 5. Meeting Rooms II (Interval)

    Companies: Amazon - 58, Google - 22, Facebook - 20, Bloomberg - 19, Microsoft - 18

  • 6. Employee Free Time (Interval)

    Companies: Google - 10, Uber - 4, Apple - 3, Microsoft - 2, Amazon - 3


5. Cyclic Sort

Explanation:
Cyclic Sort is used for arrays with numbers in a specific range. It swaps each number to its correct index, achieving O(n) complexity. Ideal for finding missing or duplicate numbers.

When to Use?

  • Sorted/rotated arrays with numbers in a range.
  • Finding missing or duplicate numbers.

Questions :

  • 1. First Missing Positive (Hashing)

    Companies: Amazon - 18, Microsoft - 10, Adobe - 5, Google - 3, Apple - 3

  • 2. Missing Number (Binary-Bit Manipulation)

    Companies: Microsoft - 13, Amazon - 12, Apple - 4, Adobe - 3, Facebook - 3


6. In-place Reversal of Linked List

Explanation:
This pattern reverses linked list node links in-place without extra memory. A current pointer starts at the head, and a previous pointer reverses links step-by-step.

When to Use?

  • Reversing a linked list in-place.

Questions :

  • 1. Reverse a Linked List (Linked List)

    Companies: Amazon - 15, Apple - 8, Microsoft - 7, Google - 4, Facebook - 3

  • 2. Reorder List (Linked List)

    Companies: Amazon - 9, Microsoft - 8, Facebook - 3, Google - 3, Uber - 3

  • 3. Reverse Nodes in k-Group (Linked List)

    Companies: Microsoft - 15, Amazon - 13, Google - 3, Apple - 3, Facebook - 2


7. Tree BFS

Explanation:
Tree BFS (Breadth First Search) traverses a tree level-by-level using a queue. Start with the root in the queue, process each level, and add children to the queue.

When to Use?

  • Level-order tree traversal.

Questions :


8. Tree DFS

Explanation:
Tree DFS (Depth First Search) traverses a tree depth-first (pre-order, in-order, or post-order) using recursion or a stack.

When to Use?

  • In-order, pre-order, or post-order traversal.
  • Solutions near leaf nodes.

Questions :


9. Two Heaps

Explanation:
The Two Heaps pattern uses a Min Heap and a Max Heap to balance or find medians efficiently. The first half is stored in a Max Heap (largest element), and the second half in a Min Heap (smallest element).

When to Use?

  • Finding smallest, largest, or median elements.
  • Priority queue or scheduling problems.

Questions :


10. Subsets

Explanation:
The Subsets pattern generates permutations and combinations using BFS or recursion. Start with an empty set and add elements to create new subsets.

When to Use?

  • Finding combinations or permutations.

Questions :


Explanation:
Modified Binary Search adapts binary search for rotated arrays or other variations. It calculates the middle index and narrows the search range based on the key.

When to Use?

  • Searching in sorted data structures with variations.

Questions :


12. Top K Elements

Explanation:
The Top K Elements pattern finds the top/smallest/frequent K elements using heaps or sorting.

When to Use?

  • Finding top/smallest/frequent K elements.

Questions :


13. K-way Merge

Explanation:
The K-way Merge pattern combines K sorted lists or arrays using a min-heap. Insert the smallest element from each list into the heap, remove the smallest, and add the next element.

When to Use?

  • Merging sorted arrays/lists.
  • Finding the smallest element.

Questions :

  • 1. Merge K Sorted Lists (Linked List)

    Companies: Amazon - 54, Facebook - 42, Microsoft - 17, Apple - 10, Google - 8


14. Topological Sort

Explanation:
Topological Sort arranges vertices of a directed acyclic graph (DAG) in a linear order respecting dependencies. Use in-degrees to find sources and process them with a queue.

When to Use?

  • Graphs with dependencies.
  • Sorting objects in a specific order.

Questions :

  • 1. Course Schedule (Graph)

    Companies: Amazon - 34, Google - 9, TikTok - 9, Microsoft - 8, Facebook - 7

  • 2. Course Schedule II (Graph)

    Companies: Amazon - 40, Google - 14, Microsoft - 13, Apple - 6, TikTok - 4

  • 3. Alien Dictionary (Graph)

    Companies: Airbnb - 18, Amazon - 12, Facebook - 9, Google - 6, Microsoft - 5


Other Techniques

These questions don’t fit directly into the 14 patterns but are still important for coding interviews. They can be solved using other common techniques like Dynamic Programming, Graph DFS/BFS, Stack, Hashing, etc.

Questions :


By Hardik B Vanza

Comments (2)