DSA Patterns

A curated list of patterns to help you solve any coding interview problem efficiently.

function findPattern(problem) {
    if (problem.array && problem.array.isSorted) {
      return ["Binary search", "Two pointers"];
    }
    
    if (problem.requiresAllPermutations) {
      return ["Backtracking"];
    }
    
    // More patterns...
  }
            

Algorithm Patterns

Recognize these patterns to solve any coding interview problem efficiently

Sorted Array

If the input array is sorted, consider:

  • Binary search
  • Two pointers
Time Complexity: O(log n) or O(n)

Permutations/Subsets

If asked for all permutations/subsets, consider:

  • Backtracking
Time Complexity: O(n * 2^n)

Tree Problems

If given a tree, consider:

  • DFS
  • BFS
Time Complexity: O(n)

Graph Problems

If given a graph, consider:

  • DFS
  • BFS
Time Complexity: O(V + E)

Linked List

If given a linked list, consider:

  • Two pointers
Time Complexity: O(n)

Recursion Banned

If recursion is banned, consider:

  • Stack
Time Complexity: O(n)

In-Place Operations

If must solve in-place, consider:

  • Swap corresponding values
  • Store multiple values in same pointer
Space Complexity: O(1)

Max/Min Subarray

If asked for maximum/minimum subarray/subset/options, consider:

  • Dynamic programming
Time Complexity: O(n^2) or O(n)

Top/Least K Items

If asked for top/least K items, consider:

  • Heap
  • QuickSelect
Time Complexity: O(n log k) or O(n)

Common Strings

If asked for common strings, consider:

  • Map
  • Trie
Time Complexity: O(n)

General Patterns

General patterns to consider:

  • Map/Set for O(1) time & O(n) space
  • Sort input for O(nlogn) time and O(1) space
Based on problem constraints