LeetCode Question · Oct 2024 · USA

DSA Patterns you need to know !!!

762 upvotes 28,828 views 62 replies

Question Details

After solving lot of DSA problems, I\u2019ve noticed some key patterns that are important for coding interviews. At the end of this article, I have also included links to some...

Full Details

After solving lot of DSA problems, I\u2019ve noticed some key patterns that are important for coding interviews.
At the end of this article, I have also included links to some of the best LeetCode articles that I found helpful for better understanding.



1. Fast and Slow Pointer

Description: This technique uses two pointers moving at different speeds to solve problems involving cycles, such as finding the middle of a list, detecting loops, or checking for palindromes.


2. Overlapping Intervals

Description: Intervals are often manipulated through sorting and merging based on their start and end times.


3. Prefix Sum

Description: Prefix Sums/Products are techniques that store cumulative sums or products up to each index, allowing for quick subarray range queries.


4. Sliding Window

Description: A sliding window is a subarray or substring that moves over data to solve problems efficiently in linear time.

Fixed Size

Variable Size


5. Two Pointers

Description: The two pointers technique involves having two different indices move through the input at different speeds to solve various array or linked list problems.

  • [Two Sum II -

Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)
- Dutch National Flag: Sort Colors
- Next Permutation
- Bag of Tokens
- Container with most water
- Trapping Rain Water


6. Cyclic Sort (Index-Based)

Description: Cyclic sort is an efficient approach to solve problems where numbers are consecutively ordered and must be placed in the correct index.
- Missing Number
- Find Missing Numbers
- Set Mismatch
- First Missing Positive


7. Reversal of Linked List (In-place)

Description: Reversing a linked list in place without using extra space is key for problems that require in-place list manipulations.


8. Matrix Manipulation

Description: Problems involving 2D arrays (matrices) are often solved using row-column traversal or manipulation based on matrix properties.


9. Breadth First Search (BFS)

Description: BFS explores nodes level by level using a queue. It is particularly useful for shortest path problems.


10. Depth First Search (DFS)

Description: DFS explores as far as possible along a branch before backtracking. It\'s useful for graph traversal, pathfinding, and connected components.


11. Backtracking

Description: Backtracking helps in problems where you need to explore all potential solutions, such as solving puzzles, generating combinations, or finding paths.


12. Modified Binary Search

Description: A modified version of binary search that applies to rotated arrays, unsorted arrays, or specialized conditions.


13. Bitwise XOR

Description: XOR is a powerful bitwise operator that can solve problems like finding single numbers or efficiently pairing elements.


14. Top \'K\' Elements

Description: This pattern uses heaps or quickselect to efficiently find the top \'K\' largest/smallest elements from a dataset.


15. K-way Merge

Description: The K-way merge technique uses a heap to efficiently merge multiple sorted lists or arrays.
- Find K Pairs with Smallest Sums
- Kth Smallest Element in a Sorted Matrix
- Merge K Sorted Lists
- Smallest Range: Smallest Range Covering Elements from K Lists


16. Two Heaps

Description: This pattern uses two heaps (max heap and min heap) to solve problems involving tracking medians and efficiently managing dynamic data.


17. Monotonic Stack

Description: A monotonic stack helps solve range queries by maintaining a stack of elements in increasing or decreasing order.


18.

Trees

Level Order Traversal (BFS in Binary Tree)

Tree Construction

Height related Problems

Root to leaf path problems

Ancestor problem

Binary Search Tree


19.

DYNAMIC PROGRAMMING

Take / Not take (DP)

Description: Solve optimization problems like selecting items with the max/min value under certain constraints.

Infinite Supply (DP)

Description: Similar to the 0/1 knapsack, but items can be chosen multiple times.

Longest Increasing subsequence

Description: It involves finding the longest subsequence of a given sequence where the elements are in ascending order

DP on Grids

Description: Dynamic Programming on matrices involves solving problems that can be broken down into smaller overlapping subproblems within a matrix.

DP on Strings

Description: It Involves 2 strings, whenever you are considering two substrings/subsequence from given two strings, concentrate on what happens when the last characters of the two substrings are same, i.e, matching.

DP on Stocks

Description: It focuses on maximizing profit from buying and selling stocks over time while considering constraints.
- Buy and Sell Stocks ii
- Buy and Sell Stocks iii
- Buy and Sell Stocks iv
- Buy and Sell Stocks with Cooldown
- Buy and Sell Stocks with Transaction fee

