5 Most Common Amazon Coding Interview Questions for 2022

33.96k views3779 WordsCopy TextShare
Codebagel
These are the most common coding interview questions that Amazon gives for software engineering inte...
Video Transcript:
THESE are the 5 TOP coding questions that Amazon gives for software engineering interviews, and I’m going to walk you through how to solve each of them. If you want to get a job as a software engineer at Amazon, I highly recommend you know these 5 questions very well. If you can solve these 5 questions, you should feel very comfortable walking into your Amazon interviews.
And if you can’t yet solve them, no worries, I’m going to walk you through the steps you would take to solve them. I’ll be explaining the way we solve the problem first, and then coding it out. I’ll be coding in Python, but in the description there’s a link to the solutions page on LeetCode so you can see the actual code explained in any language.
Let’s not waste any more time and get right into our list of the top 5. 5) Search Suggestions System We’re starting off with search suggestions system, which is a Medium on LeetCode, although I think it’s on the easier side of things. Amazon loves string questions and sorting questions, and this one has both.
For this question, we’re given an array of strings, where each string is a product, and a searched word. Like when you actually use Amazon to search something up, every letter you type brings up suggestions for what you’re searching for. Our goal here is to return lists of at most 3 suggested products as each letter of the word is typed.
So using this first example here, you can see that we have 5 products, and our search word is mouse. When we type the m, we want to return 3 suggestions. However, all 5 start with m, so what do we do?
Well, the problem tells us to return the 3 lexicographically minimum products, which is just a fancy way of saying alphabetical order. So, we would return mobile, moneypot, and monitor, because these 3 are alphabetically earlier than the other 2. That’s the first list in our result.
The next list would be of the suggestions after we type “mo”. Again, all 5 start with “mo”, so our second list is the same as the first. Our third list is the suggestions after we’ve typed in “mou”.
Now, only 2 of the products even match this search, so our third list is just these 2. If we continue for “mous” and “mouse”, these 2 products are still suggested, so the fourth and fifth lists are the same as our third. And there we go.
Those steps we just manually went through are the same steps your program is going to go through. Now that we’ve laid it out, it’s much easier to code up. The only trick to this question is figuring out how we go through the product list.
The product lists are not necessarily sorted in alphabetical order, so if we have more than 3 suggestions, how do we know which 3 to pick? Well, we’ve said right there. The product lists aren’t sorted in alphabetical order, so let’s sort them!
Sorting the products is the very first thing we’re doing to do here. In Python, it’s very simple, we just have to add the . sort method to the end of the list.
Now, we need a way to go through the products, so we’re going to use a left and right pointer. We also need a result array to store our suggestions for each letter in. All we’re going to do now is go through each letter in the search word, and check if that letter is also in the same position as the products that our left and right pointers are pointing at.
As long as our left pointer doesn’t pass the right pointer, there’s 2 ways we can remove a product from the suggestions. If the length of the product is less than the position of the search letter, we know it can’t be a suggestion, and if the letter we’re looking at doesn’t match the search letter, we also know it can’t be a suggestion. If either of these two things occurs, move your left and right pointers in, essentially removing the product from the list.
Remember, if our product doesn’t match this search suggestion, it can’t match the rest, so we’re fine to remove it from consideration for the rest of the problem. When these conditions can no longer hold, it means every remaining word is a match for the current search. We want to check how many words are left, and if that number is equal to or greater than 3, we just want to append the leftmost three words, as these are the three earliest in the alphabet.
If we only have 1 or 2 words that match, we can just append them to the result. And if no words match, we’ll append an empty list. We’ll return our list, and voila!
We’ve solved the first question. Let’s move on to number 4. 4) K Closest Points to Origin K Closest Points to Origin is probably one of the easiest Medium LeetCode questions on the site, and is very straight-up in what it’s asking.
There are no tricks or patterns to identify here – you just need to code it out. We’re being given an array of points on a 2D plane and being asked to return a certain number k of closest points to the origin, which is at (0,0). So if k were 4, we’d return the 4 points with the smallest distance to (0,0).
Luckily for us, this question also gives us the distance formula that we’ll be using. All we have to do is go through each point in the list of points and calculate it’s distance to the origin. Once we’ve done all the calculations, we find the smallest distances, and add those points to the result.
As I said, this question is pretty self-explanatory and there’s not much to read into. The only thing that sometimes trips people up is that we’re sorting the distances, but then have to get the points associated with these distances. That can easily be solved in a variety of ways, like a HashMap or a Heap, but I’m actually going to show you a more simple way that doesn’t even require use of one of these data structures.
To start, we know we need an empty array for our result, and we’ll also need an empty array to store the distances we calculate. The first thing we need to do is actually calculate those distances. We’ll go through each point in the points list, and look at their x and y values, which in Python we can do like this.
Our distance formula is given to us, so we can just recreate that in code, and as you’ll notice, it ends up being pretty short because the origin’s coordinates are 0 and 0, so they aren’t even in the calculation. Now, once we’ve calculated the distance, we want to add it to the distances list, but that’s not all. Remember, our result is going to be the POINTS with the shortest distances, so we’re going to add the x and y coordinates with the distance so we can keep them paired together.
Once we’ve gone through each, we have a list of distances that is not yet sorted. What we need to do here is sort the distance list so we can quickly pop off the smallest distances. However, each element in the distance list is an array with a distance, and x value, and a y value.
We don’t want to sort by all of these, but the distance value only. To do this, we have to give a custom key to our . sort method.
In Python, you do that like this. Our key is going to be lambda, which basically represents an unnamed function, and we’ll call the variable we’re sorting, x. This is where we add in how we want to sort.
We want to sort in descending order, so that our smallest distances can be popped right off of the end, and we want to look at index 0 for each element, which is where the distance is. Now, we take our k value. Let’s say it’s 3.
This represents how many points we have left to add. So we first take the smallest distance, which we know is at the end of the distances list, along with it’s x and y values. Then, we’ll add only the x and y values as a point to our result.
We’ve now added 1 of the 3 we need, so we subtract 1 from this, and now k is 2, meaning we have 2 left to add. This continues until k reaches 0. And there we go!
We just return our result, and bam! That’s another Amazon interview question we just knocked out of the park. Onto the next one.
3) Reorder Data in Log Files This Medium question is very similar to numbers 4 and 5 on this list, as it’s both a string and sorting question. In case you can’t tell by now, Amazon loves to ask these kinds of questions, and this one in particular is very common. We’re given an array of logs.
The first word in each log is an identifier, that marks it as either a letter log or a digit log. Letter logs have only letters after the identifier, whereas digit logs have only digits after the identifier. We’re told to reorder the logs based on 3 conditions.
One, all letter logs must come before the digit logs. Two, all letter logs are sorted lexicographically by their contents, and if these are the same, then by their identifiers. And three, digit logs stay in the same relative ordering.
Let’s take a look at example 1. We’re given 5 logs, two of which are digit logs, and three are letter logs. We can tell because the digit logs have only numbers, and the letter logs have only words.
Our result should have the letter logs all placed before the digit logs, and in alphabetical order of contents. In this case, we know that “art can” comes before “art zero”, which comes before “own kit dig”, so this is the ordering of the letter logs. As for the digit logs, we are told to maintain their relative ordering, so we keep “8 1 5 1” before “3 6”.
We’ve already looked at strings and sorting in our previous two questions, so we’ll dive right into the code for this one. First, we’re going to separate our digit and letter logs, so we’ll create two different arrays for these. After this, we’ll start off by going through each log in our list of logs, and putting any digit and letter logs in their respective arrays.
How do we tell if a log is a digit log or a letter log though? They both start with words as identifiers. But what about what they end with?
In this case, we know that digit logs always end with a digit, whereas letter logs never end in a digit. In Python, we can check if a character is a digit using the . isdigit method.
So, we’ll check if the last character in the log is a digit; if it is, we’ll add it to the digit logs list, otherwise it will go to the letter logs list. After we’ve done this, we could now add the digit logs list to the end of the letter logs list, and we would accomplish the first and third reordering conditions. But we still haven’t met the second, which is to sort by contents, so it’s time to do that.
All we need to do is call that . sort method we’ve used a few times this video, and build our own custom key. Remember, we do that by typing key=lambda, and then choosing x as a variable.
Now, how do we want to sort? We first want to sort lexicographically by contents. The contents are all words past index 0, so we split the log up using the .
split method, and then sort by everything from index 1 to the end. After sorting this way, if any two logs have the same contents, we want to sort by identifier, which is at index 0, so we add that sorting condition in after the previous one. Our letter logs are now sorted.
All we do now is add the digit logs to the end of our letter logs, and return the result. Just like that, we’ve solved question 3 of the most important Amazon questions. 2) LRU Cache Finally, a break from all of the string and sorting questions.
This question is an object-oriented design question, and gets asked by Amazon all the time. There is a trick specific to this question that you kind of have to know when solving it, so I’ll explain the question briefly and spend more time explaining how the solution actually works. Our goal here is to create a data structure for a least recently used cache.
This data structure should have a capacity, representing the size of key-value pairs it can hold. It should also have two methods, get and put. Get should return the value of a key, or -1 if the key doesn’t exist, and put should add a new key-value pair to the cache, unless the key already exists, in which case it just updates the key’s value.
However, the “least recently used” functionality comes into play here, because if we are already at max capacity when we try to add a new key, we want to replace whichever key has been used least recently. A key is considered to be “used” when either get or put is called on it. So, we need a way to keep track of what the least recently used key is, and that’s the trick to this question.
If we were just storing values in an array, we could move the value to the end of the array when it is used, and that would allow us to order values by how recently they were used. But we have key-value pairs, and the question calls for get and put to run in O(1) time, which means we’ll be using a HashMap to store this information. How can we order a HashMap by how recently the key was used?
Well, luckily for us, there’s actually a unique data structure we can use called an OrderedDict, which is basically a HashMap that can be ordered through the use of the . move_to_end method. Let’s start coding this one up, and we’ll see how this OrderedDict works once we get to it.
The first thing we would do would be to complete the init, because it’s really simple. All we have to do is set self. capacity to capacity.
Now let’s complete the get function. All we’re going to do is check if the key already exists in our LRUCache. If it does, we’ll return the value for this key, and if it doesn’t, we’ll return -1.
There’s one thing we’re still missing though – how do we mark this key:value pair as being the most recently used? This is where the OrderedDict comes in. We can call a method called move_to_end, which will move this key:value pair to the end of our OrderedDict.
In order to use an OrderedDict, we need to first import it, like this, and then we need to tell the LRUCache class to inherit the properties of the OrderedDict, like this. Now, our LRUCache is an OrderedDict, and our get function works properly. Lastly, we need to complete this put function.
We first want to set the given value to the given key, which we can do like this. If we’ve just changed an existing key, then everything is ok. But if we’ve added a new key, we need to check if we are now exceeding the capacity, by checking if the length is greater than the allowed capacity.
If we are, we want to pop the item at the front of the OrderedDict, which we can do by calling the . popitem method with last set to False. After we’ve done this, we’ve met the capacity, so our OrderedDict should be all good.
But remember, we just used this key, so we need to call the move_to_end method on this key so we order it as the most recently used. Now we can run it, and as you can see, everything works. Of all the questions on this list, this one definitely has the most specific answer, and would be hard to come up with if you didn’t already know the OrderedDict trick.
I recommend practicing this one multiple times so you remember how to do it if it comes up in your interviews, which as you can tell by its place in the number 2 spot, is quite common. 1) Number of Islands THE classic graph question. If you’ve done any graph questions on LeetCode, there’s a solid chance you’ve come across this one.
There are tons of graph questions on LeetCode that are based on this one, and Amazon loves to ask this question reworded with different situations. You’re almost guaranteed to see a close variation of this question in any interview, so let’s make sure we know how to solve it. The question tells us we’re given a 2D grid which represents a map of land and water, where 1s are land and 0s are water.
Land that is surrounded by water is considered an island. If two pieces of land are connected horizontally or vertically, they are both considered the same island. Our goal is to return the total number of islands for any given grid.
Let’s look at both examples briefly. In the first example, we can see that every 1 is connected to another 1 either vertically or horizontally, making them all 1 single island. In the second example, the first four 1s are all connected, making them 1 island, but there’s also a 1 on it’s own in the third row, as well as two 1s in the bottom right corner.
Therefore, there are 3 islands in this grid. So, how do we determine the number of islands? Well, let’s think about how we would do it manually.
We would go through the grid until we found a 1, a piece of land. From here, we’d look to its left, right, up, and down. If there’s another 1 in any direction, we go to that 1 and look in all directions for it.
Once we’ve seen all of the 1s that are a part of that island, we make a note that that’s 1 island, and we continue through the grid, only stopping when we find a 1 that was NOT part of the island we just saw. Hopefully thinking about it that way makes the problem seem a bit less complicated, because that exact process is what our code is going to follow as it executes. Going down the pieces of land each time we see a new 1 is a common graphing algorithm called depth-first search, or, DFS.
If you’re still confused, I recommend you check out THIS video that I made explaining the common algorithms you should know for interviews. Once you understand depth-first search, this problem becomes a simple implementation of the algorithm. Now, let’s get to coding!
The first thing we need to do is check how many rows and columns we have in the grid, in order to keep track of where we’re at. As is standard with any DFS algorithm, we also need to create a set called visited, to keep track of pieces of land we’ve already counted. Lastly, we need to create a result, which we’ll set to zero as we have not seen any islands yet.
Now, we’ll create the DFS function, which will take in a row coordinate and a column coordinate, which gives us a point on the grid. We first want to check if this point has been visited. If it has, we’ll exit the function now by returning nothing.
If it hasn’t been visited, we’re visiting it now, so let’s add it to the visited set. From here, we want to check if there’s extending land in any of the 4 directions around this piece of land. We can do this by first creating a directions list, which looks like this.
Then, we’ll test moving in each direction. For each move, we’ll check if the new row coordinate and new column coordinate exist on the grid, and if they are in range, we need to check if they’re land or water. If our new grid point is a 1, we know that this island continues in that direction, and so we will now call the DFS function again on this new piece of land.
This process repeats until we’ve explored every piece of land on this island. Finally, we need a way to go through this grid from the beginning, and to actually start the counting of islands. To do this, we’ll go through every single row in the grid, and every single column in each row.
So, we’ll start at row 0, in column 0, and check if it’s an unvisited piece of land. Remember, if we’ve already visited it, we’ve already accounted for this island, so we want to ignore it. As long as it’s unvisited, let’s call our depth-first search on these coordinates, and go through this entire island.
After we’ve marked this entire island as visited, we’ll increase our island count by 1. Just return the result, and there we go. You’ve officially solved the top 5 Amazon interview questions, and it only took us 20 or so minutes.
Conclusion If you’ve stuck around til the end, thanks so much for watching! Please like, subscribe, and most importantly, please share this video with your friends. I want to help as many people as possible, and I need all of your guys’ help to get these videos out there.
Also, we have an awesome Discord community for both coding beginners and professional software engineers. If you’re interested in learning more about programming, or you want a community of developers to chat with and send memes to, this is the place. It’s also the best place to stay up to date with the channel, as well as enter in upcoming giveaways.
Link to join is in the description. Thanks for watching, and I’ll see you in the next video!
Copyright © 2025. Made with ♥ in London by YTScribe.com