Skip to content

Repository files navigation


πŸ’‘ Core Philosophy

Important

Stop grinding random LeetCode questions. Blindly memorizing hundreds of solutions leads to interview anxiety and failure whenever a slight variation appears.

Instead, master patterns. When you understand why a technique works, what triggers it, and how to optimize repeated work, you can recognize and solve unseen interview problems effortlessly.

🎯 Patterns Covered 🟒 Easy Problems 🟑 Medium Problems πŸ”΄ Hard Problems πŸš€ Total Implemented
11 Core Modules 121 Foundations 227 Interview Core 79 Bar-Raisers 427+ Full Solutions

πŸ—ΊοΈ Visual Learning Roadmap

Follow this progressive order to build rock-solid algorithmic intuition from linear structures to advanced optimization.

flowchart TD
    classDef foundation fill:#0a192f,stroke:#00f5ff,stroke-width:2px,color:#00f5ff;
    classDef linear fill:#0d2040,stroke:#2563eb,stroke-width:2px,color:#7ef9ff;
    classDef search fill:#1f1338,stroke:#a855f7,stroke-width:2px,color:#e9d5ff;
    classDef advanced fill:#1e293b,stroke:#00FF00,stroke-width:2px,color:#4ade80;

    subgraph Phase1 ["⚑ Phase 1: Foundations"]
        A["πŸ“¦ 01. Arrays & Two Pointers"]:::foundation --> B["πŸͺŸ 02. Sliding Window & Prefix Sum"]:::foundation
    end

    subgraph Phase2 ["πŸ”— Phase 2: Linear Data Structures"]
        B --> C["πŸ”€ 03. Strings & Hashing"]:::linear
        C --> D["πŸ”— 04. Linked Lists"]:::linear
        D --> E["πŸ“š 05. Stacks & Queues"]:::linear
    end

    subgraph Phase3 ["🎯 Phase 3: Search & Hierarchies"]
        E --> F["🎯 06. Binary Search"]:::search
        F --> G["🌳 07. Trees & BST"]:::search
    end

    subgraph Phase4 ["🧠 Phase 4: Non-Linear & Optimization"]
        G --> H["⛰️ 08. Heaps & Greedy"]:::advanced
        H --> I["🧩 09. Backtracking"]:::advanced
        I --> J["πŸ•ΈοΈ 10. Graphs"]:::advanced
        J --> K["🧠 11. Dynamic Programming"]:::advanced
    end
Loading

πŸ“Š Curriculum Matrix

Every topic contains carefully curated problems classified into Easy, Medium, and Hard. Click [Explore Module βž”] to access topic-specific roadmaps and direct problem links.

No. Module / Topic Core Concepts & Sub-Patterns 🟒 Easy 🟑 Med πŸ”΄ Hard πŸ“š Total Action
01 πŸ“¦ Arrays Prefix Sum, Kadane, Two Pointers, In-Place Manipulation, Dutch Flag 19 35 3 57 Explore Module βž”
02 πŸͺŸ Sliding Window & Prefix Fixed/Dynamic Window, Range Frequency, Subarray Sums, Suffix Max 16 25 8 49 Explore Module βž”
03 πŸ”€ Strings & Hashing Anagrams, Rolling Hash, Frequency Counting, Palindromes, State Tracking 20 26 14 60 Explore Module βž”
04 πŸ”— Linked List Fast & Slow Pointers, In-Place Reversal, Cycle Detection, Sentinel Nodes 16 25 9 50 Explore Module βž”
05 πŸ“š Stack & Queue Monotonic Stack, Next Greater Element, Queue BFS, Parentheses Validation 16 22 11 49 Explore Module βž”
06 🎯 Binary Search Search in Rotated Arrays, Binary Search on Answer Space, Peak Elements 10 24 16 50 Explore Module βž”
07 🌳 Trees & BST Tree Traversals (DFS/BFS), Diameter, LCA, BST Properties, Path Sums 18 17 7 42 Explore Module βž”
08 ⛰️ Heap & Greedy Min/Max Heaps, Top-K Frequent, Interval Scheduling, Greedy Choice Property 4 12 4 20 Explore Module βž”
09 🧩 Backtracking Permutations, Combinations, Subsets, Constraint Propagation, Pruning 0 16 4 20 Explore Module βž”
10 πŸ•ΈοΈ Graphs BFS/DFS, Cycle Detection, Topological Sort, Shortest Path, Disjoint Set Union 1 17 2 20 Explore Module βž”
11 🧠 Dynamic Programming 1D Memoization/Tabulation, 2D Grid DP, Knapsack Variations, Subsequences 1 8 1 10 Explore Module βž”
TOTAL All 11 Modules Combined Comprehensive Full-Stack Interview Mastery 121 227 79 427 Begin Practice πŸš€

