Five Leetcode Programming Patterns to Get the Entry Level Job of Your Dreams.
So if you’re if this deep into your Medium rabbit hole, you’re definitely searching for one thing and one thing only — that sweet, sweet entry level software engineer position. Believe me, I’ve been there.
Some stressed out devs will corner you and tell you that if you memorize the top 100 problems on Leetcode, that is your ticket to development’s ‘promised land.’ These guys will belt the number of Leetcode problems they have memorized for some kind of external validation, and while it is impressive to sit in front of a computer for that long, I would argue an alternative strategy.
You see, the idea comes down to patterns. If you can identify what the problem is asking, then the lines of code that you need to write is easy money, all layups. Here are the five most popular Leetcode problems according to the website, and the most efficient pattern to solve them.
Problem 1: Two-Sum (Difficulty: Easy)
So basically, it’s just saying “what two numbers can be added together to get the target number.” For these kind of problems, whenever you see a number that needs to be added to get to another number, you immediately need to create a comp (complement) variable. With this variable, you can save the complement and do a quick, easy search within the array to see if the complement exists. Then, you return the pair.
Problem 2: Adding Linked-List (Difficulty: Medium)
Problem two at first glance, can be understood as a simple addition problem. However, when each Linked list node only contains one number, it becomes a little less intuitive. There are two tricks here — first, you must create a dummy node and attach it to the linked-list that you would like to add. Because you are iterating through the linked-list, you will need to loop back to the head at some point. By doing this, you are able to return the head command.
The second trick involves using a carry variable to store any value that’s greater than a single digit. Because the arithmetic can produce a value as large as 18 (9+9), you must have a carry to store the tens place digit.
Problem 3: Longest Non-repeating Characters (Difficulty: Medium)
Ok, so right off the bat you realize that we need to return an integer that indicates the longest time something can go without a repeated character. Two pro tips for these kind of problems:
First, whenever you see repeated or any phrase thereof, your brain should automatically think of a dictionary. By utilizing a dictionary, we are able to save each character exactly one time, thus ensuring that the variables that we have saved do not repeat in any sort of fashion.
Second, when a problem is asking for the length of something (be it the actual integer or simple a substring), the way to calculate the length of that item is:
end _of_phrase — start_of_phrase + 1
Get in the habit of calculating the length of two items like this, and you will save yourself hours of headache attempting to figure out why your return is just slightly off.
Problem 4: Longest Palindromic Substring
This is the bread and butter of interview questions. I think I have almost never had an interview where I was not asked this question. Thus, you have to have a solution ready when the time comes.
The trick with this one here is that the interviewer is looking for you to parse the solution into another method that you can call. With this method, in this case we call, expandCenter, we can write a simple snippet of code that simply expands the string until the two characters do not match anymore. Notice that this time, when we return the length, we instead subtract the one instead of adding it against the grain of our formula listed above.
The more complex trick with this problem is that we want to make sure the max_length that the palindrome can be does not exceed the length of the string in the first place, within our constraints. Thus, we can write a conditional check to divide the maximum length of a returned string in half, so that it may function within the bounds of the problem.
Problem 5: Largest Rectangle Possible
This is another problem that, if you have a longer software engineer interview scheduled, you will see. The reason is that this problem is somewhat complicated to explain, and so there will be multiple steps involved. The graph gives the situation much more context.
This solution is rather straightforward, where the user does checks starting at opposite ends of the data, and checking to find its optimal container to save within a maximum value. However, albeit basic, an important interview practice note is that when you want to iterate across indices in Python, you need to manipulate the for loop a bit.
In Python, the for loop will give the iterator the value of whatever variable type you are iterating across (letter if it’s a word, index if it’s an array, etc.). Therefore, if you would like to iterate across the actual indexes of the array, you must use the range() function to output the numbers from 0 to the length of your desired string or list, as shown in this example.
So there you have it. Memorize these five patterns, and I can’t promise that you do move on in your technical interview (I mean, who REALLY knows which questions you’ll be bombarded with), but you’ll have the peace of mind that these will be in the back of your mind. Until next time!