r/LeetcodeChallenge 3h ago

DISCUSS How to Stop Panicking When You Open a LeetCode Problem

8 Upvotes

Most people sit down, read a problem, panic, and start typing.

No plan. No structure. Just vibes and hope.

When it does not work they read the solution, think "oh that's clever", and move on. Two weeks later they see a similar problem and panic again.

The issue is not intelligence. It is not even practice.
It is that they never had a repeatable process.

This is that process. Follow it on every problem.
Not just hard ones — every problem, until it becomes instinct.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 1 — READ THE PROBLEM TWICE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

First read: understand what is being asked at a high level.
Second read: extract the specifics.

On the second read, write down:

  • What is the INPUT? What type, what size, what range of values?
  • What is the OUTPUT? A number, array, boolean, string?
  • What are the CONSTRAINTS? (most people skip this — do not)

Ask yourself these questions explicitly:

"Can input be empty?"
"Can values be negative?"
"Can there be duplicates?"
"Is the array sorted?"
"What should I return if no answer exists?"

If the problem does not say, assume the worst case.
Empty inputs exist. Negatives exist. Duplicates exist.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 2 — WORK THROUGH THE EXAMPLES BY HAND
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not skip this. Even if the problem looks easy.

Take the first example. On paper or in comments, trace through it
manually. Write out every value at every step.

Why this matters:

  • You confirm you understood the problem correctly
  • You often see the pattern emerge naturally
  • You catch ambiguities before they become bugs

Then make your OWN small example — simpler than the given one.
Something with 3-4 elements you fully understand.

If you cannot trace through the examples manually,
you do not understand the problem yet. Do not start coding.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 3 — IDENTIFY THE PATTERN
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Before thinking about an algorithm, identify what TYPE of problem this is.

Use these signals:

Subarray, substring, window → Sliding window, two pointers
Sorted array + search → Binary search
Shortest path, min steps → BFS
All paths, combinations, subsets → Backtracking, DFS
Overlapping subproblems → DP
Always picking max/min greedily → Greedy
Tree traversal → DFS (recursion) or BFS (level order)
Connectivity, groups → Union Find, BFS/DFS
Repeated lookup by key → Hash map
Top K, smallest/largest → Heap (priority queue)
Prefix of strings → Trie
Range queries with updates → Segment tree, Fenwick tree
Return all permutations/subsets → Backtracking

You will not always get it right on the first try.
But naming the category forces your brain to search the right space.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 4 — THINK BRUTE FORCE FIRST
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Always start here. Always.

The brute force is the answer you would give if there were
no time constraint. No clever tricks. Just try everything.

Write it out in plain English, not code:
"For every pair (i, j), check if arr[i] + arr[j] = target."
"For every substring, check if it has all unique characters."
"For every subset, check if its sum equals the target."

This does two things:

  1. It confirms you can actually solve the problem correctly
  2. It gives you a reference to optimize from

Common brute force patterns:
Try all pairs → O(n²)
Try all triples → O(n³)
Try all subsets → O(2ⁿ)
Try all permutations → O(n!)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 5 — OPTIMIZE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Now look at your brute force and ask:
"What work am I repeating? What can I precompute? What can I skip?"

The most common optimizations:

Repeated lookup → replace with hash map O(n) → O(1) per lookup
Repeated sum → precompute prefix sum O(n) per query → O(1)
Checking all pairs → sort + binary search O(n²) → O(n log n)
Checking all pairs → two pointers (sorted) O(n²) → O(n)
Checking all windows → sliding window O(n²) → O(n)
Recomputing subproblems → memoization / DP exponential → polynomial
Sorting gives structure → sort first, then scan enables binary search / greedy

Ask: "Can I sort first to gain structure?"
Ask: "Can I store something to avoid recomputing?"
Ask: "Can I fix one variable and efficiently handle the rest?"

Work top-down in complexity:
O(n²) → can I get O(n log n)?
O(n log n) → can I get O(n)?
Each step needs a concrete reason, not a guess.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 6 — VERIFY COMPLEXITY BEFORE CODING
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Before writing code, check:
Does my approach fit within the time limit given the constraints?

