Ask HN: How are you supposed to use LeetCode?
It probably took me 2 full hours, and I only got it with a brute force approach and a butt-load of optimization to make it efficient later.
So, anyway, then I go to the discussion tab, and that's where everyone is chatting about this really cool, elegant, O(n) solution. But it's the kind of thing you would see in a CS textbook, not just come up with on your own. And I didn't see any proofs, so I still don't know that it _really_ works. But I do see lots of examples of exactly how to implement it.
My question is; what's the point then? Are you supposed to not look at the discussion and beat your head against the wall trying to best Donal Knuth, or do you look at the discussion and then just copy it all over with your brain totally turned off? I don't get it.
Oh, here it is, in case anyone is wondering:
https://leetcode.com/problems/container-with-most-water/
17 comments
[ 1.3 ms ] story [ 49.0 ms ] threadI copied my solution elsewhere ran it, and it worked fine.
The only thing I could guess is the input wasn't what I was expecting, but I couldn't verify what input leetcode was actually giving me, so I just walked away.
The whole thing was a waste of time anyways. I still have yet to ever get a leetcode question during an interview and I've been doing this almost a decade now.
Then again, I don't try and get jobs at FAANG companies or FAANG wannabes.
Well, LeetCode is a CS-textbook-problems kind of site.
You use it however you like. While I was searching for my first job, I spent some time solving problems and optimizing them, half to keep my skills from degrading after college, and half just for fun. I just treated it like a puzzle collection and had a good time with some of the more open-ended questions. I'm sure it'd be useful to help learn a new language as well. I'm not convinced it's effective as a general skill-improvement tool, though.
I think that if you're stuck on a problem for more than 30 minutes, it's better to go read the solution and learn from it than to keep struggling. (Then go back a week later and see if you can solve the problem from memory).
Opinions certainly differ on whether leetcode makes for a good interviewing process. On your specific problem, there are a ton of similar problems where you need to traverse possible solutions in a particular order to be efficient. I regularly see similar problems at work.
No way anyone gets that in 30 minutes without having seen it exactly before. Moving pointers in from the sides, according to Y height, both at the same time when equal, and then not using that to find the solution, just to narrow the search space? I mean, it's not like I and everyone else didn't think along those lines, but that exact algorithm is totally non-obvious. I spent like 20 minutes on something similar, another 20 on a recursive split/merge thing that went nowhere, then got brute force working.
Here's the truth, there are over >200 people that apply for a single FANG job. Don't believe me? Buy LinkedIn premium and find out yourself. Talk to a Fang recruiter. Leetcode is just a way to filter people down to 5. It reflects nothing on your intelligence other than willingness to be a lemming that receives >200k per annum.
Of course trying to make this deduction during an actual interview when one or multiple people are watching you is an entirely different problem. Hopefully leetcode will give you enough practice to recognize patterns so that you don't need to think too deeply during interviews.
[3,2,100,100,2,3]
So you start on the outsides working in with your method, and you will stop at the two outermost 3s, because the 2 is less area, and never make it to the inner 100s. That's why the actual algorithm is to keep moving the pointers in until they meet, in a very specific way, keeping track of the maximum the whole time. It's not about actually finding the maximum and stopping early, it's about reducing the search space to O(n) from the naive O(n^2).
EDIT: However! There is a condition where h[n] <= len(h), which actually makes your method work! But you never mentioned that, so I think you may have gotten it by accident. :D
Like, with that condition, it becomes something like this:
[1,1,6,6,1,1]
and then stopping at the outer 1s is equivalent to making it in to the 6s.
I guess my issue is that I didn't example the conditions well enough. There's was a huge clue in there!
[3,2,100,100,2,3]
[2,100,100,2,3] (keep right 3, check if 2-3 has a larger area)
[100,100,2,3] (keep right 3, check if 100-3 has a larger area)
[100,100,2] (keep left 100, check if 100-2 has a larger area)
[100,100] (keep left 100, check if 100-100 has a larger area)
In reality, after you practice a lot of these problems, you realize that there are only a few broad categories and that most of them just use what you learned in school. You just have to practice to become fast.
We have yet to see outsized results from the leetcode group, but we have seen outsized results from the pre-non-leetcode group.
Results matter, as the leetcode dipshits would say.