Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»DSA Interview Cheat Sheet: All Patterns You Need to Know
    Data Structures & Algorithms

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    Anirudh KhuranaBy Anirudh Khurana4 April 2026Updated:28 October 2026No Comments8 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Most DSA interviews test the same set of patterns again and again. Whether it is TCS, Infosys, Amazon, Google, or any startup, the problems change but the underlying patterns stay the same. If you learn to recognize these patterns, you can solve most interview problems without memorizing hundreds of questions.

    This cheat sheet covers every major DSA pattern, when to use it, what type of problems fall under it, and the key idea behind each one. Bookmark this page and come back to it before every interview.

    Every problem below links to our written solution, the problem page on LeetCode or GeeksforGeeks so you can solve it yourself, and a video walkthrough on the Code and Debug YouTube channel where one is available. Want the complete problem list in order? Follow it in our Master DSA with LeetCode course.


    Why Patterns Matter More Than Problems

    A lot of students make the mistake of solving 500+ LeetCode problems randomly. They solve a problem, feel good, and move to the next one. But when a slightly different version shows up in an interview, they get stuck.

    The reason is simple. They memorized solutions instead of understanding patterns. Once you know a pattern, you can apply it to 20 or 30 different problems. That is a much better use of your time.

    Think of it this way. If someone asks you to find two numbers that add up to a target in a sorted array, and you know the two pointer pattern, you do not need to have seen that exact problem before. You just recognize the pattern and apply it. Same goes for finding the longest substring without repeating characters. If you know the sliding window, the problem is half solved before you even start coding.

    The patterns listed below cover roughly 90% of what you will face in interviews at Indian product companies, service companies, and startups.


    1. Two Pointers

    The two pointer pattern uses two variables (usually called left and right) that move through an array or string from different positions.

    When to use it:

    • The input is sorted
    • You need to find pairs or triplets that satisfy a condition
    • Useful when comparing elements from both ends of an array

    Common problems:

    ProblemArticleSolveVideo
    Two Sum (sorted array)ReadLeetCodeWatch
    Three SumReadLeetCodeWatch
    Remove Duplicates from Sorted ArrayReadLeetCodeWatch
    Trapping Rain WaterReadLeetCode–

    Key idea:
    Instead of checking every possible pair using two nested loops (O(n²)), you use two pointers that move towards each other based on some condition. This reduces the time complexity to O(n).

    How to spot it:
    If the problem says “sorted array” and asks you to find a pair or group of elements, think two pointers first.


    2. Sliding Window

    Sliding window is one of the most frequently asked patterns in interviews. It works on problems where you need to find something (maximum, minimum, count) within a contiguous subarray or substring.

    When to use it:

    • The problem involves a subarray or substring
    • You need to optimize from a brute force approach

    Common problems:

    ProblemArticleSolveVideo
    Maximum Sum Subarray of Size K–GFG–
    Longest Substring Without Repeating CharactersReadLeetCodeWatch
    Minimum Window Substring–LeetCode–
    Fruits Into BasketsReadLeetCodeWatch
    Longest Repeating Character ReplacementReadLeetCode–

    Key idea:
    Maintain a window (two pointers) and expand or shrink it based on conditions. As the window slides forward, you add new elements and remove old ones, avoiding recomputation.

    Fixed window vs variable window:

    • Fixed window: Size is given (e.g., size K)
    • Variable window: Find smallest/largest window satisfying a condition

    3. Binary Search

    Binary search is not just about searching in a sorted array. Its real power is search space reduction.

    When to use it:

    • Input is sorted
    • You are searching in a monotonic space (increasing/decreasing)

    Common problems:

    ProblemArticleSolveVideo
    Search in Rotated Sorted ArrayReadLeetCodeWatch
    Find Minimum in Rotated Sorted ArrayReadLeetCodeWatch
    Koko Eating BananasReadLeetCode–
    Median of Two Sorted ArraysReadLeetCode–
    Aggressive CowsReadGFG–
    Book Allocation ProblemReadGFG–

    Key idea:
    Check the middle element and eliminate half of the search space each time → O(log n)

    Important for Indian interviews:
    Problems like Book Allocation, Aggressive Cows, Painter’s Partition are extremely common. These are “binary search on answer” problems where you search on the answer space instead of the array.


    4. Hashing

    Hashing is the go-to technique when you need fast lookups, counting, or tracking elements.

    When to use it:

    • Need O(1) lookup
    • Counting frequency of elements
    • Checking if something exists

    Common problems:

    ProblemArticleSolveVideo
    Two Sum (unsorted)ReadLeetCodeWatch
    Longest Consecutive SequenceReadLeetCodeWatch
    Subarray Sum Equals K–LeetCode–
    Valid AnagramReadLeetCodeWatch

    Key idea:
    Use a hash map (dictionary) to store elements and their properties (index, frequency, etc.), converting slow lookups into O(1) operations.

    Watch out for:
    Worst-case hash collisions, but in interviews, average O(1) is what matters.


    5. Stack and Queue

    Stack problems usually involve matching or tracking elements in order. The monotonic stack is a very important subpattern.

    When to use it:

    • Matching brackets
    • Finding next greater/smaller elements
    • Evaluating expressions

    Common problems:

    ProblemArticleSolveVideo
    Valid ParenthesesReadLeetCodeWatch
    Next Greater ElementReadGFGWatch
    Daily Temperatures–LeetCode–
    Largest Rectangle in Histogram–LeetCode–
    Min StackReadLeetCodeWatch
    Evaluate Reverse Polish Notation–LeetCode–
    Implement Queue Using StacksReadLeetCodeWatch

    Key idea:

    • Stack → Last In First Out (LIFO)
    • Monotonic stack → maintain increasing or decreasing order

    Monotonic stack trick:
    For every element, pop elements that are smaller (or larger). The remaining top gives the answer.


    6. Linked List Patterns

    Linked list problems test pointer manipulation skills.

    When to use it:

    • Problem involves a linked list
    • Need in-place rearrangement

    Common problems:

    ProblemArticleSolveVideo
    Reverse Linked ListReadLeetCodeWatch
    Detect CycleReadLeetCodeWatch
    Find MiddleReadLeetCodeWatch
    Merge Two Sorted Lists–LeetCode–
    Remove Nth Node from EndReadLeetCodeWatch
    Palindrome Linked ListReadLeetCode–
    Intersection of Two Linked ListsReadLeetCode–

    Key tricks:

    • Slow and fast pointer (middle, cycle detection)
    • Reverse technique
    • Dummy node (simplifies edge cases)

    7. Trees (BFS and DFS)

    Tree problems are extremely common and mostly follow BFS or DFS.

    When to use BFS:

    • Level order traversal
    • Shortest path
    • Process nodes level by level

    When to use DFS:

    • Path-based problems
    • Checking tree properties
    • Computing depth

    Common problems:

    ProblemArticleSolveVideo
    Maximum DepthReadLeetCodeWatch
    Level Order TraversalReadLeetCodeWatch
    Validate BST–LeetCodeWatch
    Lowest Common Ancestor–LeetCode–
    DiameterReadLeetCodeWatch
    Right Side ViewReadLeetCodeWatch
    Zigzag TraversalReadLeetCode–

    Key idea (DFS):
    Ask:

    • What do I need from the left subtree?
    • What do I need from the right subtree?
    • How do I combine them?

    Key idea (BFS):
    Use a queue and process nodes level by level.


    8. Graph Traversal

    Graphs extend trees with cycles and multiple paths.

    When to use it:

    • Problems involving connections, grids, networks

    Common problems:

    ProblemArticleSolveVideo
    Number of IslandsReadLeetCodeWatch
    Course ScheduleReadLeetCodeWatch
    Rotten OrangesReadLeetCodeWatch
    Word LadderReadLeetCodeWatch
    Number of Provinces–GFGWatch
    Surrounded RegionsReadLeetCodeWatch
    Topological SortReadGFGWatch

    Key ideas:

    • Track visited nodes
    • BFS → shortest path
    • DFS → connected components

    Topological sort:
    Used for task ordering with dependencies (Kahn’s algorithm or DFS).


    9. Recursion and Backtracking

    Backtracking is recursion with undoing choices.

    When to use it:

    • Find all combinations, permutations, subsets

    Common problems:

    ProblemArticleSolveVideo
    SubsetsReadLeetCodeWatch
    Permutations–LeetCode–
    Combination SumReadLeetCodeWatch
    N QueensReadLeetCodeWatch
    Sudoku Solver–LeetCode–
    Word Search–LeetCode–
    Palindrome Partitioning–LeetCode–

    Key idea:
    Think of it as a decision tree.

    Template:

    • Define base case
    • Loop through choices
    • Make a choice
    • Recurse
    • Undo choice (backtrack)

    Pruning:
    Skip invalid paths early to improve performance.


    10. Dynamic Programming

    Dynamic Programming (DP) is optimized recursion.

    When to use it:

    • Overlapping subproblems
    • Optimal substructure

    Common problems:

    ProblemArticleSolveVideo
    Climbing StairsReadLeetCodeWatch
    House RobberReadLeetCodeWatch
    Longest Common Subsequence (LCS)ReadLeetCodeWatch
    0/1 KnapsackReadGFGWatch
    Edit Distance–LeetCode–
    Coin Change–LeetCode–
    Matrix Chain Multiplication–GFG–

    Approach:

    1. Start with recursion
    2. Identify state variables
    3. Add memoization
    4. Convert to tabulation

    Common categories:

    • 1D DP (Linear)
    • 2D DP (Grid)
    • String DP
    • Knapsack variants
    • Interval DP
    • Tree DP

    Important for Indian interviews:

    • 0/1 Knapsack and variants
    • Longest Common Subsequence (LCS)

    11. Greedy

    Greedy algorithms make the best local choice at each step.

    When to use it:

    • When local optimal → global optimal

    Common problems:

    ProblemArticleSolveVideo
    Fractional KnapsackReadGFGWatch
    Activity Selection–GFG–
    Job SequencingReadGFG–
    Minimum PlatformsReadGFGWatch
    Jump GameReadLeetCodeWatch
    Gas Station–LeetCode–

    Key idea:
    Sort input and make locally optimal decisions.


    12. Heap / Priority Queue

    A heap gives quick access to the minimum or maximum element.

    When to use it:

    • Kth largest/smallest problems
    • Top K elements
    • Merging sorted lists

    Common problems:

    ProblemArticleSolveVideo
    Kth Largest Element–LeetCode–
    Top K Frequent Elements–LeetCode–
    Merge K Sorted Lists–LeetCode–
    Find Median from Data Stream–LeetCode–
    Task Scheduler–LeetCode–

    Key idea:

    • Use min heap / max heap
    • Maintain a heap of size K

    13. Bit Manipulation

    Less frequent but important for optimization-heavy problems.

    When to use it:

    • Finding unique elements
    • Checking power of 2
    • Bit counting

    Common problems:

    ProblemArticleSolveVideo
    Single NumberReadLeetCodeWatch
    Power of Two–LeetCode–
    Counting Bits–LeetCode–
    Missing NumberReadLeetCodeWatch

    Key operations:

    • AND, OR, XOR, shifts
    • XOR properties:
      • a XOR a = 0
      • a XOR 0 = a

    How to Use This Cheat Sheet

    Do not try to master all patterns at once.

    • Week 1–2: Two Pointers, Sliding Window, Hashing
    • Week 3–4: Binary Search, Stack, Linked List
    • Week 5–6: Trees, Graphs
    • Week 7–8: Recursion, Backtracking, DP
    • Ongoing: Greedy, Heap, Bit Manipulation

    For each pattern, solve 5 to 8 problems. Focus on understanding, not memorizing.


    One Last Tip

    After solving a problem, spend 2 minutes asking:

    • What pattern did I use?
    • How would I recognize this pattern in a new problem?

    This habit builds pattern recognition faster than solving more problems blindly.

    Start Your DSA Journey

    If you are just getting started with DSA and Python, check out our Zero to Hero Python DSA course. It covers everything from basics to advanced topics in a structured way.

    Already comfortable with the fundamentals and want to practice problems pattern by pattern? Our Master DSA with LeetCode course walks you through 150+ handpicked problems organized exactly by the patterns covered in this cheat sheet.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleSymmetric Tree | Leetcode 101 | Recursive DFS Approach
    Next Article Pandas Python: The Complete Roadmap for Beginners in 2026
    Anirudh Khurana
    • Website

    Anirudh Khurana is the founder of Code and Debug, an Indian EdTech platform that helps students and working professionals crack coding interviews and move into data and AI roles. He teaches DSA, Python, SQL, data engineering and machine learning through live bootcamps and his YouTube channel, Code and Debug, which has over 50,000 subscribers. Every guide on this blog is written or reviewed by him and tested with real code.

    Related Posts

    Data Structures & Algorithms

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025
    Data Structures & Algorithms

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025
    Data Structures & Algorithms

    Bottom View of Binary Tree | BFS with Horizontal Distance Mapping

    2 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • AI & Machine Learning (4)
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Pandas (1)
    • Python (1)
    • Resume (1)
    • SQL (1)
    Recent Posts

    Confusion Matrix in Machine Learning: An Interactive Guide

    21 October 2026

    100 SQL Interview Questions with Queries and Answers (Freshers to Experienced)

    17 October 2026

    Logistic Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    Linear Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    100 Python Interview Questions and Answers for Freshers and Experienced (Free PDF)

    17 October 2026
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.