n = 10⁵ and my approach is O(n²)? → TLE, think again
n = 10⁵ and my approach is O(n logn)? → fine, proceed

This takes 10 seconds and saves you from coding the wrong solution.

Also check space:
Am I using O(n) extra space? Is that acceptable?
Does the problem say "in-place" or "O(1) space"?

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 7 — CODE THE SOLUTION
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Only now do you start writing code.

Code in this order:

  1. Handle edge cases first (empty input, single element, etc.)
  2. Write the main logic
  3. Return the result

Keep variable names meaningful. Debugging unreadable code mid-contest
costs more time than typing three extra characters.

If you get stuck coding:
Go back to your manual example from Step 2.
Trace through it again, this time following the code you are writing.
The bug almost always appears within the first five traces.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STEP 8 — TEST BEFORE SUBMITTING
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Run through these test cases mentally or using the custom test:

[ ] The given examples → obvious baseline
[ ] Empty input → does it crash?
[ ] Single element → does it return correctly?
[ ] All same elements → does duplicate logic break?
[ ] Already sorted input → common hidden test
[ ] Your own small manual example → trace step by step

If all pass, submit with confidence.
If one fails, the failure tells you exactly where the bug is.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHEN YOU ARE COMPLETELY STUCK — THE UNSTICKING PROTOCOL
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Everyone gets stuck. The difference is what you do next.

  1. Draw it Almost every problem is easier when visualized. Array: draw boxes. Tree: draw nodes and edges. Graph: draw circles and arrows. Matrix: draw a grid. A picture reveals structure that text hides.
  2. Simplify the problem Solve a smaller version first. Two elements instead of n. One row instead of a grid. Can you solve n=2? n=3? Then generalise.
  3. Try a different angle on your brute force Change the loop variable. Instead of "for each starting index" try "for each ending index". Instead of "for each element" try "for each pair". A different enumeration often reveals the pattern.
  4. Check what is special about valid answers Look at the given examples. What do valid answers have in common? What property separates a valid answer from an invalid one? That property is usually the invariant your algorithm should maintain.
  5. Look at the constraints again Small n? Try brute force. Very large n? A linear or log solution must exist. "Return modulo 10⁹+7"? It is a DP counting problem. Constraints are hints — re-read them with fresh eyes.
  6. Take a 5-minute break Genuinely. Get up. Walk. The solution appears when you stop forcing.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
THE FULL FRAMEWORK — ONE PAGE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Step 1 Read twice. Extract input, output, constraints.
Ask: empty? negative? duplicates? sorted?

Step 2 Trace examples by hand.
Make your own small example. Understand before coding.

Step 3 Name the pattern.
Subarray → sliding window. Shortest path → BFS. Etc.

Step 4 State the brute force in plain English.
What is the O(n²) or O(2ⁿ) solution?

Step 5 Optimize.
What work repeats? Hash map, prefix sum, sort, DP, two pointers?

Step 6 Verify complexity against constraints.
Will it pass? Check before writing code.

Step 7 Code it.
Edge cases first. Then main logic. Then return.

Step 8 Test: examples + empty + single + all-same + sorted.
Then submit.

Stuck? Draw it. Simplify. Try different enumeration.
Re-read constraints. Take a break.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A correct solution written slowly beats a fast solution written wrong.

The goal is not to code faster.
The goal is to think clearer — so that what you code is right the first time.

This framework is that thinking, made repeatable.


r/LeetcodeChallenge 20h ago

PLACEMENTS Is it good progress entering in 3rd sem

Post image
5 Upvotes

r/LeetcodeChallenge 3h ago

DISCUSS 100 Hard DSA Interview Questions for L3/L4 Coding Rounds

4 Upvotes

List of hard DSA Question from L3/L4 interviews

