Of LeetCode, I

Posted in Engineering

It’s been a while since I tried solving programming challenges. I decided to brush up my programming skills with LeetCode, and it feels “weird”.

We will look into one of the infamous 150 questions Study Plan on LeetCode’s questions and see how we can solve it. Along the way, I will share my thoughts on how I approach the problem and how I think about the solution; and why I think these kinds of problems are not really suitable for brushing up your programming skills.

The First Puzzle, 88. Merge Sorted Array

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

You can see the full problem here.

The Solution

Spoilers Ahead - If you want to try the problem yourself, please do so before reading the solution.

var merge = function (nums1, m, nums2, n) {

    nums1.splice(m, n)
    nums1.push(...nums2)
    nums1.sort((a, b) => a - b);

};

It feels weird to work on this problem because it is a combination of things that we can do on our own, and the other is something that has been researched for years.

Sorting an array is one thing, doing them efficiently is another thing. Sorting is a “hard” problem, and there have been a lot of algorithms being developed to solve this problem.

nums1.splice(m, n)
nums1.push(...nums2)

This part is very straightforward, we don’t need the placeholder in JS, so we can just remove them and append the second array. Alternatively, we can also iterate and override it on our own.

for (let i = 0; i < n; i++) {
    nums1[m + i] = nums2[i]
}

Then we sort the array, and we are done.

nums1.sort((a, b) => a - b);

JavaScript has a built-in sort function, which I trust more than my own implementation. There are a couple of differences in implementation between engine, but it is generally fast enough for most use cases.

V8, for example, uses Timsort for sorting arrays. https://v8.dev/blog/array-sort.

While SpiderMonkey uses a combination of Insertion Sort and Merge Sort.

The Problem

In case you haven’t realized, LeetCode and interview that uses similar questions are weird because you usually are not expected to use any of the built-in functions. You are expected to implement your own sorting algorithm, and you are expected to do it efficiently.

While this is strength on its own, it is rarely what you do in your day-to-day job. If it’s a critical piece of your application, you would spend hours or days researching the best algorithm for your use case, and you would probably use a library that has been tested and used by many people.

So, what to do?

We don’t really have a consensus on how to do hiring interviews. Some people think that LeetCode is a good way to evaluate candidates, while others think that it is a waste of time. Some companies use it as its first funnel, while others use it as a last resort. People start curating companies that don’t use LeetCode style interviews. There seems to be a lot of opinions on this topic.

There are a lot of nuances in this problem. If the candidates can’t do LeetCode, does it mean that they are not good? If the candidates can do LeetCode, does it mean that they are good? Can senior developers be considered senior if they can’t even do LeetCode? Can junior developers be considered to be senior if they can do LeetCode?

Of course, for the candidates, the solution is to just learn how to do LeetCode to increase your chance of getting hired. At the same time, don’t limit yourself to just LeetCode; there are a lot of other things that you can learn to improve yourself. For example, learning how to design a system, how to write a good test, how to write a good documentation, how to improve development experience, and a lot more.

For me, I wanted to increase my exposure to different problems and different solutions; and LeetCode and other platforms seem to be a good place to do it.

Will I use it on my day-to-day job? Probably not.