πŸ’­ The 8-Step Algorithmic Blueprint

Follow this battle-tested mental sequence for every problem before touching the keyboard:

                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 1. Restate Input & Output    β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 2. Audit Constraints & Big-O β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 3. Identify Pattern Triggers β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 4. Mental Brute Force Model  β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 5. Optimize Redundant Work   β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 6. Dry Run on Edge Cases     β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 7. Write Clean, Modular Code β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚ 8. Analyze Time & Space      β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Restate Input & Output: Clearly specify types, expected null/empty behaviors, and return values.
  2. Audit Constraints: Determine your allowed complexity budget.
    • $N \le 10^5 \to O(N)$ or $O(N \log N)$
    • $N \le 10^3 \to O(N^2)$
    • $N \le 20 \to O(2^N)$ or $O(N!)$ (Backtracking)
  3. Identify Pattern Triggers: Is it sorted? Are continuous elements queried? Are choices repetitive?
  4. Mental Brute Force: What is the most obvious solution? Why is it slow?
  5. Optimize Redundant Work: Replace nested loops with HashMaps, Two Pointers, Monotonic Stacks, or DP state tables.
  6. Dry-Run Edge Cases: Check empty arrays, single elements, identical values, negative numbers, and boundary indices.
  7. Clean Implementation: Use intuitive naming, handle base cases upfront, and avoid convoluted nested branches.
  8. Complexity Audit: Explicitly state both Time and Space complexities ($O(N)$, $O(1)$, etc.).

πŸ”Ž Master Pattern Recognition Guide

Keep this cheat sheet handy during interview preparation to instantly connect question cues to optimal techniques:

