DEV Community

Dev Cookies
Dev Cookies

Posted on

The Ultimate DSA Patterns Guide for Top Tech Interviews

Mastering Data Structures and Algorithms (DSA) patterns is crucial for succeeding in technical interviews at top tech companies like Google, Amazon, Microsoft, Facebook, Apple, and others. This comprehensive guide covers all essential patterns with extensive LeetCode problem links to help you prepare systematically.

Table of Contents

  1. Two Pointers Pattern
  2. Sliding Window Pattern
  3. Fast & Slow Pointers (Cycle Detection)
  4. Merge Intervals Pattern
  5. Cyclic Sort Pattern
  6. In-place Reversal of LinkedList
  7. Tree Breadth First Search (BFS)
  8. Tree Depth First Search (DFS)
  9. Two Heaps Pattern
  10. Subsets Pattern
  11. Modified Binary Search
  12. Bitwise XOR Pattern
  13. Top K Elements Pattern
  14. K-way Merge Pattern
  15. Topological Sort Pattern
  16. Dynamic Programming Patterns
  17. Graph Algorithms
  18. Backtracking Pattern
  19. Greedy Algorithms
  20. Stack and Queue Patterns

Two Pointers Pattern

The two pointers pattern is used when you need to find a pair of elements or compare elements from both ends of a sorted array/string.

When to Use:

  • Sorted arrays or strings
  • Finding pairs that meet certain criteria
  • Removing duplicates
  • Comparing elements from both ends

LeetCode Problems:

Easy:

Medium:

Hard:


Sliding Window Pattern

Used for problems involving subarrays or substrings with a specific condition, typically to find the optimal window size.

When to Use:

  • Finding subarrays/substrings with specific properties
  • Maximum/minimum window size problems
  • Problems with contiguous sequences

LeetCode Problems:

Easy:

Medium:

Hard:


Fast & Slow Pointers (Cycle Detection)

This pattern uses two pointers moving at different speeds to detect cycles in linked lists or find middle elements.

When to Use:

  • Detecting cycles in linked lists
  • Finding middle of linked list
  • Palindrome linked list problems
  • Happy number problems

LeetCode Problems:

Easy:

Medium:


Merge Intervals Pattern

Used for problems dealing with overlapping intervals, merging them, or finding gaps.

When to Use:

  • Overlapping intervals
  • Scheduling problems
  • Range problems
  • Calendar applications

LeetCode Problems:

Easy:

Medium:


Cyclic Sort Pattern

Used when dealing with arrays containing numbers in a given range, typically to find missing or duplicate numbers.

When to Use:

  • Arrays with numbers in specific range [1, n] or [0, n-1]
  • Finding missing or duplicate numbers
  • Placing numbers in their correct positions

LeetCode Problems:

Easy:

Medium:


In-place Reversal of LinkedList

Pattern for reversing linked lists without using extra space.

When to Use:

  • Reversing linked lists
  • Reversing portions of linked lists
  • Rotating linked lists

LeetCode Problems:

Easy:

Medium:

Hard:


Tree Breadth First Search (BFS)

Used for level-order traversal of trees and finding shortest paths in unweighted graphs.

When to Use:

  • Level-order traversal
  • Finding minimum depth/height
  • Connecting nodes at same level
  • Binary tree problems requiring level-wise processing

LeetCode Problems:

Easy:

Medium:


Tree Depth First Search (DFS)

Used for exploring all paths in a tree, finding paths with specific sums, or tree validation.

When to Use:

  • Path sum problems
  • Tree validation
  • Finding all paths
  • Maximum depth problems

LeetCode Problems:

Easy:

Medium:

Hard:


Two Heaps Pattern

Used when you need to find medians, or divide elements into two groups dynamically.

When to Use:

  • Finding median in a stream
  • Dividing elements into two equal groups
  • Sliding window median problems

LeetCode Problems:

Hard:

Medium:


Subsets Pattern

Used for generating all possible subsets, permutations, or combinations.

When to Use:

  • Generating all subsets/combinations
  • Backtracking problems
  • Power set generation

LeetCode Problems:

Medium:


Modified Binary Search

Binary search variations for rotated arrays, finding peaks, or search in infinite arrays.

When to Use:

  • Rotated sorted arrays
  • Finding peak elements
  • Search in infinite arrays
  • Finding elements in bitonic arrays

LeetCode Problems:

Easy:

Medium:

Hard:


Bitwise XOR Pattern

Uses XOR properties to solve problems involving duplicates or missing numbers.

When to Use:

  • Finding single number in pairs
  • Missing number problems
  • Complement problems

LeetCode Problems:

Easy:

Medium:


Top K Elements Pattern

Uses heaps to find top K elements, Kth largest/smallest elements.

When to Use:

  • Finding top K elements
  • Kth largest/smallest problems
  • Frequency-based problems

LeetCode Problems:

Easy:

Medium:

Hard:


K-way Merge Pattern

Used for merging K sorted arrays or lists.

When to Use:

  • Merging multiple sorted arrays/lists
  • Finding smallest range covering elements from K lists

LeetCode Problems:

Medium:

Hard:


Topological Sort Pattern

