Problem-Solving Patterns

πŸ“– Introduction

After studying dozens of algorithms and data structures, the real skill is knowing which tool to reach for when facing a new problem. This final article synthesizes everything we've learned into a practical framework for problem-solving.

Over the years, I've noticed that most problems follow recognizable patterns. Once you learn to identify these patterns, what seemed like hundreds of unique problems becomes a handful of techniques applied in different contexts.


🎯 The Problem-Solving Framework

Step-by-Step Approach

spinner

1. Understand the Problem

  • Read carefully: What are the inputs? Outputs? Constraints?

  • Identify the goal: Optimization? Counting? Feasibility?

  • Note constraints: n ≀ 10⁡ suggests O(n log n), n ≀ 20 suggests exponential is okay

2. Work Through Examples

  • Start with simple cases

  • Try edge cases (empty, single element, duplicates)

  • Trace through by hand before coding

3. Identify the Pattern

  • What data structure fits naturally?

  • Does it resemble a known problem type?

  • Can you break it into subproblems?

4. Plan Before Coding

  • Outline the approach in comments

  • Consider time/space complexity upfront

  • Think about edge cases


πŸ“Š Pattern Recognition Guide

Quick Reference Table

Pattern
Key Signal
Example Problems

Two Pointers

Sorted array, find pairs

Two Sum II, Container with Water

Sliding Window

Contiguous subarray/substring

Max sum subarray, longest substring

Binary Search

Sorted data, search space

Search in rotated, Koko eating

BFS

Shortest path, level-order

Word ladder, rotting oranges

DFS

Explore all paths, backtrack

Permutations, word search

Dynamic Programming

Overlapping subproblems

Coin change, LCS

Greedy

Local optimal β†’ global

Activity selection, jump game

Hash Map

O(1) lookup, counting

Two sum, anagrams

Stack

Matching, monotonic

Valid parentheses, daily temps

Heap

Top K, stream median

Kth largest, merge K lists

Union-Find

Connectivity, components

Number of islands, redundant edge

Topological Sort

Dependencies, ordering

Course schedule, alien dict


πŸ” Pattern Deep Dives

Pattern 1: Two Pointers

When to use: Sorted arrays, finding pairs, partition

Classic problems:

  • Container With Most Water

  • 3Sum, 4Sum

  • Remove duplicates

  • Merge sorted arrays


Pattern 2: Sliding Window

When to use: Contiguous subarray/substring with constraint

Classic problems:

  • Maximum sum subarray of size K

  • Longest substring without repeating

  • Minimum window substring

  • Permutation in string


Pattern 3: Binary Search Variations

When to use: Sorted data, monotonic property, search space

Classic problems:

  • Search in rotated array

  • Find first/last position

  • Square root

  • Koko eating bananas


Pattern 4: BFS Level-Order

When to use: Shortest path (unweighted), level-by-level processing

Classic problems:

  • Word ladder

  • Rotting oranges

  • Binary tree level order

  • Walls and gates


Pattern 5: DFS & Backtracking

When to use: Explore all paths, generate combinations/permutations

Classic problems:

  • Subsets, permutations, combinations

  • Word search

  • N-Queens

  • Number of islands


Pattern 6: Dynamic Programming

When to use: Overlapping subproblems, optimal substructure

Classic problems:

  • House robber

  • Coin change

  • Longest increasing subsequence

  • Edit distance


Pattern 7: Monotonic Stack

When to use: Next greater/smaller element, histogram

Classic problems:

  • Daily temperatures

  • Largest rectangle in histogram

  • Trapping rain water

  • Stock span


Pattern 8: Heap / Priority Queue

When to use: Top K, stream processing, scheduling

Classic problems:

  • Top K frequent elements

  • Find median from data stream

  • Merge K sorted lists

  • Meeting rooms II


🧠 Problem-Type Decision Tree

Optimization Problems

Counting Problems

Existence/Feasibility


πŸ“‹ Common Pitfalls

Off-by-One Errors

Integer Overflow

Modifying While Iterating

Reference vs Copy


⏱️ Complexity Quick Reference

By Constraint

n limit
Acceptable Complexity

n ≀ 10

O(n!), O(2^n)

n ≀ 20

O(2^n), O(n^2 * 2^n)

n ≀ 500

O(nΒ³)

n ≀ 10⁴

O(nΒ²)

n ≀ 10⁢

O(n log n)

n ≀ 10⁸

O(n)

n > 10⁸

O(log n), O(1)

By Pattern

Pattern
Time
Space

Two Pointers

O(n)

O(1)

Sliding Window

O(n)

O(k)

Binary Search

O(log n)

O(1)

BFS/DFS

O(V + E)

O(V)

Sorting

O(n log n)

O(n)

Heap operations

O(log n)

O(n)

DP (2D table)

O(n * m)

O(n * m)


🎯 Practice Strategy

Deliberate Practice

  1. Learn one pattern at a time

    • Study the template

    • Solve 5-10 similar problems

    • Review and internalize

  2. Spaced repetition

    • Revisit problems after 1, 3, 7 days

    • Try solving without looking at solution

  3. Time yourself

    • Easy: 15-20 minutes

    • Medium: 25-35 minutes

    • Hard: 40-50 minutes

  4. Review mistakes

    • Why did you miss it?

    • What pattern did you overlook?

    • Add to your pattern library


πŸ“ Quick Checklist

Before submitting:


πŸŽ‰ Series Summary

Congratulations on completing the DSA 101 series! Here's what we covered:

Phase
Topics

Foundation

Complexity analysis, recursion

Linear Structures

Arrays, strings, linked lists, stacks, queues, hash tables

Non-Linear Structures

Trees, heaps, graphs

Algorithms

Sorting, searching, DP, greedy, backtracking

Advanced

Graph algorithms, problem-solving patterns

Key Principles

  1. Understand before coding

  2. Pattern recognition is key

  3. Start simple, optimize later

  4. Practice consistently

  5. Learn from mistakes


πŸ“š Resources for Continued Learning

Books

  • "Introduction to Algorithms" (CLRS)

  • "Algorithm Design Manual" (Skiena)

  • "Elements of Programming Interviews"

Online Platforms

  • LeetCode (patterns and practice)

  • Codeforces (competitive)

  • HackerRank (fundamentals)

Communities

  • r/leetcode

  • Competitive programming Discord servers

  • Study groups


πŸ™ Final Thoughts

Data structures and algorithms aren't just interview topicsβ€”they're tools that help you build better software. The patterns you've learned here will serve you throughout your career, whether you're optimizing database queries, designing system architectures, or building user-facing applications.

Keep practicing, stay curious, and remember: every expert was once a beginner.

Happy coding! πŸš€

Last updated