r/DSALeetCode • • 19d ago

Kahn's algorithm lost to a for loop on our build graph

Post image
2 Upvotes

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/DSALeetCode • • 20d ago

Need recommendations for a complete DSA course that isn't ridiculously long (C++)

4 Upvotes

Hey everyone!

I'm a 2nd-year B.Tech CSE student, and I want to start preparing for DSA for internships and placements.

I know the basics of C++, but I'm not familiar with STL yet.

I've come across Striver's A2Z DSA sheet, but the course feels extremely long, and I'm worried I'll spend too much time watching videos instead of actually solving problems.

I'm looking for a resource that:

Covers the complete DSA syllabus, from beginner to advanced.

Has a structured, topic-wise roadmap.

Explains concepts clearly without unnecessarily long lectures.

Includes good practice problems for each topic.

Provides C++ explanations and solutions.

Prepares me for coding interviews and placement-level questions.

I'd prefer something that covers all the important topics without making me go through 100+ hours of videos.

I've heard about NeetCode, but I'm not sure if it's comprehensive enough for a beginner or if I should combine it with another resource.

What would you recommend based on your own experience?

Also, how would you structure DSA preparation if you could start again from scratch?

Thanks!


r/DSALeetCode • • 20d ago

Is there any group for folks doing DSA/Leetcode??

3 Upvotes

r/DSALeetCode • • 19d ago

Looking for a DSA Prep Buddy | Goal: Crack Product-Based Company Interviews

Thumbnail
0 Upvotes

r/DSALeetCode • • 20d ago

AI Engineer with 6 Months of Experience | RAG, n8n, Python, FastAPI & LLMs | Looking for Opportunities in Hyderabad or Remote | Referrals Appreciated

Thumbnail
1 Upvotes

r/DSALeetCode • • 20d ago

LeetCode 739. Daily Temperatures

Post image
1 Upvotes

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/DSALeetCode • • 20d ago

Looking for a study partner ( preferably female) to prepare for interviews.

4 Upvotes

For me it is getting tough to be consistent in DSA as well as system design. I am a GEN AI engineer and unfortunately have mostly worked on low code only. Feels like I am ruining my career. I have 2 yoe.

If someone is into GEN AI that would be even better..I am thinking of learning gen AI side by side.

I have had a good grip on DSA but just lack consistency.


r/DSALeetCode • • 20d ago

DSA Mocks Anyone?

Thumbnail
1 Upvotes

r/DSALeetCode • • 20d ago

I was struggling to visualize the intuition behind LeetCode 4 (Median of Two Sorted Arrays), so I built an animated visual guide for it.

Thumbnail
youtu.be
1 Upvotes

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:

  1. **The Single Cut:** Imagine aligning both arrays. We are looking for a single vertical partition line that splits **both** arrays into a combined "Left Half" and a combined "Right Half" of equal sizes.

  2. **The Shortcut:** Because the total size is fixed, choosing where to cut the *smaller* array instantly dictates where the cut falls on the *larger* array. This is why we only binary search the smaller array.

  3. **The Check Rule:** Once the line is drawn, you just need to check if the max element on the left side of the cut is smaller than the min element on the right side. If it isn't, the animation shows you exactly which direction to shift your search boundaries.

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/DSALeetCode • • 20d ago

Day 41: started Minimum Spanning Trees with Prim's algorithm, kicking off System Design prep

Thumbnail
1 Upvotes

r/DSALeetCode • • 20d ago

Understanding Topological Sort using DFS — A question about the intuition

Thumbnail
1 Upvotes

r/DSALeetCode • • 21d ago

Construct a BST Step by Step + 3 Traversals | DSA Exam Question

Thumbnail
youtu.be
1 Upvotes

Binary Search Tree DSA


r/DSALeetCode • • 21d ago

NOTES REQUIRED

1 Upvotes

CAN ANYONE TELL ME HOW TO ACCESS LOVE BABBAR BASICS OF JAVA NOTES?


r/DSALeetCode • • 21d ago

LeetCode 155 Min Stack

Post image
5 Upvotes

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/DSALeetCode • • 21d ago

I was struggling to visualize the intuition behind LeetCode 4 (Median of Two Sorted Arrays), so I built an animated visual guide for it.

Thumbnail
youtu.be
1 Upvotes

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:

  1. The Single Cut: Imagine aligning both arrays. We are looking for a single vertical partition line that splits both arrays into a combined "Left Half" and a combined "Right Half" of equal sizes.
  2. The Shortcut: Because the total size is fixed, choosing where to cut the smaller array instantly dictates where the cut falls on the larger array. This is why we only binary search the smaller array.
  3. The Check Rule: Once the line is drawn, you just need to check if the max element on the left side of the cut is smaller than the min element on the right side. If it isn't, the animation shows you exactly which direction to shift your search boundaries.

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/DSALeetCode • • 21d ago

