Skip to content

Latest commit

 

History

History
97 lines (68 loc) · 1.63 KB

File metadata and controls

97 lines (68 loc) · 1.63 KB

Core DSA Patterns Every Engineer Should Recognize

Most "hard" DSA problems are not hard because of logic. They are hard because the pattern is not obvious.

Strong problem solvers identify structure before writing code.


1. Sliding Window

When to Use:

  • Subarray / substring problems
  • Contiguous elements
  • Fixed or variable window size
  • Optimize from O(n²) → O(n)

Common Signals:

  • "Longest substring"
  • "Maximum sum subarray"
  • "At most K distinct characters"

2. Two Pointers

When to Use:

  • Sorted arrays
  • Pair problems
  • Partitioning
  • Palindrome checks

Common Signals:

  • "Find two numbers that sum to X"
  • "Remove duplicates"
  • "Container with most water"

3. Prefix / Suffix Sum

When to Use:

  • Repeated range queries
  • Subarray sum problems
  • Cumulative calculations

Common Signals:

  • "Sum between L and R"
  • "Subarray sum equals K"
  • "Equilibrium index"

4. Binary Search on Answer

When to Use:

  • Search space is monotonic
  • You can validate a condition
  • Optimize brute force range

Common Signals:

  • "Minimum possible"
  • "Maximum feasible"
  • "Capacity / allocation problems"

5. Dynamic Programming (State + Transition)

When to Use:

  • Overlapping subproblems
  • Optimal substructure
  • Decision at each index/state

Key Questions:

  • What is my state?
  • What decisions do I have?
  • What is the transition?
  • What is the base case?

The Real Skill

DSA mastery is not memorizing solutions.

It is recognizing:

  • Structure
  • Constraints
  • State modeling
  • Pattern reuse

Pattern recognition reduces thinking cost. That is real DSA maturity.

— CodeWithIshwar