Mastering Coding Interviews: 15 Essential Patterns to Solve Any Problem

Babek Naghiyev
6 min readFeb 10, 2025

--

Coding interviews are a cornerstone of tech hiring, but their unpredictability can be daunting. The key to success lies in recognizing recurring problem-solving patterns. By mastering these patterns, you can efficiently tackle a wide range of questions, from array manipulations to graph traversals. In this article, we’ll break down 15 essential coding patterns, their use cases, and examples to help you ace your next interview.

1. Sliding Window

Use Case: Subarrays, substrings, or sequences where you need to find averages, sums, or optimize contiguous data.
Example Problem: Find the maximum sum of a subarray of size k.
Approach:

  • Maintain a window (start and end pointers).
  • Slide the window by adjusting pointers while computing results incrementally.
    Why It Works: Reduces time complexity from O(n²) to O(n).

Easy examples:

  1. Maximum Sum Subarray of Size K
  2. Smallest Subarray with a given sum Educative.io

2. Two Pointers

Use Case: Sorted arrays, linked lists, or paired elements.
Example Problem: Find a pair of numbers in a sorted array that sum to a target.
Approach:

  • Use left/right pointers to traverse from both ends.
  • Adjust pointers based on comparisons (e.g., move left if the sum is too small).
    Variants: Fast & Slow Pointers (for cycle detection in linked lists).

Easy examples:

  1. Pair with Target Sum LeetCode
  2. Remove Duplicates LeetCode LeetCode LeetCode LeetCode LeetCode
  3. Squaring a Sorted Array LeetCode

3. Fast & Slow Pointers (Tortoise and Hare)

Use Case: Detect cycles, find middle nodes, or palindrome checks.
Example Problem: Detect a cycle in a linked list.
Approach:

  • Advance the fast pointer by 2 steps and the slow by 1.
  • If they meet, a cycle exists.

Easy examples:
1. LinkedList Cycle Leetcode

2. Middle of the LinkedList Leetcode

4. Merge Intervals

Use Case: Overlapping intervals or conflicting appointments.
Example Problem: Merge overlapping intervals.
Approach:

  • Sort intervals by start time.
  • Iterate and merge if the current interval overlaps with the previous.

5. Cyclic Sort

Use Case: Arrays with elements in a known range (e.g., 1 to n).
Example Problem: Find the missing number in an array of 1 to n.
Approach:

  • Place each number in its correct index (e.g., 3 at index 2).
  • Traverse to find mismatches indicating missing/duplicate numbers.

Easy examples:

  1. Find the Duplicate Number Leetcode
  2. Find the Missing Number Leetcode
  3. Cyclic Sort Geeksforgeeks
  4. Find all Missing Numbers Leetcode
  5. Problem Challenge 1: Find the Corrupt Pair TheCodingSimplified
  6. Find all Duplicate Numbers Leetcode

6. In-Place Reversal

Use Case: Reverse linked lists or sublists without extra memory.
Example Problem: Reverse a linked list in groups of k.
Approach:

  • Use pointers to track previous, current, and next nodes.
  • Reverse links iteratively.

Easy example: Reverse a LinkedList Leetcode

7. Tree BFS (Breadth-First Search)

Use Case: Level-order traversal, shortest path in unweighted graphs.
Example Problem: Zigzag traversal of a binary tree.
Approach:

  • Use a queue to process nodes level by level.

Easy examples:

  1. Binary Tree Level Order Traversal Leetcode
  2. Level Averages in a Binary Tree Leetcode
  3. Minimum Depth of a Binary Tree Leetcode
  4. Maximum Depth of a Binary Tree Leetcode
  5. Level Order Successor Geeksforgeeks
  6. Reverse Level Order Traversal Leetcode

8. Tree DFS (Depth-First Search)

Use Case: Path sum, subtree checks, or permutations.
Example Problem: Validate a binary search tree.
Approach:

  • Recursive or stack-based traversal with bounds checking.

Easy example: Binary Tree Path Sum Leetcode

9. Two Heaps

Use Case: Streams of data requiring median tracking or partitioning.
Example Problem: Find the median of a data stream.
Approach:

  • Use a max-heap for the lower half and a min-heap for the upper half.
  • Balance heaps to quickly compute medians.

10. Subsets/Backtracking

Use Case: Combinations, permutations, or exhaustive searches.
Example Problem: Generate all subsets of a set.
Approach:

  • Recursively build subsets, adding/removing elements (backtrack).

Easy examples:

  1. Subsets With Duplicates Educative.io
  2. Subsets Educative.io

11. Modified Binary Search

Use Case: Rotated sorted arrays, unknown-order searches.
Example Problem: Search in a rotated sorted array.
Approach:

  • Compare mid with start/end to determine the sorted half.
  • Adjust search space based on target position.

Easy examples:

  1. Bitonic Array Maximum Geeksforgeeks
  2. Order-agnostic Binary Search Geeksforgeeks

12. Top K Elements

Use Case: Frequent elements, largest/smallest values.
Example Problem: Find the top k frequent numbers.
Approach:

  • Use a min-heap to track the top k elements (O(n log k)).
  • Alternatively, QuickSelect for O(n) average time.

Easy examples:

  1. Kth Smallest Number
  2. Top ‘K’ Numbers Solution
  3. ‘K’ Closest Points to the Origin Leetcode
  4. Connect Ropes

13. K-way Merge

Use Case: Merge k sorted lists or arrays.
Example Problem: Merge k sorted linked lists.
Approach:

  • Use a heap to track the smallest element from each list.

14. Topological Sort

Use Case: Task scheduling, dependency resolution.
Example Problem: Course schedule with prerequisites.
Approach:

  • Kahn’s algorithm (in-degree tracking with BFS).
  • DFS-based post-order traversal.

15. Dynamic Programming (0/1 Knapsack)

Use Case: Optimization problems with constraints.
Example Problem: Maximize value in a knapsack with weight limits.
Approach:

  • Build a DP table where dp[i][w] = max value for first i items and weight w.

Bonus Patterns

  • Bitwise XOR: Find missing numbers or unique elements.
  • Union-Find: Detect cycles in undirected graphs.
  • Monotonic Stack: Next greater element or histogram areas.

How to Practice Effectively

  1. Pattern-First Approach: Categorize problems by pattern.
  2. Repetition: Solve similar problems to reinforce recognition.
  3. Mock Interviews: Simulate real conditions to build speed and accuracy.

Resources

By internalizing these patterns, you’ll transform coding interviews from chaotic challenges into systematic exercises. Remember, practice and pattern recognition are your greatest allies — start today! 🚀

--

--

Babek Naghiyev
Babek Naghiyev

Written by Babek Naghiyev

Senior Software Engineer (Frontend)

No responses yet