Tech IndustryDec 10, 2020
RaytheonihateLC

How do you guys figure out these solutions on LC?

I want a better job and I’m starting on leetcode. I consider myself a decent coder, but some of these solutions seem clever and I feel like I’d never be able to think of them on my own. For example, in the question about detecting a cycle in a LinkedIn list I used a hash table to store the memory address of the node and the frequency. The moment any address shows up a second time I can return true. When looking at the solution, it said to use two pointers, one moving one step forward and one moving two steps forward. They will eventually meet if there is a cycle. This makes sense to me and it uses less space.... but it doesn’t seem like something I could’ve come up with on my own which is discouraging. Sure, after reading the explanation I can code it up, but I didn’t come up with idea on my own. Is everyone able to figure these out on their own by just thinking about the problem for a while? Does anyone have advice on how to get better at coming up with clever solutions like that? TC: 78k :( YOE: 1.5 #Leetcode #lc #tech #leetcodeeasy

Google test@ Dec 10, 2020

You come up with clever solutions by thinking about them. It's ok to not know the answer. Understand the solutions and keep them in the back of your mind. In CS, there are not too many algorithmic ideas (maybe in 100s but not more). Once you do a lot of problems, you will see patterns developing and how problems reduce to the same set of ideas. You are invincible once you reach that stage.

Raytheon ihateLC OP Dec 10, 2020

Thank you for the response, can’t wait for that day to come :)

Volvo ascnrdrkly Dec 10, 2020

The one you're talking about is a classic algo, you might need to brush up on theory.

Microsoft boring-rp Dec 10, 2020

By brush up do you mean memorizing?

Intel zcLE73 Dec 10, 2020

I was in the same boat, started easy LC for FAANG interviews. It comes with practise and understanding the logic. Keep at it. You’ll get better.👍🏽

Raytheon ihateLC OP Dec 10, 2020

Thank you 🙏

New
GkTe12 Dec 10, 2020

you learn the pattern, in this case it is “slow fast pointer”. then when you get stuck on a linked list/ array question, you try it and see if it helps. with time it becomes simple pattern recognition. also, the particular question you are talking about is a very very popular one, and no, almost no one gets the O(1) solution without heavy hints. you may as well just learn it and move on

Raytheon ihateLC OP Dec 10, 2020

Gonna keep practicing, it’s all about understanding it and recognizing when the pattern could be used! Thank you!

IntelliSurvey FNoa45 Dec 10, 2020

This technique is discussed even in CTCI. After doing some problems, next time you see similar problem you should recognize the strategy

VMware vMojoJojo Dec 10, 2020

Don't beat yourself up. Unless you are interested in coming up with new algorithms, like floyd's cycle detection one that you mentioned, you just have to understand the concept behind it and learn why it works. Make notes of all these learnings and revise over time (see spaced repetition). You'll eventually start getting better. Just keep at it.

eBay Leroy Dec 10, 2020

Who answered above answer the wrong question. It’s not about “how” to solve it (once you know the patterns, you can decrease a problem to one of those), but about whether one can come up with the best solutions without seeing anything like that before. My answer is that normal people cannot come up with ideas like “slow fast pointer” or a 2D array for a longest increasing subsequence without being shown how to do it. There are exceptions for sure; people on the level of DeepMind, renaissance technologies, etc

Google test@ Dec 10, 2020

If you have never seen anything remotely close to a hard problem before, you will reinvent the solution of a paper published in the 80s and coming up with those insights is not happening in the interview. Computer Science problems are solved via reductions. To do the reductions, you need to know the fundamental ideas. You learn the fundamentals by practicing. As simple as that.

Intertrust Technologies yeah_nah Dec 11, 2020

Opinion of competitive programmer: slow/fast pointer trick is one of the oddest ones I've ever seen.