Pattern #25

Divide & Conquer

Important interview questions, thinking patterns, Divide / Solve / Combine lifecycle, Merge Sort, Quick Sort, Binary Search, and Go rules.

Must Solve

15 core questions — solve these first.

  1. 1.Merge Sort
    easy
  2. 2.Quick Sort
    medium
  3. 3.Binary Search
    easy
  4. 4.Maximum Subarray
    medium
  5. 5.Sort an Array
    medium
  6. 6.Kth Largest Element in an Array
    medium
  7. 7.Majority Element
    easy
  8. 8.Search a 2D Matrix II
    medium
  9. 9.Pow(x, n)
    medium
  10. 10.Different Ways to Add Parentheses
    medium
  11. 11.Construct Binary Tree from Preorder and Inorder Traversal
    medium
  12. 12.Convert Sorted Array to Binary Search Tree
    easy
  13. 13.Count of Smaller Numbers After Self
    hard
  14. 14.Reverse Pairs
    hard
  15. 15.Count Inversions in an Array
    medium

Also Important

8 more questions worth practicing.

  1. 16.Median of Two Sorted Arrays
    hard
  2. 17.Closest Pair of Points
    hard
  3. 18.Merge K Sorted Lists
    hard
  4. 19.Beautiful Array
    medium
  5. 20.Maximum Binary Tree
    medium
  6. 21.Quad Tree Construction
    medium
  7. 22.Karatsuba Multiplication
    medium
  8. 23.Strassen Matrix Multiplication
    hard

How to Think

  1. Can I split the input into halves?Divide and Conquer
  2. Can each half be solved independently?Solve left, Solve right, Combine
  3. Problem size becomes half each time?N → N/2 → N/4 (log N levels)
  4. Need sort efficiently?Merge Sort / Quick Sort (O(N log N))
  5. Need search sorted data?Binary Search (O(log N))

Go Divide & Conquer Template

Universal Divide & Conquer Shape in Go

func solve(nums []int) Result {
    // 1. Base Case (stop division)
    if len(nums) <= 1 {
        return baseResult(nums)
    }

    // 2. Divide
    mid := len(nums) / 2

    // 3. Solve Subproblems (Left & Right)
    leftResult := solve(nums[:mid])
    rightResult := solve(nums[mid:])

    // 4. Combine Answers
    return combine(leftResult, rightResult)
}

👉 Total Time: O(N log N) when combine work is O(N) | Space: O(N)

Core 3-Step Lifecycle

1. DIVIDE

Split the main problem into smaller, independent subproblems (usually halves at mid).

2. CONQUER (SOLVE)

Recursively solve the smaller subproblems until reaching base cases (e.g. array size <= 1).

3. COMBINE

Merge subproblem answers into the final result (e.g. 2-pointer sorted merge in Merge Sort).

Divide & Conquer vs Recursion vs Dynamic Programming

Recursion

Any function calling itself. D&C uses recursion, but not all recursion is D&C!

Divide & Conquer

Subproblems are INDEPENDENT (e.g. left half & right half in Merge Sort).

Dynamic Programming

Subproblems OVERLAP repeatedly (e.g. Fibonacci fib(3) & fib(2)). Use Memoization!

Visual Memory Rule
DIVIDE (Split in half)  →  SOLVE (Recurse left & right)  →  COMBINE (Merge results)
Independent Subproblems  →  Divide and Conquer
Overlapping Subproblems  →  Dynamic Programming / Memoization
Binary Search            →  Divide & Discard (No Combine Step Needed!)

💡 Golden Rule: "Break one hard problem into smaller easy problems, solve them, then combine the answers."

Common Interview Mistakes

1. Missing Base Case

Forgetting base case condition leads directly to infinite recursion and call stack overflow crash!

2. Off-by-One Mid Slice Split

Slice indexing errors with mid in Go (nums[:mid] vs nums[mid:]) cause infinite loops.

3. Ignoring Combine Cost

Splitting is fast, but an expensive O(N^2) combine step ruins overall time complexity. Always account for combine cost!

4. Assuming Balanced Split

Quick Sort degrades to O(N^2) if pivot selection repeatedly splits into 0 and N-1 elements instead of balanced halves.

Interview Rules

  1. 1. Can split into smaller independent parts? → Divide and Conquer
  2. 2. Halve repeatedly?log₂ N recursion levels
  3. 3. Efficient sorting? → Merge Sort (O(N log N)) or Quick Sort
  4. 4. Sorted search? → Binary Search (O(log N))
  5. 5. Solve left + right? → Recursion with base case
  6. 6. Need combine? → Account for combine step complexity
  7. 7. Repeated overlapping subproblems? → Use DP / Memoization instead
  8. 8. Overall ComplexityO(N log N) time, O(N) space for Merge Sort

Small Rules

  1. Rule 1:Every D&C problem follows: Divide, Solve Recursively, Combine.
  2. Rule 2: Not every problem needs a combine step (Binary Search discards one half directly).
  3. Rule 3: Recurrence T(N) = 2T(N/2) + O(N) yields O(N log N) total time.
  4. Rule 4: Balanced splits (N/2 and N/2) ensure optimal logarithmic depth.
  5. Rule 5: Subproblems MUST be mostly independent; overlapping subproblems require Dynamic Programming.

Production Thinking

Parallel ProcessingRun independent left and right half computations concurrently in parallel threads

Large File Chunk ProcessingSplit multi-gigabyte datasets into Part A, Part B, process separately, and aggregate results

Distributed Systems (MapReduce)Distribute chunks across worker machines, then combine/aggregate outputs

Search System EliminationEliminate large irrelevant search space halves early without scanning all data

Remember This

Divide                → Split problem
Conquer               → Solve smaller parts
Combine               → Build final answer
Half repeatedly       → log n levels
Merge Sort            → Split + Merge
Quick Sort            → Pivot + Partition
Binary Search         → Discard half
Repeated subproblems  → Think DP

💡 Golden Rule: "Break one hard problem into smaller easy problems, solve them, then combine the answers."