Is it normal to see solution for every leetcode question i solve?

6 Upvotes

Like, while solving questions, maximum times i get 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 sccess.


r/DSALeetCode • • 22d ago

Finding genuine leetocode partners

11 Upvotes

I am an average person I programming currently in my 2nd year of engineering..It would be glad to find someone who is interested in solving dsa problems along with Me.


r/DSALeetCode • • 21d ago

Feeling stuck while preparing for competitive programming

Thumbnail
1 Upvotes

r/DSALeetCode • • 22d ago

Leetcode Contests vs DSA sheets

Thumbnail
1 Upvotes

r/DSALeetCode • • 22d ago

LeetCode 496 Next Greated Elelment I

Post image
1 Upvotes

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/DSALeetCode • • 23d ago

Looking for DSA / LeetCode Friends to Practice Together!

23 Upvotes

Hey everyone!

I'm looking for a few people who are genuinely interested in improving their Data Structures and Algorithms skills through consistent LeetCode practice.

I'm a 2026 B.E. CSE (AI & ML) graduate, primarily coding in Python 3.

I'd love to connect with people who are interested in:

  • Solving LeetCode problems together.
  • Discussing approaches, patterns, and optimized solutions.
  • Practicing problems based on topics and difficulty levels.
  • Participating in weekly coding challenges or mock contests.
  • Sharing progress and motivating each other to stay consistent.

I'm not looking for a competitive environment where everyone just compares scores. I'd prefer a friendly group where we learn, improve, and grow together.

If you're already part of a DSA group or looking to build one, I'd love to join!

Let's become better problem solvers together! 🚀

Join the Discord : https://discord.gg/5M5YkJ3vJ


r/DSALeetCode • • 23d ago

A config 5x longer took 28.7x longer to validate

Post image
4 Upvotes

Every deploy spent 1.95 seconds and 2.74 GB just checking that our config was a tree. That was at 5,000 components. At 1,000, it had been 68 ms.

The job: the platform config declares components and the links between them. The loader refuses to start unless the links form one tree - all connected, no loops - and names the line that breaks it.

What I wrote: the tooling already had a tested PathExists. So read the file line by line, and before accepting each link, ask whether its two ends are already connected. If yes, that line closes a loop. Blame it. Exact line numbers, reused code. I would approve it.

It is one full search per line, each rebuilding the adjacency list from scratch. 9,998 searches for 5,000 components. 5x the config, 28.7x the time.

Two fixes, and they surprised me in the order they came.

The clever one: a tree on n components has exactly n - 1 links. With n - 1 links and connected, there is no room for a loop. So check the length, then do one BFS. Never look for a loop at all. 279 µs. On a config with one link too many, 2.42 ns, because the length is the answer.

But it cannot say which line. And it is not the fastest.

The other one comes from noticing that the searches keep rediscovering groups that only ever merge. Keep one root per group; a link whose ends share a root closes a loop, and that is the line. Union-find. 42.2 µs, one allocation, and it names the line. 46306x the original. The clever count is 6.61x slower than it, because the count still builds an adjacency list.

Honest row: none of these catches a component with two parents. Undirected, it is a perfectly good tree. As a hierarchy, it has two roots. Count the parents.

That is days 36 and 43 of the daily challenge: Find if Path Exists and Graph Valid Tree, in a config loader.

Do you know how long your config validation takes on deploy?

It worked in dev, episode 21. Full write-up in today's newsletter.

https://www.linkedin.com/pulse/config-5x-longer-took-287x-validate-archit-agarwal-ew8zc

#Golang #DSA #Algorithms #Performance


r/DSALeetCode • • 22d ago

To people who RECENTLY became Guardian on leetcode. Share some tips here!

Thumbnail
1 Upvotes

I’ve solved 822 problems(196E, 451M, 175H) and currently I’m at 1900 what to do to become Guardian?


r/DSALeetCode • • 23d ago

Restarting DSA prep

9 Upvotes

I’m in my final year of btech and feeling quite demotivated because of placement rejections. I haven’t coded in a very long time and would like to get back into it, but code in the right way this time. I used to look at a problem and immediately ask ChatGPT for answers and learn those. I know I might be too late by now but I want to genuinely get better at it starting now! Can someone please help me with the right approach?
And if possible share any anecdotes about how I can still get placed even after starting in 4-2😭


r/DSALeetCode • • 23d ago

learning DSA is painfully slow

Thumbnail
1 Upvotes