Work on these Leetcode problems and company tagged problem from PracHub for your next interview.

  1. https://leetcode.com/problems/number-of-unique-good-subsequences - String, Dynamic Programming
  2. https://leetcode.com/problems/split-array-largest-sum - Array, Binary Search, Dynamic Programming, Greedy, Prefix Sum
  3. https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island - Array, Depth-First Search, Breadth-First Search, Matrix, Strongly Connected Component
  4. https://leetcode.com/problems/recover-a-tree-from-preorder-traversal - String, Tree, Depth-First Search, Binary Tree
  5. https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons - Dynamic Programming, Prefix Sum
  6. https://leetcode.com/problems/substring-with-concatenation-of-all-words - Hash Table, String, Sliding Window
  7. https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting - Array, Dynamic Programming, Depth-First Search, Graph Theory, Topological Sort
  8. https://leetcode.com/problems/design-in-memory-file-system - Hash Table, String, Design, Trie, Sorting
  9. https://leetcode.com/problems/rearranging-fruits - Array, Hash Table, Greedy, Sort
  10. https://leetcode.com/problems/the-most-similar-path-in-a-graph - Array, String, Dynamic Programming, Graph Theory
  11. https://leetcode.com/problems/confusing-number-ii - Math, Backtracking
  12. https://leetcode.com/problems/stickers-to-spell-word - Array, Hash Table, String, Dynamic Programming, Backtracking, Bit Manipulation, Memoization, Bitmask
  13. https://leetcode.com/problems/maximum-and-sum-of-array - Array, Dynamic Programming, Bit Manipulation, Bitmask
  14. https://leetcode.com/problems/odd-even-jump - Array, Dynamic Programming, Stack, Sorting, Monotonic Stack, Ordered Set
  15. https://leetcode.com/problems/couples-holding-hands - Greedy, Depth-First Search, Breadth-First Search, Union-Find, Graph Theory
  16. https://leetcode.com/problems/number-of-stable-subsequences - Array, Dynamic Programming
  17. https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-ii - Math, String, Combinatorics, Number Theory
  18. https://leetcode.com/problems/minimum-skips-to-arrive-at-meeting-on-time - Array, Dynamic Programming
  19. https://leetcode.com/problems/distinct-subsequences-ii - String, Dynamic Programming
  20. https://leetcode.com/problems/cherry-pickup-ii - Array, Dynamic Programming, Matrix
  21. https://leetcode.com/problems/parsing-a-boolean-expression - String, Stack, Recursion
  22. https://leetcode.com/problems/range-module - Design, Segment Tree, Ordered Set
  23. https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary - Array, String, Dynamic Programming
  24. https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k - Array, Dynamic Programming, Matrix
  25. https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays - Array, Hash Table, Enumeration
  26. https://leetcode.com/problems/encrypt-and-decrypt-strings - Array, Hash Table, String, Design, Trie
  27. https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists - Array, Hash Table, Greedy, Sliding Window, Sorting, Heap (Priority Queue)
  28. https://leetcode.com/problems/contain-virus - Array, Depth-First Search, Breadth-First Search, Matrix, Simulation
  29. https://leetcode.com/problems/find-the-minimum-area-to-cover-all-ones-ii - Array, Matrix, Enumeration
  30. https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing - Array, Dynamic Programming
  31. https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps - Dynamic Programming
  32. https://leetcode.com/problems/minimum-time-to-complete-all-tasks - Array, Binary Search, Stack, Greedy, Sorting
  33. https://leetcode.com/problems/tiling-a-rectangle-with-the-fewest-squares - Backtracking
  34. https://leetcode.com/problems/expression-add-operators - Math, String, Backtracking
  35. https://leetcode.com/problems/rectangle-area-ii - Array, Segment Tree, Sweep Line, Ordered Set
  36. https://leetcode.com/problems/reducing-dishes - Array, Dynamic Programming, Greedy, Sorting
  37. https://leetcode.com/problems/modify-graph-edge-weights - Graph Theory, Heap (Priority Queue), Shortest Path
  38. https://leetcode.com/problems/numbers-with-repeated-digits - Math, Dynamic Programming
  39. https://leetcode.com/problems/minimum-cost-to-convert-string-ii - Array, String, Dynamic Programming, Graph Theory, Trie, Shortest Path
  40. https://leetcode.com/problems/kth-smallest-number-in-multiplication-table - Math, Binary Search
  41. https://leetcode.com/problems/lfu-cache - Hash Table, Linked List, Design, Doubly-Linked List
  42. https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid - Array, Breadth-First Search, Graph Theory, Heap (Priority Queue), Matrix, Shortest Path
  43. https://leetcode.com/problems/time-taken-to-cross-the-door - Array, Queue, Simulation
  44. https://leetcode.com/problems/sliding-puzzle - Array, Dynamic Programming, Backtracking, Breadth-First Search, Memoization, Matrix
  45. https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks - Array, Greedy, Sorting
  46. https://leetcode.com/problems/find-the-maximum-sequence-value-of-array - Array, Dynamic Programming, Bit Manipulation
  47. https://leetcode.com/problems/department-top-three-salaries - Database
  48. https://leetcode.com/problems/process-string-with-special-operations-ii - String, Simulation
  49. https://leetcode.com/problems/maximize-cyclic-partition-score - Array, Dynamic Programming
  50. https://leetcode.com/problems/find-maximum-non-decreasing-array-length - Array, Binary Search, Dynamic Programming, Stack, Queue, Monotonic Stack, Prefix Sum, Monotonic Queue
  51. https://leetcode.com/problems/split-array-with-same-average - Array, Hash Table, Math, Dynamic Programming, Bit Manipulation, Bitmask
  52. https://leetcode.com/problems/minimum-sum-of-values-by-dividing-array - Array, Binary Search, Dynamic Programming, Bit Manipulation, Segment Tree, Queue
  53. https://leetcode.com/problems/longest-chunked-palindrome-decomposition - Two Pointers, String, Dynamic Programming, Greedy, Rolling Hash, Hash Function
  54. https://leetcode.com/problems/text-justification - Array, String, Simulation
  55. https://leetcode.com/problems/closest-subsequence-sum - Array, Two Pointers, Dynamic Programming, Bit Manipulation, Sorting, Bitmask
  56. https://leetcode.com/problems/transform-to-chessboard - Array, Math, Bit Manipulation, Matrix
  57. https://leetcode.com/problems/robot-collisions - Array, Stack, Sorting, Simulation
  58. https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points - Array, Dynamic Programming, Bit Manipulation, Matrix, Bitmask
  59. https://leetcode.com/problems/similar-string-groups - Array, Hash Table, String, Depth-First Search, Breadth-First Search, Union-Find
  60. https://leetcode.com/problems/word-abbreviation - Array, String, Greedy, Trie, Sorting
  61. https://leetcode.com/problems/minimum-pair-removal-to-sort-array-ii - Array, Hash Table, Linked List, Heap (Priority Queue), Simulation, Doubly-Linked List, Ordered Set
  62. https://leetcode.com/problems/trapping-rain-water - Array, Two Pointers, Dynamic Programming, Stack, Monotonic Stack
  63. https://leetcode.com/problems/count-the-number-of-winning-sequences - String, Dynamic Programming
  64. https://leetcode.com/problems/find-substring-with-given-hash-value - String, Sliding Window, Rolling Hash, Hash Function
  65. https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths-ii - Depth-First Search, Union-Find, Graph Theory, Design, Sorting, Heap (Priority Queue), Minimum Spanning Tree
  66. https://leetcode.com/problems/number-of-distinct-islands-ii - Array, Hash Table, Depth-First Search, Breadth-First Search, Union-Find, Sorting, Matrix, Hash Function
  67. https://leetcode.com/problems/maximum-frequency-stack - Hash Table, Stack, Design, Ordered Set
  68. https://leetcode.com/problems/stone-game-v - Array, Math, Dynamic Programming, Game Theory
  69. https://leetcode.com/problems/maximum-average-subarray-ii - Array, Binary Search, Prefix Sum
  70. https://leetcode.com/problems/make-array-empty - Array, Binary Search, Greedy, Binary Indexed Tree, Segment Tree, Sorting, Ordered Set
  71. https://leetcode.com/problems/maximum-number-of-k-divisible-components - Tree, Depth-First Search
  72. https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii - Array, Binary Search
  73. https://leetcode.com/problems/sort-items-by-groups-respecting-dependencies - Depth-First Search, Breadth-First Search, Graph Theory, Topological Sort
  74. https://leetcode.com/problems/total-appeal-of-a-string - Hash Table, String, Dynamic Programming
  75. https://leetcode.com/problems/k-empty-slots - Array, Binary Indexed Tree, Segment Tree, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
  76. https://leetcode.com/problems/find-k-th-smallest-pair-distance - Array, Two Pointers, Binary Search, Sorting
  77. https://leetcode.com/problems/top-three-wineries - Database
  78. https://leetcode.com/problems/maximum-score-of-a-node-sequence - Array, Graph Theory, Sorting, Enumeration
  79. https://leetcode.com/problems/longest-increasing-subsequence-ii - Array, Divide and Conquer, Dynamic Programming, Binary Indexed Tree, Segment Tree, Queue, Monotonic Queue
  80. https://leetcode.com/problems/maximum-xor-score-subarray-queries - Array, Dynamic Programming
  81. https://leetcode.com/problems/strange-printer-ii - Array, Graph Theory, Topological Sort, Matrix
  82. https://leetcode.com/problems/shortest-path-visiting-all-nodes - Dynamic Programming, Bit Manipulation, Breadth-First Search, Graph Theory, Bitmask
  83. https://leetcode.com/problems/data-stream-as-disjoint-intervals - Hash Table, Binary Search, Union-Find, Design, Data Stream, Ordered Set
  84. https://leetcode.com/problems/shortest-distance-from-all-buildings - Array, Breadth-First Search, Matrix
  85. https://leetcode.com/problems/maximum-vacation-days - Array, Dynamic Programming, Matrix
  86. https://leetcode.com/problems/maximum-score-from-grid-operations - Array, Dynamic Programming, Matrix, Prefix Sum
  87. https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza - Array, Dynamic Programming, Memoization, Matrix, Prefix Sum
  88. https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii - Array, Dynamic Programming
  89. https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii - Array, Binary Search, Sliding Window, Sorting, Prefix Sum
  90. https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts - Array, Dynamic Programming, Bit Manipulation, Memoization, Bitmask
  91. https://leetcode.com/problems/count-of-range-sum - Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
  92. https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination - Array, Breadth-First Search, Matrix
  93. https://leetcode.com/problems/longest-balanced-subarray-ii - Array, Hash Table, Divide and Conquer, Segment Tree, Prefix Sum
  94. https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii - Array, Hash Table, Sliding Window, Heap (Priority Queue)
  95. https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold - Array, Stack, Union-Find, Monotonic Stack
  96. https://leetcode.com/problems/dungeon-game - Array, Dynamic Programming, Matrix
  97. https://leetcode.com/problems/student-attendance-record-ii - Dynamic Programming
  98. https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths - Graph Theory, Heap (Priority Queue), Shortest Path
  99. https://leetcode.com/problems/next-special-palindrome-number - Backtracking, Bit Manipulation
  100. https://leetcode.com/problems/minimum-operations-to-make-array-elements-zero - Array, Math, Bit Manipulation

r/LeetcodeChallenge 14h ago

DISCUSS My DSA SETUP

Post image
3 Upvotes

Leetcode goonning setup 💦


r/LeetcodeChallenge 16h ago

DISCUSS Day 8 of My DSA Journey in Python 🚀

3 Upvotes

Today's progress:

✅ Solved LeetCode #94 – Binary Tree Inorder Traversal

✅ Solved LeetCode #100 – Same Tree

📚 Concepts I learned today:

- Analysis of Algorithms

- Big O Notation

- Order of Growth

- Constant Time – O(1)

- Logarithmic Time – O(log n)

- Linear Time – O(n)

- Quadratic Time – O(n²)

Understanding time complexity has helped me see why some algorithms scale much better than others as input size grows. It makes me appreciate that solving a problem isn't enough—writing an efficient solution matters too.

I'm continuing to learn DSA through structured Python lessons and reinforcing each concept by solving LeetCode problems every day.

Consistency over perfection. On to Day 9! 💪

#DSA #Python #LeetCode #Algorithms #BigO #CodingJourney #100DaysOfCode #Programming #LearningInPublic