Partition DP (MCM)

Description: It Involves a sequence that needs to be divided into partitions in an optimal way. The goal is often to minimize or maximize a cost function, such as computation time, multiplications, or some other metric, by exploring all possible partitions and combining results from subproblems.


20.

Graphs

Topological Sort

Description: Topological sorting is useful for tasks that require dependency resolution (InDegree) in directed acyclic graphs (DAGs).

Union Find (Disjoint Set)

Description: Union-Find (or Disjoint Set) is used to solve problems involving connectivity or grouping, often in graphs.

Graph Algorithms

Description: Advanced graph algorithms are used to solve complex problems involving shortest paths, minimum spanning trees, and graph cycles.


21. Greedy

Description: Greedy algorithms make local optimal choices at each step, which lead to a global optimal solution for problems like scheduling and resource allocation.


22. Design Data Structure

Description: It involves building custom data structures to efficiently handle specific operations, like managing data access, updates, and memory usage. Focusing on optimizing performance and resource management.

-----------------------------

Some Useful Articles on LeetCode for Better Understanding!

Two Pointers

Sliding Window

Greedy

  • [Greedy for Beginners: Problems &

Sample Solutions](https://leetcode.com/discuss/general-discussion/669996/greedy-for-beginners-problems-sample-solutions)
- Top Greedy Questions

Linked List

Trees

Binary Search

Dynamic Programming (DP)

Graphs

Bit Manipulation


Happy LeetCoding !

Free preview — 6 questions shown. Unlock all Twitter/X questions →

About This Question

This is a reported interview question from a twitter/x interview for a eng manager role during the oa round reported in 2024.

It covers the following topics: Arrays, Backtracking, Binary Search, Binary Tree, Bit Manipulation, Dynamic Programming, Graph, Greedy, Hash Table, Heap, Linked List, Matrix, Queue, Recursion, Sliding Window, Sorting, Sql, Stack, Strings, Two Pointers, Union Find .

Difficulty rating: Easy

About Twitter/X Interview Reports

This question was reported by a candidate who interviewed at Twitter/X. LeakCode aggregates interview reports from 10+ sources, including 1Point3Acres, Glassdoor, LeetCode Discuss, Blind, Reddit, Indeed, and Nowcoder. Each report is translated where necessary, deduplicated against existing entries, and tagged by company, role, round type, and reporting date.

Use this question as one calibration data point, not a memorization target. Companies typically rotate their question pools every 2-4 months; the exact wording of a 2024 question may differ from what you encounter today. The underlying pattern, difficulty level, and follow-up depth at Twitter/X are the higher-signal extractions to take from this report.

For broader preparation context, the Twitter/X interview process typically includes a recruiter screen, one or two technical phone screens, and a 4-5 round on-site loop covering coding, system design (at L4+ levels), and behavioral. Reports tagged on LeakCode show the round-by-round distribution and typical difficulty calibration. To browse questions filtered by round type and seniority, use the company hub linked above.

How To Practice This Type of Question

Solve similar problems on LeetCode under timed conditions (25-35 minutes per medium difficulty). The goal is pattern recognition: recognize the underlying technique (sliding window, two-pointer, BFS, memoized recursion, etc.) within 60-90 seconds of reading. Strong candidates verbalize their hypothesis out loud before coding, then iterate based on feedback. Weak candidates dive into implementation immediately, lose time on the wrong approach, and run out of time for follow-ups.

Companies update their question pools every 2-4 months. The exact wording of any given question may have been retired by the time you interview. Focus your prep on the pattern, not the specific problem. The patterns that appear in Twitter/X reports consistently are the ones worth investing in; one-off niche problems are not.

During Your Twitter/X Round

Apply the standard interview round template: clarify requirements (2-3 minutes), state your approach out loud and confirm direction with the interviewer (3-5 minutes), code with narration (15-25 minutes), test with concrete examples including edge cases (5 minutes), discuss optimization or trade-offs if time permits (5 minutes). This template is universally accepted across FAANG and adjacent companies; deviating from it produces weaker interviewer feedback signal.

The single most predictive failure mode in Twitter/X reports tagged "no hire": not asking clarifying questions. Interviewers are explicitly trained to weight this. Strong candidates ask 3-5 clarifying questions even on problems that look obvious; weak candidates dive into code immediately. The clarifying-question check is often the first signal recorded in the interviewer's written notes.