Should I Avoid Built-in Functions When Solving Problems?

0
15
Asked By CuriousCoder42 On

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?

4 Answers

Answered By DevDreamer77 On

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!

Answered By TechieTyler On

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.

Answered By CodeCrafter89 On

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!

Answered By SmartSolver On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.