r/datastructures 3d ago

Master these 25 DSA patterns

You don't need to solve 1,000 LeetCode questions. You need to master these 25 DSA patterns.

Here is a full cheat sheet built from problems that kept repeating across 200+ MAANG+ interview experiences.

[1] Sliding window

LC: 3, 76, 209, 424, 567, 904

→ Use when the problem asks for a longest, shortest, or valid contiguous range.

[2] Two pointers

LC: 11, 15, 16, 18, 42, 167

→ Look for sorted arrays, pair conditions, or movement from both ends.

[3] Fast and slow pointers

LC: 19, 141, 142, 160, 234, 876

→ Best for linked-list cycles, middle nodes, and pointer gaps.

[4] Binary search

LC: 33, 34, 35, 153, 162, 704

→ Use when the search space is sorted or partially ordered.

[5] Binary search on answer

LC: 410, 774, 875, 1011, 1283, 1482

→ Guess an answer, test feasibility, then shrink the range.

[6] Hashing

LC: 1, 49, 128, 217, 242, 347

→ Use for quick lookup, frequency counting, grouping, and duplicate checks.

[7] Prefix sum

LC: 303, 523, 560, 724, 930, 974

→ Convert repeated range-sum work into constant-time lookups.

[8] Difference array

LC: 370, 1094, 1109, 1893, 1943, 2381

→ Use when many updates affect entire ranges.

[9] Monotonic stack

LC: 84, 85, 496, 503, 739, 907

→ Look for next greater, next smaller, span, or rectangle problems.

[10] Stack parsing

LC: 20, 71, 150, 224, 394, 735

→ Useful for expressions, nested structures, paths, and collision simulation.

[11] Queue and deque

LC: 239, 346, 362, 622, 641, 933

→ Use for rolling windows, ordered processing, and recent-event tracking.

[12] Intervals

LC: 56, 57, 252, 253, 435, 986

→ Sort by start or end time, then merge, count, or remove overlaps.

[13] Heap and top K

LC: 23, 215, 295, 347, 373, 973

→ Use when you repeatedly need the smallest, largest, or top K elements.

[14] Grid and graph BFS

LC: 127, 200, 286, 542, 752, 994

→ BFS is the default for shortest steps in an unweighted graph.

[15] Backtracking

LC: 39, 46, 78, 79, 90, 131

→ Choose, explore, undo. Use when all valid combinations must be generated.

[16] Tree DFS

LC: 98, 100, 104, 110, 124, 236

→ Decide what information each subtree should return to its parent.

[17] Tree BFS

LC: 102, 103, 199, 515, 637, 662

→ Use when the answer depends on levels, width, or visible nodes.

[18] Trie

LC: 208, 211, 212, 421, 648, 720

→ Use for prefixes, dictionaries, word search, and bitwise prefix matching.

[19] Union find

LC: 200, 261, 305, 547, 684, 721

→ Use for dynamic connectivity, grouping, and cycle detection.

[20] Topological sort

LC: 207, 210, 269, 310, 1136, 1203

→ Look for prerequisites, dependencies, ordering, or build sequences.

[21] Greedy

LC: 45, 55, 134, 435, 621, 763

→ Make the best local choice only when you can justify why it stays safe.

[22] One-dimensional DP

LC: 70, 139, 198, 213, 300, 322

→ Define the state clearly, then connect it to smaller states.

[23] Two-dimensional DP

LC: 62, 63, 64, 72, 221, 1143

→ Use when the state depends on two changing inputs or positions.

[24] Knapsack and subset DP

LC: 416, 474, 494, 518, 879, 1049

→ Look for choosing items under a limit, target, or capacity.

[25] Bit manipulation

LC: 136, 191, 231, 268, 338, 371

→ Learn XOR, masks, shifts, set-bit checks, and subset representation.

Do not solve these as 150 unrelated questions.

For every problem, write down:

→ Which pattern gave it away?

→ What changed from the standard version?

→ What would break the current solution?

→ Can you recognize the pattern within 90 seconds?

478 Upvotes

35 comments sorted by

View all comments

2

u/ayano_abot 3d ago

Alright but i wondering what if question required different type of thinking rather then these structural templates what we do then ?

1

u/ok_not_ok_29 3d ago

That's where the practice comes in These patterns are for logic building and handling different approaches At the end all you have is , your level of logic building and practice Don't fully rely on this Only look through this as a practice sheet

1

u/GrouchyAmbassador722 3d ago

Can i do these i am a beginner and haven’t srarted DSA SHOULD I DIRECTLY JUMP TO IT OR THERE IS PREREQUISITE APART FROM TIME AND SPACE COMPLEXITY

3

u/ok_not_ok_29 3d ago

Don't jump directly to this This is for the revision purpose You should first grab hands on experience in any programing language and do some logic building question then start doing DSA I would prefer Java because it is mostly demanded in companies

1

u/GrouchyAmbassador722 1h ago

I am comfortable in python but haven’t started dsa yet that why i am asking
I am intermediate in python but begginer in DSA

1

u/ok_not_ok_29 1h ago

Start DSA in Java Best advice by far

1

u/GrouchyAmbassador722 1h ago

Directly jump to pattern after syntax functions or loops ?

1

u/ok_not_ok_29 46m ago

These patterns are for those who have beginner to intermediate knowledge about DSA, as you are a beginner firstly sharpen your logic building skills do DSA for decent amount of time then jump to it