ReviseAlgo Logo

Pattern Recognition Center

Pattern Comparison

Master pattern comparisons: Two Pointers vs Sliding Window, DFS vs BFS, and Greedy vs Dynamic Programming.

Last Updated: August 2, 2026 15 min read

1. Introduction

What is Pattern Comparison?

Pattern Comparison is the analysis of lookalike algorithmic paradigms. Many coding questions can appear to match multiple patterns. Understanding the subtle differences between these patterns prevents you from writing incorrect or inefficient code during interviews.

Why study it?

Interviewers frequently ask you to justify your choices (e.g. "Why did you choose BFS instead of DFS here?"). Being able to articulate the trade-offs in time, space, and complexity between similar patterns demonstrates senior engineering depth.

2. Mental Models

Two Pointers vs. Sliding Window

  • Two Pointers (Sorted Convergence): Imagine two people walking from opposite ends of a bridge to meet in the middle. They adjust their speeds based on their collective weight.
  • Sliding Window (Contiguous Stretch): Imagine a caterpillar stretching its front head forward, then pulling its back tail forward, crawl-scanning along a branch to stay within a leaf cluster limit.
  • DFS vs. BFS

  • DFS (Deep Dive): Exploring a maze by walking down a single corridor as far as possible until hitting a dead end, then backtracking.
  • BFS (Ripple Effect): Dropping a stone in water and watching waves expand outwards in concentric rings. It visits all immediate neighbors before traveling further.

  • 3. Key Comparisons & Trade-offs

    1. Two Pointers vs. Sliding Window

  • Two Pointers: Used to find pairs or triplets in a sorted array (e.g. Two Sum II). Pointers converge from opposite ends (left = 0, right = N-1).
  • Sliding Window: Used to find a contiguous subarray or substring satisfying a condition (e.g., Minimum Window Substring). Pointers expand or contract dynamically in the same direction (left and right scan forward).
  • 2. DFS vs. BFS

  • DFS: Uses a recursive call stack. Best for all-paths search, cycle detection, topological sorting, and tree traversals.
  • BFS: Uses a FIFO queue. Best for finding the shortest path in unweighted graphs/grids (guarantees finding shortest distance first).
  • 3. Greedy vs. Dynamic Programming

  • Greedy: Makes the locally optimal choice now and never backtracks. Runs in O(N) time and O(1) space, but only works if the problem satisfies the greedy choice property.
  • Dynamic Programming: Solves and caches subproblems. Used when local choices affect future options, requiring you to evaluate multiple paths to find the global optimum.

  • 4. Decision Matrix: DFS vs. BFS

    Comparing traversal choices on a tree with a target node located at a shallow depth:


    5. Real-World Examples

  • GPS Map Navigation: Using BFS (Dijkstra) to find the shortest driving distance route, rather than DFS (which might take you on a massive detour).
  • Social Network Connections: BFS is used to suggest "Friends of Friends" at distance level 2.

  • 6. Interview Perspective

    How Interviewers Ask This Topic

    Interviewers test paradigm decisions:
  • "Why can't we use two pointers from both ends to find the longest substring without repeats?"
  • Answer: Two pointers from opposite ends require a sorted array to decide whether to increment left or decrement right. Substrings are unsorted and require contiguous windows, so we must use a sliding window moving in the same direction.
  • "Why is BFS preferred over DFS for finding the shortest path in unweighted grids?"
  • Answer: BFS searches level-by-level. The first time BFS reaches the destination node, it is guaranteed to be via the shortest path. DFS explores deep paths first and can find a very long path early, requiring you to search the entire graph to guarantee it is the shortest.
  • Common Mistakes

    Warning: 1. DFS for Shortest Path: Using DFS to find shortest paths in large unweighted grids, which requires visiting all nodes and causes timeouts.
    > 2. Confusing contiguous vs. non-contiguous: Applying sliding window to find non-contiguous subsequences. Sliding window only works on contiguous subarrays/substrings.

    7. Summary

  • Two Pointers: Converges from ends. Finds pairs/triplets in sorted arrays.
  • Sliding Window: Scans forward in same direction. Finds contiguous subarrays.
  • BFS: Level-order. Best for unweighted shortest paths.
  • DFS: Deep-first. Best for all-paths, cycle checks, and trees.

  • 8. Quiz

    Question 1: If we need to find all paths from root to leaves in a binary tree, should we use DFS or BFS? Answer: DFS. DFS is recursive, which allows you to easily maintain the current path on the call stack and backtrack when you reach a leaf node.
    Question 2: What is the benefit of BFS space complexity over DFS in a very deep, narrow tree? Answer: BFS uses O(W) space (where W is the maximum width of the tree), which is O(1) for a narrow tree. DFS would require O(D) space (where D is the depth), which is O(N) for a narrow tree.
    Question 3: If target sum is 9 and array is [1, 2, 4, 6, 8] (sorted), what is the optimal pointer choice? Answer: Two pointers converging from both ends (left and right). If sum is too small, increment left; if too large, decrement right.
    Question 4: True or False: Greedy algorithms are always faster than DP algorithms. Answer: True (usually). Greedy algorithms run in a single linear pass (O(N)) and use O(1) space, whereas DP requires filling a table, which takes at least O(N) or O(N²) space.
    Question 5: If graph contains negative edge weights, can we use BFS to find the shortest path? Answer: No. Negative weights violate BFS and Dijkstra assumptions. You must use the Bellman-Ford algorithm.