Should I Avoid Built-in Functions When Solving Problems?
Asked by
PAPER TIGER
I've been tackling LeetCode challenges for about a month now and initially aimed to avoid built-in functions to better grasp algorithms. However, I found myself spending a lot of time on one problem that could have been solved in just two minutes using built-ins. So, I'm wondering: should I stick to avoiding built-ins to really understand the algorithms behind the problems? For instance, in problem 151, my approach ended up being really messy and hard to read, which is not ideal if I were collaborating with others. What do you all think?
Answers
There are two parts to this:
1. Use functions that speed up your coding without sacrificing performance. If the problem doesnโt need a super optimized approach, feel free to use built-ins.
2. Take a step back and consider why your code is looking messy. Can it be simplified? Sometimes there's a more elegant solution.
Reply to this answer
Totally get what you're saying! If the task is literally about "reversing words in a string," then relying on split().reverse().join() takes away from the learning experience. On the other hand, using a hashtable for lookups in a totally different problem? Thatโs just smart coding!
Reply to this answer
I think it's fine to use built-in functions and standard data structures as long as they aren't central to the problem. For example, using a hashtable might be acceptable unless the problem specifically revolves around implementing one. In your case, though, using functions like split, reverse, and join might defeat the purpose of the task since it essentially asks you to reverse the words in a string.
Reply to this answer
Honestly, don't overthink it too much. Your messy code is what interviewers are often looking for. A clean one-liner with built-ins might actually hurt you more than help. Plus, working through the messy solution helps you learn more!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions