Skip to content

Kali-Prem/500-DSA-Pattern-Problems

Repository files navigation

πŸš€ 500 DSA Pattern Problems

A pattern-first DSA practice roadmap. Each section groups problems by the idea you should learn, not just by difficulty, so you can recognize the same technique when it appears in a new form.

Important: Do not solve random problems. Solve problems pattern by pattern, understand why that pattern works, and remember the trigger for using it. The goal is to recognize the pattern in a new question, not to memorize one exact solution.

πŸ“š How to Use This Repository

Start with one topic folder, read its README.md, then solve the problems in order. Each topic README has one-click links in the last column so you can open any question directly.

No. Topic Problems Start Here
1 πŸ“¦ Arrays 57 Open
2 πŸͺŸ Sliding Window + Prefix Sum 49 Open
3 πŸ”€ Strings + Hashing 60 Open
4 πŸ”— Linked List 50 Open
5 πŸ“š Stack + Queue 49 Open
6 🎯 Binary Search 50 Open
7 🌳 Trees + BST 42 Open
8 ⛰️ Heap + Greedy 20 Open
9 🧩 Backtracking 20 Open
10 πŸ•ΈοΈ Graphs 20 Open
11 🧠 Dynamic Programming 10 Open

πŸ“Œ Current question files: 427

πŸ’­ How to Think Before Solving Any Problem

Do not jump to code first. Spend a few minutes identifying the shape of the problem.

  1. Restate the task Say what the input is, what output is needed, and what counts as a valid answer.

  2. Check constraints Constraints tell you the allowed time complexity. If n is huge, avoid nested loops. If the input is sorted, think binary search or two pointers.

  3. Find the pattern Ask what the problem is really about: searching, counting, grouping, shortest path, choosing best value, recursion, or state transition.

  4. Try a brute force idea Write the simplest possible logic mentally. Then ask which part repeats too much.

  5. Optimize the repeated work Use a hashmap, prefix sum, sliding window, stack, heap, binary search, DFS/BFS, or DP depending on what is being repeated.

  6. Dry run Test your idea on a small example and one edge case before writing code.

  7. Code cleanly Keep variables meaningful. Handle empty input, single element input, duplicates, and boundary indexes.

  8. Analyze Always finish with time and space complexity.

πŸ”Ž Pattern Recognition Guide

Use these signals to choose a technique quickly.

Signal in Problem First Technique to Try
πŸ“ˆ Sorted array, find pair/range Two pointers or binary search
πŸͺŸ Subarray or substring with condition Sliding window
βž• Sum/count over many ranges Prefix sum
πŸ”‘ Need fast lookup or frequency HashMap / HashSet
πŸ“š Next greater/smaller, remove while condition holds Monotonic stack
⛰️ Top K, smallest/largest repeatedly Heap
🎯 Minimum/maximum answer with yes/no check Binary search on answer
🧩 All combinations or permutations Backtracking
πŸ—ΊοΈ Grid, connected components, shortest unweighted path BFS / DFS
πŸ“Œ Dependencies or ordering Topological sort
🧠 Repeated choices with optimal result Dynamic programming
πŸ”— Linked list cycle or middle Slow and fast pointers
πŸ“… Intervals overlap or merging Sort by start/end, then greedy

⚑ Best Techniques to Solve Faster

πŸ“¦ Arrays

Think about index movement. If the array is sorted, use two pointers or binary search. If you need repeated range values, use prefix or suffix arrays.

πŸͺŸ Sliding Window

Use when the answer is a continuous subarray or substring. Expand the right pointer, shrink the left pointer when the condition breaks, and update the answer at the right moment.

πŸ”€ Strings + Hashing

Convert string problems into counts, indexes, or character states. Hash maps help with anagrams, frequency, first occurrence, and duplicate detection.

πŸ”— Linked List

Draw pointer movement before coding. For cycle, middle, or nth-from-end questions, slow-fast pointers are usually the cleanest start.

πŸ“š Stack + Queue

Use stack when the latest item must be resolved first. Use monotonic stack when elements wait for a future greater/smaller value. Use queue/BFS when processing level by level.

🎯 Binary Search

Binary search is not only for arrays. If the answer is numeric and you can check whether a value is possible, binary search the answer.

🌳 Trees + BST

Most tree problems are DFS with information passed down or returned up. For BSTs, use the sorted property to prune unnecessary branches.

⛰️ Heap + Greedy

Use heap when you repeatedly need the smallest or largest item. Use greedy when a local best choice can be proven to keep the future valid.

🧩 Backtracking

Think in choices: choose, explore, undo. Add pruning rules early so you do not explore impossible paths.

πŸ•ΈοΈ Graphs

Model nodes and edges first. Use BFS for shortest path in unweighted graphs, DFS for components, Union-Find for connectivity, and topological sort for dependencies.

🧠 Dynamic Programming

Use DP when the same subproblem appears again and choices affect future results. Define the state, transition, base case, and answer location.

🧠 How to Remember Questions

The goal is not to memorize exact solutions. Remember the pattern trigger and the key trick.

  1. After solving, write one sentence: "This problem is solved by ___ because ___."
  2. Re-solve the same problem after 1 day, 3 days, and 7 days.
  3. Group mistakes by type: wrong pattern, missed edge case, bad index handling, or weak implementation.
  4. Keep a small list of "signature problems" for each pattern.
  5. Before looking at a solution, spend at least 15 minutes forming a brute force and optimized idea.
  6. After reading a solution, close it and code again from memory.
  7. Compare similar problems together so the pattern becomes visible.

⏱️ The 15-Minute Rule

When stuck:

  1. First 5 minutes: understand examples and constraints.
  2. Next 5 minutes: write brute force logic.
  3. Next 5 minutes: identify repeated work and choose a pattern.

If you still cannot solve it, read only the hint or approach, then code the solution yourself.

πŸ” Revision Plan

Use this cycle for strong retention:

Day Work
🟒 Day 1 Solve 5 new problems from one pattern
πŸ”΅ Day 2 Revise yesterday's problems and solve 3 new ones
🟑 Day 4 Re-solve the problems you got wrong
🟣 Day 7 Mix problems from 2-3 older patterns
🟠 Day 14 Do timed practice without hints
πŸ”΄ Day 30 Revisit only weak patterns

🧾 Problem-Solving Template

Use this checklist before submitting:

1. What is the input and output?
2. What are the constraints?
3. Is the data sorted, repeated, connected, or continuous?
4. What brute force solution works?
5. What work is repeated?
6. Which pattern removes that repeated work?
7. What are the edge cases?
8. What is the time and space complexity?

πŸ›£οΈ Recommended Study Order

  1. πŸ“¦ Arrays
  2. πŸͺŸ Sliding Window + Prefix Sum
  3. πŸ”€ Strings + Hashing
  4. πŸ”— Linked List
  5. πŸ“š Stack + Queue
  6. 🎯 Binary Search
  7. 🌳 Trees + BST
  8. ⛰️ Heap + Greedy
  9. 🧩 Backtracking
  10. πŸ•ΈοΈ Graphs
  11. 🧠 Dynamic Programming

Follow the order if you are building fundamentals. If you are preparing for interviews soon, rotate between one easy, two medium, and one revision problem daily.

Releases

No releases published

Packages

 
 
 

Contributors