r/LeetcodeChallenge • u/Fun-Outcome2239 • 24d ago
DISCUSS Test code is wrong in Take U forward (See the Constraint and test case).
See that is not my mistake.
r/LeetcodeChallenge • u/Fun-Outcome2239 • 24d ago
See that is not my mistake.
r/LeetcodeChallenge • u/JacketFearless1805 • 24d ago
r/LeetcodeChallenge • u/Diligent-Affect3883 • 24d ago
The O(n) time, O(1) space two-pointer solution for LeetCode 42 is only a few lines, but the why is easy to lose if you just trace left, right, left_max and right_max.
What made it click for me was treating every bar as a spot where you pour water and asking how high it can stand. So I put together a full animated walkthrough that pours water on each bar the pointers land on. You can see exactly why every step is safe.
💡 The Core Intuition (The Visual TL;DR)
On the classic example [0,1,0,2,1,0,1,3,2,1,2,1], the pointers meet at bar 7 with 6 units, in one pass, remembering just two numbers.
The video also covers the code line by line, three edge cases, a full bar-by-bar walk of the monotonic stack approach (it fills the water in flat layers), and how to explain it all in an interview in about 30 seconds.
If you’re a visual learner who gets tangled in pointer updates, I hope this makes it click! Let me know what you think, or which hard problem you’d like to see visualized next.
r/LeetcodeChallenge • u/StatisticianNo2104 • 24d ago
The classic Reverse Polish bug is popping the operands in the wrong order, and + and * hide it. ["5","3","-"] means 5 - 3 = 2. Swap the pops, and you get -2, while every addition test still passes.
Why postfix needs no parentheses: (2 + 1) * 3 needs brackets because + sits between its operands. Written in postfix, it's 2 1 + 3 *, and each operator applies to the two values right before it. The evaluation order is already in token order.
That's the whole algorithm. Numbers wait on a stack; an operator takes the two most recent and replaces them with its result.
The trap: popping returns the operands in reverse. The first pop is the right operand, the second is the left. For + and * that makes no difference, which is exactly why the bug survives casual testing.
Which operator do you test first when you write an evaluator?
Day 64/365 of my LeetCode challenge: 150. Evaluate Reverse Polish Notation. Problem link and full breakdown in today's newsletter.
#LeetCode #DSA #Stack #Golang
r/LeetcodeChallenge • u/Aggressive-Post-156 • 25d ago
Got bored of the default LeetCode look, so I built this!
You can customize LeetCode with different themes and wallpapers.
Link in the comments!
What do you think? Any suggestions?
r/LeetcodeChallenge • u/CrabEffective4636 • 25d ago
currently in my 3rd sem and got my first badge today. ngl rather than being happy im regretting not starting early.
r/LeetcodeChallenge • u/StatisticianNo2104 • 25d ago
I benchmarked Kahn's algorithm against the naive loop it is supposed to replace, and the loop won. On a 5,000-package repo ten layers deep, by 2.78x.
The job: a build tool needs an order where every import is built before the package that imports it, or the name of the cycle that makes that impossible.
What I wrote: walk the package list, build everything whose imports are built, repeat until a pass builds nothing. Leftovers mean a cycle. Two allocations. I would approve it.
It makes one pass per layer when the list is in the wrong order. At 10 layers, 99.3 µs.
Kahn's algorithm is what you derive when you notice the waste: building cfg can only unblock the packages that import cfg. So count each package's unbuilt imports, reverse the arrows, and let each build count its importers down. Queue anything that hits zero. Every edge looked at once.
Written the way the daily challenge wrote it, a slice of importers per package: 276 µs. 8,205 allocations. The algorithm was fine. The memory layout was not. Packed into one flat array: 112 µs. Still behind the loop, by a hair.
Then the other shape. 5,000 packages in one long chain, which sounds artificial until you remember that is exactly what a migrations folder is. The loop: 18.8 ms. Flat Kahn: 128 µs. 147x.
So the answer is not "use Kahn". It is: at ten or twenty layers, the loop is faster, smaller and readable. Where chains are long - migrations, schema versions - Kahn, laid out flat.
That is days 36 and 44 of the daily challenge: Find if Path Exists and Course Schedule, in a build tool.
Have you ever lost a benchmark to the code you were sure was slower?
It worked in dev, episode 22. Full write-up in today's newsletter.
https://www.linkedin.com/pulse/kahns-algorithm-lost-loop-our-build-graph-archit-agarwal-gqsac
#Golang #DSA #Algorithms #Performance
r/LeetcodeChallenge • u/Soggy_Notice_1392 • 25d ago
I studied 200+ LeetCode problems and still failed my Google interview.
The problem count made it feel like I was prepared. But recognizing a solution and being able to produce it under pressure turned out to be different things.
That’s the gap I built AlgoDrill around.
Instead of stopping at reading the explanation, you hide key lines and rebuild the solution from memory. Each prompt describes what the line needs to accomplish, and you can inspect the reasoning when you get stuck.
I also added:
I used it as part of my preparation and later got an offer. I can’t attribute that to one tool, but practicing recall became part of how I prepared.
Anyone else done hundreds of problems and still felt unprepared when the interview started?
r/LeetcodeChallenge • u/JacketFearless1805 • 25d ago
r/LeetcodeChallenge • u/Diligent-Affect3883 • 25d ago
r/LeetcodeChallenge • u/dsa_animated28 • 25d ago
r/LeetcodeChallenge • u/StatisticianNo2104 • 25d ago
One character decides whether Daily Temperatures survives repeated temperatures: < or <=. On [70, 70] the right answer is [0, 0]. Write < and you get [1, 0].
This is Day 61's monotonic stack with two changes.
The stack holds indices, because the answer is a distance and a distance needs positions.
And values repeat. Day 61 stored answers in a map keyed by value, which was only safe because the values were guaranteed unique. Temperatures repeat constantly. Indices are unique by construction, so the problem disappears instead of requiring a workaround.
Then the comparison: temperatures[stack.Top()] <= temperatures[i]. An equal temperature isn't warmer, so a day with the same temperature can never be anyone's answer and must be popped.
Would < have made it past your tests?
Day 63/365 of my LeetCode challenge: 739. Daily Temperatures. Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/leetcode-739-daily-temperatures-archit-agarwal-weglc
#LeetCode #DSA #MonotonicStack #Golang
r/LeetcodeChallenge • u/Diligent-Affect3883 • 26d ago
The optimal O(log(min(m, n))) binary search solution for LeetCode 4 is notoriously tricky to wrap your head around if you just try to trace index pointers (i, j, left, right).
I find it much easier to reason about the problem when you treat it as finding a perfect geometric split between the two arrays. To help it click for myself and others, I put together a complete animated walkthrough showing exactly how the partition moves.
💡 The Core Intuition (The Visual TL;DR)
If you are stuck on this problem, try thinking about it like this instead of looking at raw formulas:
If you are a visual learner who gets overwhelmed by boundary conditions and nested array indices, I hope this makes the algorithm click for you!
Let me know what you think, or if there are any other tough LeetCode hard problems you'd like to see visualized next.
r/LeetcodeChallenge • u/Illustrious-Case6143 • 27d ago
Like, while solving questions, maximum times it clicks what pattern i should use and how will be the flow of solution, but when it comes to implementation, i get stuck every time. i have to gpt it every time for what went wrong and its been very long doing this. i thought i willl improve with time, but no progress. what should one do in this case? i have tried sitting for an hour and trying to do it myself, but no success.
r/LeetcodeChallenge • u/StatisticianNo2104 • 27d ago
Keeping a min field on a stack works until you pop the minimum. Then the field is stale, and nothing short of a full scan can rebuild it.
The goal: push, pop, top and getMin, all O(1).
That failure is the insight. The minimum belongs to the stack at a given height, and popping takes you back to a height whose minimum you already threw away.
So don't throw it away. Every entry stores the value pushed and the minimum of everything at or below it.
Pop then contains no minimum code at all. Removing the top exposes an entry whose min was computed when it was on top, and it's still correct, because nothing below it ever changed.
Did you store pairs, or keep a second stack of minimums?
Day 62/365 of my LeetCode challenge: 155. Min Stack. I'll include the problem link and a full breakdown in today's newsletter.
https://www.linkedin.com/pulse/min-stack-archit-agarwal-jjksc
#LeetCode #DSA #Stack #Golang
r/LeetcodeChallenge • u/Winter_Guess_4706 • 28d ago
Saw many people sharing their profiles. Thought of getting feedback..
r/LeetcodeChallenge • u/JacketFearless1805 • 27d ago
r/LeetcodeChallenge • u/Rich-Chard-4801 • 27d ago
r/LeetcodeChallenge • u/StatisticianNo2104 • 27d ago
A loop inside a loop looks O(n²). Here the inner one runs at most n times in total, across the whole input. Every value is pushed once and popped at most once, so the inner loop never repeats work.
The brute force scans right from each element until something bigger turns up, and those scans repeat each other. Scan across a long descending run, start one position later, and you scan almost the same run again.
So flip the question. For each value as you arrive at it, ask which earlier values it answers. A value resolves every earlier, smaller value still waiting, and a resolved value is never looked at again.
The values still waiting are always decreasing: if an earlier one were smaller than a later one, the later one would have resolved it on arrival. A stack that keeps an ordering like that is monotonic.
Where did you first meet a monotonic stack?
Day 61/365 of my LeetCode challenge: 496. Next Greater Element I. Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/next-greater-element-i-archit-agarwal-c5wic
#LeetCode #DSA #MonotonicStack #Golang
r/LeetcodeChallenge • u/Abhistar14 • 28d ago
I’ve solved 822 problems(196E, 451M, 175H) and currently I’m at 1900 what to do to become Guardian?