Problem Signal & Cue First Pattern to Try Time Budget Signature Problem
Sorted array, search target or pair Two Pointers / Binary Search $O(N)$ / $O(\log N)$ Two Sum II, 3Sum
Continuous subarray / substring condition Sliding Window $O(N)$ Longest Substring Without Repeating Characters
Repeated range sum or query count Prefix Sum / Difference Array $O(N)$ build, $O(1)$ query Subarray Sum Equals K
Instant lookups, anagrams, frequency HashMap / HashSet / Frequency Array $O(N)$ Group Anagrams
Next greater / smaller element, histograms Monotonic Stack $O(N)$ Daily Temperatures, Trapping Rain Water
Top K elements, dynamic median Min/Max Heap (PriorityQueue) $O(N \log K)$ Kth Largest Element
Monotonic predicate ("Is value $X$ valid?") Binary Search on Answer Space $O(N \log(\text{range}))$ Koko Eating Bananas
Generate all permutations / combinations Backtracking with Pruning Exponential N-Queens, Subsets
Shortest unweighted path, level order BFS (Queue) $O(V + E)$ Binary Tree Level Order, Word Ladder
Connected components, cycle, tree paths DFS / Disjoint Set Union (DSU) $O(V + E)$ Number of Islands
Task scheduling with prerequisites Topological Sort (Kahn's / DFS) $O(V + E)$ Course Schedule
Overlapping subproblems + optimal substructure Dynamic Programming Polynomial Coin Change, Longest Common Subsequence
Interval overlaps, merging, scheduling Sort by Start/End + Greedy $O(N \log N)$ Merge Intervals
Cycle detection or midpoint in Linked List Floyd's Tortoise & Hare (Fast/Slow) $O(N)$ time, $O(1)$ space Linked List Cycle

⚑ Module-by-Module Master Playbook

πŸ“¦ 1. Arrays & Two Pointers (Click to expand)
  • Core Intuition: Understand index transitions. If unsorted, consider hash tables, prefix sums, or sorting. If sorted, two pointers (left and right) allow skipping unnecessary comparisons.
  • Key Sub-Patterns:
    • Opposite-Direction Pointers: Palindrome checks, 2Sum sorted, container with most water.
    • Same-Direction Pointers (Read/Write): Removing duplicates, moving zeroes in-place.
    • Dutch National Flag (3 Pointers): Partitioning values ($<$, $=$, $>$ pivot) in a single pass.
  • Common Pitfalls: Off-by-one errors on boundary conditions, integer overflow on array sums.
πŸͺŸ 2. Sliding Window & Prefix Sum (Click to expand)
  • Core Intuition: Solves subarray and substring challenges without recomputing contiguous elements from scratch.
  • Key Sub-Patterns:
    • Fixed Window: Window size $K$ remains constant; slide right by adding arr[r] and subtracting arr[l].
    • Variable Window: Expand r to satisfy condition; shrink l when invalid to restore constraint.
    • Prefix Sum + HashMap: When subarrays can have negative numbers, sliding window fails; store cumulative sum frequencies.
  • Common Pitfalls: Forgetting to update the optimal answer before/after shrinking, mixing up 0-indexed prefix arrays.
πŸ”€ 3. Strings & Hashing (Click to expand)
  • Core Intuition: Strings are character arrays. Count frequencies with constant space ($O(1)$ auxiliary memory using int[26] or int[128]).
  • Key Sub-Patterns:
    • Anagram Detection: Sorted key strings or frequency vectors.
    • Rolling Hash (Rabin-Karp): Substring matching in $O(N)$ average time.
    • State Encoding: Transforming board/string states into hashable string representations.
  • Common Pitfalls: Creating too many intermediate String objects (use StringBuilder in Java to prevent memory spikes).
πŸ”— 4. Linked Lists (Click to expand)
  • Core Intuition: Pointer manipulation without random array access. Sentinel (dummy) head nodes eliminate edge cases for head deletions and insertions.
  • Key Sub-Patterns:
    • Fast & Slow Pointers: Midpoint detection, cycle discovery (Floyd's algorithm), finding the $k$-th node from the end.
    • In-Place Pointer Reversal: Maintain prev, curr, next pointers to reverse in $O(1)$ memory.
    • Merge & Partition: Dividing lists for Merge Sort or reordering around pivot values.
  • Common Pitfalls: Losing pointer references before reassignment; failing to set the last node's next to null.
πŸ“š 5. Stack & Queue (Click to expand)
  • Core Intuition: Stack = LIFO (Last-In First-Out) for backtracking/nesting; Queue = FIFO (First-In First-Out) for level-order flow.
  • Key Sub-Patterns:
    • Monotonic Stack: Maintain strictly increasing or decreasing elements; immediately provides the "Next Greater" or "Previous Smaller" element in $O(N)$ total time.
    • Parentheses & Nested Structures: Push open brackets, match with tops on close brackets.
    • Monotonic Deque: Sliding window maximum/minimum in $O(1)$ amortized per slide.
  • Common Pitfalls: Popping from an empty stack/queue without checking .isEmpty().
🎯 6. Binary Search (Click to expand)
  • Core Intuition: Divide the search space in half each iteration. It applies not just to sorted arrays, but to any monotonic feasibility function $f(x)$.
  • Key Sub-Patterns:
    • Classic / Rotated Search: Compare mid against ends to identify which half is cleanly sorted.
    • Binary Search on Answer Space: Guess an answer, verify with a greedy/linear check in $O(N)$, and adjust low and high bounds accordingly.
  • Common Pitfalls: Integer overflow during midpoint calculation (use low + (high - low) / 2), infinite loops caused by incorrect low = mid vs low = mid + 1.
🌳 7. Trees & Binary Search Trees (Click to expand)
  • Core Intuition: Trees are recursive structures. Solve for left and right subtrees, then combine answers at the root.
  • Key Sub-Patterns:
    • DFS (Pre-order, In-order, Post-order): Bottom-up info passing (height, diameter, balanced tree checks).
    • BFS (Level Order): Queue-based traversal for shortest depth or view from top/side.
    • BST Invariant: Left $< Node <$ Right. In-order traversal yields strictly sorted values.
  • Common Pitfalls: Forgetting the root == null base case; assuming local BST condition implies global BST validity.
⛰️ 8. Heap & Greedy (Click to expand)
  • Core Intuition: When you need dynamic extrema (minimum or maximum) or local choices that lead to globally optimal solutions.
  • Key Sub-Patterns:
    • Top-K Elements: Maintain a Min-Heap of size $K$; after iterating, the heap contains the $K$ largest items.
    • Two Heaps: Median finding using a Max-Heap for lower half and Min-Heap for upper half.
    • Interval Greedy: Sort intervals by end time to maximize non-overlapping schedules.
  • Common Pitfalls: Forgetting that Java PriorityQueue is a Min-Heap by default; greedy choice must be mathematically justifiable.
🧩 9. Backtracking (Click to expand)
  • Core Intuition: Depth-first search through decision trees with the ability to undo choices (Choose $\to$ Explore $\to$ Un-choose).
  • Key Sub-Patterns:
    • Subsets: Decision whether to include arr[i] or not.
    • Permutations: Place unused elements into position; swap or use a visited boolean array.
    • Combinations / Partitioning: Iterate from current index to prevent duplicate orderings.
  • Common Pitfalls: Not creating deep copies when saving states to result lists (new ArrayList<>(current)); missing early pruning checks.
πŸ•ΈοΈ 10. Graphs (Click to expand)
  • Core Intuition: Represent entities as nodes and relationships as edges. Always determine whether the graph is directed, weighted, or cyclic.
  • Key Sub-Patterns:
    • BFS (Queue): Guaranteed shortest path in unweighted graphs.
    • DFS (Recursion / Stack): Connected components, flood fill, cycle detection in directed graphs (3 colors: unvisited, visiting, visited).
    • Topological Sort: Dependency resolution using Kahn's in-degree algorithm or DFS post-order.
    • Disjoint Set Union (DSU): Dynamic connectivity and Kruskal's Minimum Spanning Tree.
  • Common Pitfalls: Forgetting to mark nodes visited before pushing to the BFS queue, resulting in duplicate processing and infinite loops.
🧠 11. Dynamic Programming (Click to expand)
  • Core Intuition: Break problems into subproblems with optimal substructure and overlapping calculations.
  • Key Sub-Patterns:
    • 1D DP: Fibonacci, Climbing Stairs, House Robber ($dp[i] = \max(dp[i-1], dp[i-2] + val)$).
    • 2D Grid DP: Unique paths, minimum path sum ($dp[r][c] = \min(dp[r-1][c], dp[r][c-1]) + grid[r][c]$).
    • Knapsack Variations: 0/1 Knapsack (iterate backwards in 1D array) vs Unbounded Knapsack (iterate forwards).
    • Longest Common Subsequence (LCS) / Edit Distance: String matching matrix.
  • Common Pitfalls: Incorrect base cases, traversing loop orders incorrectly for state dependency.

πŸ” Spaced Repetition & Retention Engine

Mastery comes from structured recall, not one-off solving. Use this proven retention cycle:

Day Interval Focus & Action Outcome
🟒 Day 1 Solve 4–5 new problems in a single pattern module Learn the pattern structure
πŸ”΅ Day 2 Revise yesterday's solutions without looking at code Solidify mental model
🟑 Day 4 Re-solve problems that required hints or failed on edge cases Fix conceptual gaps
🟣 Day 7 Solve 3 mixed problems across 2–3 previously studied patterns Train pattern recognition speed
🟠 Day 14 Timed mock session (20 mins per Medium problem) Build interview conditions
πŸ”΄ Day 30 Revisit only historically weak topics and Hard questions Long-term memory lock

⏱️ The 15-Minute Rule

When tackling an unfamiliar question:

  1. 0 – 5 Minutes: Read problem statement, check constraints, trace inputs/outputs by hand.
  2. 5 – 10 Minutes: Write out brute force logic and calculate its time/space complexity.
  3. 10 – 15 Minutes: Pinpoint what work repeats and choose a pattern to eliminate it.
  • If stuck after 15 minutes: Read only the Pattern or Approach section in the problem file, then close it and write the code entirely from your own mind.

🧾 Pre-Submission Checklist

Before running tests or clicking submit, verify:

[ ] 1. Have I verified empty input, single element, or null conditions?
[ ] 2. Are there duplicates in the input, and does my logic handle them?
[ ] 3. Could integer values overflow (e.g., mid calculation, large sums)?
[ ] 4. Are array bounds respected (index < 0 or index >= length)?
[ ] 5. Does the solution meet the target time complexity budget?
[ ] 6. Is auxiliary space optimal (can O(N) space be reduced to O(1))?

πŸ“‚ Repository Architecture

Each topic folder is cleanly organized by difficulty with dedicated problem notes:

500-DSA-Pattern-Problems/
β”œβ”€β”€ 01-Arrays/
β”‚   β”œβ”€β”€ Easy/               # Foundational questions
β”‚   β”œβ”€β”€ Medium/             # Core interview targets
β”‚   β”œβ”€β”€ Hard/               # Advanced bar-raisers
β”‚   └── README.md           # Module-specific problem index & links
β”œβ”€β”€ 02-Sliding-Window/
β”œβ”€β”€ 03-Strings-Hashing/
β”œβ”€β”€ 04-Linked-List/
β”œβ”€β”€ 05-Stack-Queue/
β”œβ”€β”€ 06-Binary-Search/
β”œβ”€β”€ 07-Trees-BST/
β”œβ”€β”€ 08-Heap-Greedy/
β”œβ”€β”€ 09-Backtracking/
β”œβ”€β”€ 10-Graphs/
β”œβ”€β”€ 11-Dynamic-Programming/
└── README.md               # Main Roadmap (You are here)

Each problem file (.md) is structured consistently for optimal learning:

  • πŸ”— Direct LeetCode link
  • 🧩 Underlying pattern category
  • πŸ’‘ High-level approach intuition
  • ⏱️ Exact Time and Space complexity
  • β˜• Complete, clean Java implementation
  • πŸ“ Concise line-by-line explanation


πŸ“„ License & Copyright

Copyright Β© 2026 Prem Kumar. All Rights Reserved.

This repository, including all source code, algorithms, solutions, documentation, roadmaps, diagrams, patterns, and curriculum structure, is proprietary and confidential. It is NOT open-source software and is not licensed under any permissive or copyleft license (such as MIT, Apache, GNU GPL, or AGPL).

  • πŸ”’ All Rights Reserved: No permission is granted to copy, reproduce, modify, translate, distribute, sublicense, publish, sell, or commercialize this source code or substantial portions of it without prior written authorization from the copyright holder.
  • πŸ‘οΈ Viewing & Personal Study Only: You may view this repository and its contents solely for personal, individual learning and study. Viewing the source code on GitHub or via any public interface does not grant any license or right to reuse, copy, modify, or redistribute the materials.
  • 🚫 No Unauthorized Reuse: Commercial and non-commercial reuse, mirroring, inclusion in educational courseware or bootcamps, and scraping/ingestion into AI training datasets are strictly prohibited without prior written permission.

For full legal terms and licensing inquiries, please inspect the LICENSE file or contact Prem Kumar.


🀝 Author & Connect

Built with πŸ’™ by Prem Kumar

Portfolio LinkedIn LeetCode GitHub

⭐ If you find this repository helpful, please consider giving it a star! It helps others discover these patterns. ⭐

Releases

Packages

Contributors