Used for ordering tasks with dependencies, finding course schedules.

When to Use:

  • Task scheduling with dependencies
  • Course prerequisites
  • Build order problems
  • Detecting cycles in directed graphs

LeetCode Problems:

Medium:


Dynamic Programming Patterns

DP is used for optimization problems with overlapping subproblems and optimal substructure.

Common DP Patterns:

Linear DP:

Easy:

Medium:

2D DP:

Medium:

Knapsack Pattern:

Hard:


Graph Algorithms

Essential graph traversal and shortest path algorithms.

DFS/BFS on Graphs:

Medium:

Union Find:

Medium:

Shortest Path:

Medium:


Backtracking Pattern

Used for exploring all possible solutions and choosing the optimal one.

When to Use:

  • Generating permutations/combinations
  • Solving puzzles (N-Queens, Sudoku)
  • Path finding with constraints

LeetCode Problems:

Medium:

Hard:


Greedy Algorithms

Greedy algorithms make locally optimal choices at each step, hoping to find a global optimum.

When to Use:

  • Optimization problems where greedy choice leads to optimal solution
  • Activity selection problems
  • Minimum spanning tree
  • Huffman coding type problems

LeetCode Problems:

Easy:

Medium:

Hard:


Stack and Queue Patterns

Stacks and queues are fundamental data structures used in many algorithmic patterns.

Stack Patterns:

Easy:

Medium:

Hard:

Queue Patterns:

Easy:

Medium:


Advanced Patterns and Miscellaneous

Trie (Prefix Tree):

Medium:

Hard:

Segment Tree / Binary Indexed Tree:

Medium:

Hard:

Design Patterns:

Easy:

Medium:

Hard:

Math and Number Theory:

Easy:

Medium:


Company-Specific Pattern Focus

Google Favorites:

  • Dynamic Programming (All variations)
  • Graph algorithms (BFS/DFS, Shortest Path)
  • Tree problems (especially BST)
  • System Design related problems
  • String manipulation and parsing

Amazon Favorites:

  • Two pointers and sliding window
  • Tree and graph traversals
  • Dynamic Programming
  • Heap/Priority Queue problems
  • Design problems

Microsoft Favorites:

  • Dynamic Programming
  • Graph algorithms
  • Tree problems
  • String problems
  • Design problems
  • Backtracking

Facebook (Meta) Favorites:

  • Tree and graph problems
  • Dynamic Programming
  • String manipulation
  • Two pointers
  • BFS/DFS problems

Apple Favorites:

  • Tree problems
  • Dynamic Programming
  • String problems
  • Array problems
  • Design problems

Study Strategy and Tips

Phase 1: Foundation (Weeks 1-4)

Focus on basic patterns:

  1. Two Pointers
  2. Sliding Window
  3. Binary Search
  4. Tree BFS/DFS
  5. Basic DP

Phase 2: Intermediate (Weeks 5-8)

  1. Advanced DP patterns
  2. Graph algorithms
  3. Heaps and advanced data structures
  4. Backtracking
  5. Greedy algorithms

Phase 3: Advanced (Weeks 9-12)

  1. Complex DP problems
  2. Advanced graph algorithms
  3. System design problems
  4. Company-specific practice
  5. Mock interviews

Practice Tips:

  1. Pattern Recognition: Focus on identifying patterns rather than memorizing solutions
  2. Time Management: Practice with time constraints (45 minutes per problem max)
  3. Code Quality: Write clean, readable code with proper variable names
  4. Edge Cases: Always consider edge cases and handle them explicitly
  5. Communication: Practice explaining your approach clearly
  6. Multiple Solutions: Try to find multiple approaches for the same problem
  7. Complexity Analysis: Always analyze time and space complexity
  8. Mock Interviews: Practice with peers or use platforms like Pramp, InterviewBit

Problem Selection Strategy:

  1. Start with Easy: Build confidence with easier problems
  2. Pattern Grouping: Solve 5-10 problems of the same pattern consecutively
  3. Mixed Practice: After mastering patterns, do random problem solving
  4. Company Focus: In final weeks, focus on company-specific problems
  5. Review: Regularly review solved problems to maintain familiarity

Common Mistakes to Avoid:

  1. Jumping to hard problems too early
  2. Not understanding the underlying pattern
  3. Focusing only on getting accepted solution
  4. Not practicing with time constraints
  5. Ignoring system design aspects
  6. Not communicating during problem solving
  7. Poor time management during interviews

Conclusion

Mastering these DSA patterns is essential for technical interview success. The key is consistent practice, pattern recognition, and understanding when and how to apply each technique. Focus on quality over quantity - it's better to deeply understand 200 problems across all patterns than to solve 500 problems without pattern recognition.

Remember that interviews are not just about getting the right answer, but also about demonstrating your problem-solving approach, code quality, and communication skills. Practice explaining your thought process clearly and writing clean, efficient code.

Good luck with your interview preparation! Keep practicing consistently, and you'll definitely see improvement in your problem-solving abilities.


This guide contains 300+ carefully curated LeetCode problems across all major DSA patterns. Bookmark this page and use it as your comprehensive study guide for technical interviews at top tech companies.

Top comments (0)