LeetCode: Learning Data Structures and Algorithms

LeetCode

As part of my personal development program in Bitrock as a Junior Frontend Developer, I wanted to delve into the amazing world of DSA (Data Structures and Algorithms),trying to solve those seemingly impossible LeetCode issues, as well as applying those algorithms in a real life project. So I set off, knowing that the road would be rough, but with a firm resolve that I would eventually succeed in conquering the DSA.

What’s LeetCode?

LeetCode is a popular platform used by developers to hone their skills in data structures and algorithms. It offers a wide range of problems, from basic tasks such as array summation to more complex challenges involving intricate data structures such as trees.

From Excitement to Burnout

Going back to my learning experience, I started learning by enrolling in a Udemy course called “Javascript Algorithm and Data Structures Masterclass”. I was well aware that Javascript wasn’t the best programming language for solving DSA problems, but at the time I wasn’t interested in learning another language just to solve LeetCode problems. As I watched the course, I thought to myself “This is not so bad, I can solve these problems without the help of the instructor”, but I was wrong. 

The course covered problem-solving patterns and some of them were 2 pointers, sliding window and frequency counter. These patterns are really useful for optimizing your code, they prevent the use of nested for loops and are easier to read.

A good example would be checking if a word is palindrome. Instead of using a nested for loop (slow approach), you could have 2 pointers such that they are initially 0 and array.length - 1 and increment left and decrement right after 1 cycle if they are the same character. It greatly optimizes my code because if I used the nested loop approach, my time complexity would be O(n²), much slower than O(n) using the 2 pointer approach. 

After absorbing the content of the course, I tried to solve some of the problems offered by LeetCode and realized how underprepared I was, unable to solve even the simplest problems. I tried to move on to other problems, but most of the time I couldn’t formulate a solution. I tried and tried, but for every 10 problems I saw, I could only solve 2 or 3 of them. And so, feeling burned out, I took a break from DSA.

An example of a problem I couldn’t solve was LeetCode 1876 titled: “Substring of size 3 with distinct characters”. This is a classic sliding window problem, but I could barely figure out the conditions I needed to implement in order to make the window work.

Now that I have more experience with DSA, I realized that this problem was labeled “easy” for a reason, there was no ‘extra’ logic to implement, unlike the intermediate problems, it was just the plain old algorithm. All you had to do was set 2 pointers to the first and third character of the string, move left and right from there, and increment your “result” variable if there is a match and when the right pointer reaches the last character of the string, check if there is a match and finally return the result.

Mastering DSA through LeetCode

Initially, I felt overwhelmed by the DSA problems and contemplated giving up. However, as I delved deeper into the course material, I realized that perseverance and practice were key. I started with the simplest two-pointer problems and gradually progressed to more complex challenges. Over time, I noticed significant improvement in my problem-solving skills and speed.

I was amazed at how my problem-solving abilities evolved. I tackled challenges that once seemed insurmountable, from basic two-pointer solutions, to more advanced techniques like sliding window and binary search.
One particular problem I’d like to highlight is LeetCode 2824“Count pair whose sum is less than target”. Initially, I approached it with a two-pointer solution, but I wasn’t satisfied with the performance. A colleague suggested using binary search, which led me to optimize the algorithm by sorting the array first. This improved the time complexity from O(2 (n log n)) to (O(n log n).

Conclusions

My DSA journey is far from over. I’m eager to explore more advanced topics like trees, DFS (Depth First Search) and BFS (Breadth First Search) algorithms and custom sorting implementations. While I haven’t directly applied many of these concepts in web development projects, they’ve significantly enhanced my problem-solving skills and approach to coding. I’ve become more proactive, creative and thoughtful in tackling challenges, adopting a more defensive mindset to anticipate potential issues like incomplete or erroneous data. This has undoubtedly improved the quality and efficiency of my code.


Main Author: Miguel De Leon, Front-end Developer @Bitrock

Do you want to know more about our services? Fill in the form and schedule a meeting with our team!

Skip to content