Table of content:
- Amazon Recruitment Process
- Amazon Interview Rounds
- Common Amazon Interview Questions
- Amazon Interview Questions: Behavioral-based Questions
- Amazon Interview Questions: Leadership Principles
- Company-specific Amazon Interview Questions
- 43 Top Technical/ Coding Amazon Interview Questions
71 Amazon Interview Questions With Answers You Must Know!
What characteristics do I look for when hiring somebody? That's one of the questions I ask when interviewing. I want to know what kind of they would hire.
-Jeff Bezos
This statement by Amazon CEO Jeff Bezos is a peek into the hiring methodology at the e-commerce giant. Getting the opportunity of interviewing at this organization is the stuff of dreams for many. So when you do get this opportunity, you want to give your best. And to help you in this endeavor, in this article we have listed some of the most important Amazon interview questions. We will also give you a brief on the Amazon interview process and rounds, along with some essential tips to smooth this journey out. So let's get started.
Amazon Recruitment Process
The Amazon recruitment process is split into 4 stages, as depicted in the image below.
In this article, we will focus on helping you prep for the interview rounds by listing top Amazon interview questions with answers. For more details on the Amazon recruitment process, check this article out- Everything About The Amazon Recruitment Process | Salary | Preparation Tips
Amazon Interview Rounds
The interview process will be made up of more than one round including a phone interview and multiple in-person interviews. The Amazon Behavioural Interview, Amazon Technical Interview, HR interview, etc are a few types of interviews you might have to go through. The type of Amazon interview questions asked during these rounds will depend on the kind of profile you are applying for. But irrespective, you must be prepared for anything that might come your way. There are four primary categories in which we can categorize most Amazon interview questions. They are:
Leadership principles | Behavioral interview questions |
Technical/ coding questions | Company-specific questions |
Job seekers appearing for software development engineering and similar profiles will be asked varied technical questions to test their suitability for the position, along with other types of questions. We have included a category-wise list of common interview questions in this article to help you prepare for the upcoming interview.
Common Amazon Interview Questions
In this section, we will see some of the most important, non-technical, Amazon interview questions for both freshers as well as experienced professionals. Most of these questions have subjective answers, but we have included sample answers with proper explanations everywhere.
Q1. Why do you want to join Amazon?
This is one of the most commonly asked Amazon interview questions. The intent behind this is to ascertain your motivation for wanting to become a part of the organization, and further ascertain if your motivations/ reasons align with the company's thought process and goals. Your answer should showcase your passion and enthusiasm to work at the multinational company.
Also, read- 6 Key Tips To Answer "Why Do You Want To Join Our Company?"
Sample answer:
I feel really inspired by the company's commitment to innovation and its ability to continually improve customer experience by introducing new products/ services. And also the ability to integrate cutting-edge technology to serve its purpose. I am also aware of the numerous growth opportunities that the company will provide me with and am super excited to take on this journey.
Q2. Tell me about yourself. Or What are your future goals?
How you answer this question is again completely subjective. The ideal way to answer such a question is to pick a quality or trait or incident from your present life (or current role), then segue into the past (or previous roles) by mentioning how that incident/ trait was functional in a certain aspect of your life. And then go on to derive an inference of how this will help you in your future professional and personal life.
For more details read- "Tell Me About Yourself" Here's How To Answer This Interview Question Like A Pro!
Q3. What makes you a good fit for this company? Or Why should we hire you?
The answer to this Amazon interview question can be the perfect opportunity for you to showcase how you are different from other candidates and convince the interviewer to hire you. To frame the perfect answer, read through the job description and use the requirements in there to prove your job fit, traits, skills, experience, etc.
For some sample answers read- How To Answer "Why Should We Hire You?" (With Examples)
Q4. In your previous company, which project was your favorite?
Take any past project or case that best portrays your skills and back it up with a proper explanation. If you are a fresher, you can use instances from your internship experiences to frame an answer.
Sample answer:
The development of a food delivery application was my favorite project as we got to learn many new aspects of software development.
Q5. Have there been conflicts with a superior/ manager?
The interviewer's intent behind asking this question is most probably to see how you handle conflicting situations. It is best, to be honest, and clearly state an instance of conflict you have faced.
Sample answer for tech roles is:
Normally tech companies are always extremely sorted. There have been times when I had a difference of opinion with my manager, but never an issue of conflict because of effective communication from both sides.
Q6. Did any of your ideas get implemented and resolve the issue?
With this answer, the interviewer is most probably trying to see if you are proactive, take initiative, and have analytical skills & out-of-the-box thinking. Try to showcase that you do possess these traits with the help of a past incident and give a proper explanation.
Sample answer:
I have used the shuffling method in the core development team where developers switch roles after a fixed time period to relieve them of work pressure. This idea got implemented and resolved the issues of our company's IT team and also the non-IT team.
Q7. Any example of you working out of your comfort zone?
Adaptability and agility are extremely important traits for those who want to thrive in the corporate world today. And the intent of this Amazon interview question is to see if you do too. So frame an answer that showcases this, and assures the interviewer that you are the ideal candidate for the job.
Sample Answer:
Every project has its challenging bits which require us to work out of our comfort zone. I cannot singularly point to such instances as there are many of them.
Q8. The most critical/ negative feedback you got from your team or manager.
Senior people give correct insights most of the time. But I won't call them critical as they were concerned for my well-being. I have worked overtime multi-tasking and my health has suffered as a result. The manager asked me to not strain much over an issue. Apart from this, I have never received any critical feedback.
Q9. Any time when you were faced with a problem that had a number of possible solutions. What problem you faced and how did you solve it? What was the result you got after employing that solution?
This is an important Amazon interview question for freshers as well as professionals. This is because there are many times in our lives we come to a crossroads, and the way we proceed then says a lot about our being. Have a look at the sample answer below to know how to answer this question.
Sample answer:
There was an instance in midst of a project where the testing part could be done in multiple ways. But, we took the simplest test and the result of it was that we could find all the flaws in the code in a single step and we didn't have to revise the code multiple times.
Q10. When did you take a risk, failed, or made a mistake? How do you respond to difficult situations and what was the experience you gained out of them?
Although, there have been some instances when I have made a mistake after taking a risk. But such instances have been very few. There is no exceptional mistake that I have made till this point in my career. But if you want a single instance. During one project where we had limited time for the development of software, I used a particular tried and tested method to get the project delivered before the deadline. However, due to some reason, we lagged and delivered the project two days after the deadline. That is the only time I took a risk and failed to deliver. But I kept my focus and kept on going along with the team and did not take much time to deliver the project even after the given deadline.
Q11. Describe an instance when you led a project.
As you will see in the article ahead, leadership skills and principles are especially important for Amazon. Answering this Amazon interview question right is your shot of grabbing brownie points.
Sample answer:
Yes, during one project where we had to develop banking software for a team, I led my team and received positive feedback mostly from my peers, my team, and my seniors. My leadership role was appreciated and I was made a team lead after that project.
Q12. Have you used data and developed a strategy?
Yes, I have done so in my previous company/internship. I had to wrangle a large data set to create a strategy for building a food delivery application. The development of the app was data-driven, so we had to be careful with the deployment of the app at every step. We made a strategy and managed to develop the food delivery app at the optimum time. (This is a sample answer to these types of questions. You can use your storytelling ability to concisely tell the recruiters about your capability to deal with a difficult decision in your previous job experience. The answer should bear a positive impact on the interview.)
This brings us to the end of the common Amazon interview questions segment. From here we will move on to one of the most important segments for job profiles across departments at Amazon. That is, behavioral questions since they are integral to determining if a candidate is suitable for the company and its work culture, or not.
Amazon Interview Questions: Behavioral-based Questions
The behavioral-based questions asked in Amazon interviews are intent on finding out about the past situations and challenges, candidates have come across. And also the way they have handled these situations. The foundation of these behavioral-based interview questions lies in the 16 Leadership Principles stated by Amazon.
You can use these very Amazon core leadership principles to frame your responses in a way that shows you have all the qualities they are looking for. We have specified the principles in the section ahead. We will also discuss, one of the best ways to prepare for these behavioral questions before having a look at some common Amazon interview questions in this category.
Preparing for Amazon Behavioral Questions
The best way to prepare for behavioral interview questions asked at Amazon is the STAR method. This stands for:
- Situation: Quickly describe the situation. It’s best to focus on a specific moment, like an event or a team project.
- Task: Explain the goal you have labored for. This is a point where you faced an obstacle.
- Action: Describe how you and your team tackled the problem describing the process step by step.
- Result: What was the result of your team's endeavors? For this question, specificity is your best friend.
Use specific examples with specific numbers, wherever possible. The S-T-A-R method is a winning strategy when tackling such behavioral interview questions. Ensure each answer has a beginning, middle, and end. Describe the situation or problem, the actions you took, and the outcome.
Q13. Share an instance where you found an innovative solution to a problem.
The intent behind this question is to assess your innovative ability and also the ability o convert that into actionable plans. Take any instance from the past, and explain the situation along with the steps you took to materialize the solutions. Also mention how that benefited you as well as the organization you were a part of.
Q14. What are your strengths and weakness?
If you are thinking that sharing your weaknesses is a bad idea, it is not. In contrast, mentioning your weaknesses and strengths and explaining how you are working on them shows self-awareness. It also iterates on the fact that you are willing to work on your issues and wish to grow. So be honest and frame a proper answer to this Amazon interview question. Make sure to leave an impression on the interviewer.
Read these articles to find the perfect answer- What Is Your Greatest Weakness? Here's The Perfect Reply and Guide To Answer- "What Is Your Greatest Strength?" (Samples Inside)
Q15. Say your team members are not pulling their weight. How would you handle that?
This is not an uncommon occurrence in workplaces, and the way you handle such situations will tell the interviewer a lot about your capabilities and personality. Ignoring this situation and continuing your work will make you look ignorant. Some might think telling on your colleague is the way to go, but it shows untrustworthiness and incapability in solving problems. Your answer to this Amazon interview question must highlight your positives while showing that you have the ability to take tough decisions.
Sample answer:
The best way to handle these situations is to first have an intra-team discussion and make it clear that such behavior won’t work. Discussing it within your team shows that you are a team player, and bringing these issues up positively but sternly shows that you can stand up for yourself and the company’s interests. Also discussing the problem shows support towards teammates and willingness to find out, as well as work on the root cause of the problem.
Q16. Was there ever a time when you had to pivot completely before the completion of a project? How did you take it?
The corporate world today is changing at an extremely fast pace. And those who want to keep up must know how to pivot quickly. Your answer must show that you are prepared to change with time and work towards your goals undeterred.
Sample answer:
One of the projects I was working on in my previous job was near completion everything was proceeding in a timely fashion. But at the last moment, we encountered a system breach and we were thrown off balance. We had two options, one to take the loss and delay of about 3-4 weeks. And the second was to reallocate our resources to adopt a new approach that would help us recover from the breach in half the time. I initiated the reallocation and the pivot ultimately helped us stay ahead in the game.
Q17. Can you tell an instance of how you motivated your team during a particular project?
Yes, there was a time when we were building a difficult piece of software for a client company and the development time was extremely long. I suggested a switch role method for the core development team and it worked effectively because every developer got a chance to shuffle their role and get revived from mental stagnancy. After that, the entire company started using this method for various teams.
Q18. What according to you is more important- money or work?
Another tricky Amazon interview question, that might come your way during the interview. The answer to this is completely subjective, a sample answer is given below.
Sample Answer:
I believe both money and work are important, if you work hard enough, then money will follow. And money acts as one of the motivating factors that pushed one to work hard. So it is not an either-or situation, instead one of these fuels the other, and vice versa.
Amazon Interview Questions: Leadership Principles
Amazon takes its leadership principles extremely seriously and they hold relevance in all aspects of the organization, be it hiring, brainstorming, or decision-making. It is not surprising then that Amazon interview questions will also be based on these. The principles are:
Customer Obsession (Leaders work vigorously to earn and keep customer trust. And while they do pay attention to competitors, they are obsessed with customers.) |
Ownership (Leaders are owners who think about the bigger picture i.e. they don't sacrifice long-term value for short-term benefits. You will never hear them saying- 'that's' not my job'.) |
Invent and Simplify (Leaders always, always look for new ideas everywhere and require/ expect the same from their teams.) |
Think Big |
Learn and Be Curious (Leaders are always curious about new things and never stop learning.) |
Hire and Develop the Best |
Frugality (Leaders know how to accomplish more with less.) |
|
Insist on the Highest Standards |
Earn Trust |
Bias for Action (Leaders know when to put extensive study aside and take quick action on calculated risks since speed is important in business.) |
Deliver Results (Leaders rise to the occasion despite setbacks and always deliver results with the right quality and in a timely fashion.) |
Are right, A Lot (Leaders are right a lot. They have strong judgment and good instincts. They seek diverse perspectives and work to disconfirm their beliefs.) |
Dive Deep (No task is beneath the leaders. They operate at all levels and stay connected to the details) |
Have Backbone; Disagree and Commit (Leaders are obligated to act with conviction, and respectfully challenge decisions when needed. And once a decision is determined, they commit wholly.) |
Strive to be Earth's Best Employer (Leaders lead with empathy to work every day to create a safer, more productive, higher performing, more diverse, and just work environment while also making it easy for all to have fun at work.) |
Success and Scale Bring Broad Responsibility (We have come a long way from a garage. And with the greater impact we have on the world, we have to be humble and thoughtful about even the secondary effects of our actions. We must begin each day with a determination to make better, do better, and be better for our customers, our employees, our partners, and the world at large.) |
The principles also form the foundation of many behavioral questions. Now let's have a look at a few leadership principles based Amazon interview questions and sample answers.
Q19. What was the biggest mistake of your life? And how did you overcome it?
The best way to answer this Amazon interview question is to take an experience from your life and explain it in detail but with the context of how it helped you grow. It is important to show that you learned from that mistake and have grown into a refined version.
Q20. Are you short-tempered? What angers you the most?
Being angry isn't a bad thing till the time you know how to control it, and channel it right. So take any instance where you were agitated and reflect on how to found a solution to the anger issues, or how you channeled it and made something good out of it, etc.
Q21. What would you do if one of your colleagues stole an item worth $1?
It should be clear to you that the small amount of theft is a trap. While normal human behavior is to think that this is such a small amount that it won't affect the company's bottom line. But think what will happen if everyone stole $1 and turned a blind eye to others doing the same. Also, note that shrinkage is a major concern for Amazon. So there is only one right answer to this Amazin interview question.
Sample Answer:
Irrespective of the amount, theft is theft and must be addressed since it's not only against policy but also illegal. So if I witnessed something like this, I would follow company procedure and report it.
Q22. What is the way to understand customer needs?
Amazon places great importance on 'customer obsession' and aims to provide the best customer service. And to do so you need to know how to understand customer needs and must show the same to the interviewer.
Sample Answer:
I believe active/ keen listening skills, observational and analytical skills, and asking questions to get more insights, are key to understanding customer needs. And this is critical to ensuring customer satisfaction.
Q23. What is frugality and how do you commit to it in the workplace?
Note that 'frugality' is one of the core Amazon leadership principles. So you might come across this Amazon interview question. Your answer to this question must showcase that you know how to manage and save time, money, and other resources while delivering quality results.
Sample answer:
I believe that being frugal and inculcating it in your work methodology is important. In my previous role, I discovered an alternate software for one of our functions. This software did not only cost less comparatively but improved efficiency with enhanced features. I took this to my manager and we switched to it after a demo.
There are sixteen leadership principles in total with a wide scope of applicability. It is important that you understand these principles because a lot of Amazon interview questions can be based on them. One way to prepare is to analyze instances when Amazon applied these principles. For example, there are many instances where they showed exceptional customer obsession and went out of the way to gain customer trust. Try to incorporate them in your answers to such Amazon interview questions.
Company-specific Amazon Interview Questions
Amazon is a popular and widely known e-commerce giant, but you still need to prepare for company-specific Amazon interview questions you might come across. Some are:
Q24. Who is our CEO? How to spell and pronounce his name?
Jeff Bezos [pronunciation- Jef bay-zohs]
Q25. What do you understand by our 'Strive to be Earth's best employer' principle?
To answer this Amazon interview question, explain your understanding of this principle while also connecting with your own personality and how you would emulate it in work life.
Q26. What do you think is one of the biggest challenges for Amazon, today?
It is obvious that before interviewing at the company you do your homework and conduct a deep analysis of the organization. Use that knowledge to highlight what you think is a challenge and also how you would solve it.
Q27. Give me an elevator pitch for Amazon.
We all know that the purpose of an elevator pitch is to give a quick intro while putting two-three important points across, in a duration equivalent to taking an elevator ride. A sample answer to this Amazon interview question is given below.
Sample pitch:
From a modest garage startup to becoming a multinational e-commerce/ technology giant, Amazon is the brainchild of Jeff Bezos. It truly lives up to its namesake, the Amazon river as it spans the e-commerce, online advertising, cloud computing, digital streaming, and artificial intelligence sectors.
Q28. Which Amazon leadership principles do you relate to the most, and why?
There are 16 principles in total, read through all of them and reflect on the ones you relate to the most. Irrespective of whether the interviewer asks you this Amazon interview question or not, you must have a deep understanding of these principles if you want to work at the company.
43 Top Technical/ Coding Amazon Interview Questions
Amazon operations are largely dependent upon technology, which is why the giant hires skilled professionals for numerous technical roles like software developers, etc. Naturally, the Amazon interview questions for such profiles will consist of coding/ tech questions. There are four blocks of interview rounds for tech profiles specifically software development engineer (SDE). These are classified on the basis of interviewers i.e. hiring manager, bar raiser, interviewers, and shadows. It is important to note that each of these interviewers will test you on both leadership competencies as well as technical competencies.
We have already elaborated on the leadership and behavioral questions. In this section, we will have a look at some common Amazon interview questions and answers that test tech competencies.
Q1. Give examples of greedy algorithms.
Graph - Map Coloring | Graph - Vertex Cover |
Kruskal's Minimal Spanning Tree Algorithm | Prim's Minimal Spanning Tree Algorithm |
Travelling Salesman Problem | Knapsack Problem |
Job Scheduling Problem | Dijkstra's Minimal Spanning Tree Algorithm |
Q2. Define checked exception.
A checked exception also referred to as a compile-time exception, is a fault in the code that can be recovered, as against unchecked exceptions, something wrong in the code that cannot be recovered.
Q3. What is a data structure and its types?
A data structure refers to the relationship between the multiple data and the technique to manage, organize, and efficiently sort said data. There are two types- linear and non-linear data structure. Linear data structures are ones that store data linearly or sequentially. Some examples are queues, stacks, arrays, etc. Non-linear data structures are more complex structures that are connected to the multiple, previous, and next elements. Its examples are tree, graph, and hashmap.
Q4. Explain operator overloading.
Also referred to as operator ad hoc polymorphism, operator overloading is when different operators have different implementations in reference to the arguments.
Q5. What does tree traversal mean?
Tree traversal refers to the process of visiting each tree node in the data structure, just once. This can be done in three ways i.e. inorder, preorder, and postorder.
Q6. What is the meaning of a database?
The database is the term used to refer to a collection of organized data stored in a computer system that be accessed electronically.
The Amazon interview questions listed above were some of the most basic tech-related questions. Now let's have a look at some coding questions that will help you prepare for your interview.
Q7. How do you find the missing number in the array?
package com.devglan; import java.util.Arrays; public class FindMissingNumber { public static int calculateSumOfNNumbers(int n){ return (n * (n + 1))/2; } public static int calculateSum(int [] array){ return Arrays.stream(array).sum(); } public static void main(String [] args){ int n = 9; int[] numbers = {1, 2, 4, 9, 7, 8, 5, 6}; int nNumberSum = FindMissingNumber.calculateSumOfNNumbers(n); int sumOfArray = calculateSum(numbers); int missingNumber = nNumberSum - sumOfArray; System.out.println(String.format(
Q8. Determine whether the sum of two integers is equal to the value that is given.
import java.util.*; public class Exercise52 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(
Q9. Write how to merge two sorted linked lists.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { } };
Q10. How to copy the linked list with an arbitrary pointer?
/* // Definition for a Node. class Node { int val; Node next; Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; } } */ class Solution { public Node copyRandomList(Node head) { } }
Q11. Level Order Traversal of Binary Tree.
Q12. How would you determine whether the binary tree is a binary search tree or not?
Ym9vbGVhbiBpc0JTVChOb2RlIG5vZGUpCgp7CgppZiAobm9kZSA9PSBudWxsKQoKcmV0dXJuIHRydWU7CgovKiBGYWxzZSBpZiBsZWZ0IGlzID4gdGhhbiBub2RlICovCgppZiAobm9kZS5sZWZ0ICE9IG51bGwgJiYgbm9kZS5sZWZ0LmRhdGEgPiBub2RlLmRhdGEpCgpyZXR1cm4gZmFsc2U7CgovKiBGYWxzZSBpZiByaWdodCBpcyA8IHRoYW4gbm9kZSAqLwoKaWYgKG5vZGUucmlnaHQgIT0gbnVsbCAmJiBub2RlLnJpZ2h0LmRhdGEgPCBub2RlLmRhdGEpCgpyZXR1cm4gZmFsc2U7CgovKiBGYWxzZSBpZiwgcmVjdXJzaXZlbHksIHRoZSBsZWZ0IG9yIHJpZ2h0IGlzIG5vdCBhIEJTVCAqLwoKaWYgKCFpc0JTVChub2RlLmxlZnQpIHx8ICFpc0JTVChub2RlLnJpZ2h0KSkKCnJldHVybiBmYWxzZTsKCi8qIFBhc3NpbmcgYWxsIHRoYXQsIGl0J3MgYSBCU1QgKi8KCnJldHVybiB0cnVlOwoKfQ==
Q13. Do string segmentation.
Y2xhc3MgU29sdXRpb24gewpwdWJsaWMgYm9vbGVhbiB3b3JkQnJlYWsoU3RyaW5nIHMsIExpc3Q8U3RyaW5nPiB3b3JkRGljdCkgewoKfQp9
Q14. How many ways can you make a change with coins and a total amount?
import java.util.*; class GFG { // Returns the count of ways we can // sum S[0...m-1] coins to get sum n static int count(int S[], int m, int n) { // If n is 0 then there is 1 solution // (do not include any coin) if (n == 0) return 1; // If n is less than 0 then no // solution exists if (n < 0) return 0; // If there are no coins and n // is greater than 0, then no // solution exist if (m <= 0) return 0; // count is sum of solutions (i) // including S[m-1] (ii) excluding S[m-1] return count(S, m - 1, n) + count(S, m, n - S[m - 1]); } // Driver code public static void main(String args[]) { int arr[] = { 1, 2, 3 }; int m = arr.length; System.out.println(count(arr, m, 4)); } }
Q15. Find Kth permutation.
cHVibGljIGNsYXNzIFNvbHV0aW9uIHsKIHByaXZhdGUgc3RhdGljIGZpbmFsIGludFtdIEZBQ1QgPSB7IC8vIDQ3OTAwMTYwMCA8IDIxNDc0ODM2NDcgPCA2MjI3MDIwODAwCiAxLCAxLCAyLCA2LCAyNCwgMTIwLCA3MjAsIDUwNDAsIDQwMzIwLCAzNjI4ODAsIDM2Mjg4MDAsIDM5OTE2ODAwLCA0NzkwMDE2MDAKIH07CiBwdWJsaWMgTGlzdDxMaXN0PEludGVnZXI+PiBwZXJtdXRlKGludFtdIG51bXMpIHsKIEFycmF5cy5zb3J0KG51bXMpOwogTGlzdDxMaXN0PEludGVnZXI+PiByZXN1bHQgPSBuZXcgQXJyYXlMaXN0PD4obnVtcy5sZW5ndGgpOwogZm9yIChpbnQgayA9IDA7IGsgPCBGQUNUW251bXMubGVuZ3RoXTsgKytrKSB7CiByZXN1bHQuYWRkKHBlcm11dGF0aW9uKG51bXMsIGspKTsKIH0KIHJldHVybiByZXN1bHQ7CiB9CiBMaXN0PEludGVnZXI+IHBlcm11dGF0aW9uKGludFtdIG51bXMsIGludCBrKSB7CiAvLyBrICU9IEZBQ1RbbnVtcy5sZW5ndGhdOyAvLyBpbiBjYXNlIHlvdSB3YW50IHRvIHVzZSBpdCBlbHNld2hlcmUKIExpc3Q8SW50ZWdlcj4gc291cmNlID0gdG9MaXN0KG51bXMpOwogTGlzdDxJbnRlZ2VyPiByZXN1bHQgPSBuZXcgQXJyYXlMaXN0KG51bXMubGVuZ3RoKTsKIHdoaWxlICghc291cmNlLmlzRW1wdHkoKSkgewogaW50IGYgPSBGQUNUW3NvdXJjZS5zaXplKCkgLSAxXTsKIHJlc3VsdC5hZGQoc291cmNlLnJlbW92ZShrIC8gZikpOwogayAlPSBmOwogfQogcmV0dXJuIHJlc3VsdDsKIH0KIExpc3Q8SW50ZWdlcj4gdG9MaXN0KGludFtdIG51bXMpIHsKIExpc3Q8SW50ZWdlcj4gcmVzdWx0ID0gbmV3IExpbmtlZExpc3Q8PigpOwogZm9yIChpbnQgbnVtIDogbnVtcykgewogcmVzdWx0LmFkZChudW0pOwogfQogcmV0dXJuIHJlc3VsdDsKIH0KfQ==
When you are interviewing for an SDE or any other tech role, will be asked a variety of tech Amazon interview questions which will vary in terms of difficulty. The questions above were beginner level but you can expect the question to be a little more complex as rounds progress.
Another important thing to note here is that many candidates do not prepare well for behavioral questions and are too focused on tech ones. But since every interviewer in a block is allotted both principles and tech competencies, you must be well-versed in all segments. Now let's have a look at a few coding questions of intermediate difficulty level.
Q16. Write the code to find all of the subsets of a set of integers that is given.
import java.io.IOException; class Main { // Print all subsets of given set[] static void printSubsets(char set[]) { int n = set.length; // Run a loop for printing all 2^n // subsets one by one for (int i = 0; i < (1<<n); i++) { System.out.print(
Q17. How to print balanced brace combinations?
import java.io.IOException; class Main { // Print all subsets of given set[] static void printSubsets(char set[]) { int n = set.length; // Run a loop for printing all 2^n // subsets one by one for (int i = 0; i < (1<<n); i++) { System.out.print(
Q18. Give the code to clone a Directed Graph.
LyoKLy8gRGVmaW5pdGlvbiBmb3IgYSBOb2RlLgpjbGFzcyBOb2RlIHsKcHVibGljIGludCB2YWw7CnB1YmxpYyBMaXN0PE5vZGU+IG5laWdoYm9yczsKcHVibGljIE5vZGUoKSB7CnZhbCA9IDA7Cm5laWdoYm9ycyA9IG5ldyBBcnJheUxpc3Q8Tm9kZT4oKTsKfQpwdWJsaWMgTm9kZShpbnQgX3ZhbCkgewp2YWwgPSBfdmFsOwpuZWlnaGJvcnMgPSBuZXcgQXJyYXlMaXN0PE5vZGU+KCk7Cn0KcHVibGljIE5vZGUoaW50IF92YWwsIEFycmF5TGlzdDxOb2RlPiBfbmVpZ2hib3JzKSB7CnZhbCA9IF92YWw7Cm5laWdoYm9ycyA9IF9uZWlnaGJvcnM7Cn0KfQoqLwoKY2xhc3MgU29sdXRpb24gewpwdWJsaWMgTm9kZSBjbG9uZUdyYXBoKE5vZGUgbm9kZSkgewoKfQp9
Q19. How to find the Low/ High Index?
Y2xhc3MgU29sdXRpb24gewpwdWJsaWMgaW50W10gc2VhcmNoUmFuZ2UoaW50W10gbnVtcywgaW50IHRhcmdldCkgewoKfQp9
Q20. Write the code to search Rotated Array.
Y2xhc3MgU29sdXRpb24gewpwdWJsaWMgaW50IHNlYXJjaChpbnRbXSBudW1zLCBpbnQgdGFyZ2V0KSB7Cgp9Cn0=
Q21. What is the method to find the K largest elements from an array?
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgppbXBvcnQgamF2YS51dGlsLkNvbGxlY3Rpb25zOwoKaW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CgpjbGFzcyBHRkcgewoKcHVibGljIHN0YXRpYyB2b2lkIGtMYXJnZXN0KEludGVnZXJbXSBhcnIsIGludCBrKQoKewoKLy8gU29ydCB0aGUgZ2l2ZW4gYXJyYXkgYXJyIGluIHJldmVyc2Ugb3JkZXIKCi8vIFRoaXMgbWV0aG9kIGRvZXNuJ3Qgd29yayB3aXRoIHByaW1pdGl2ZSBkYXRhCgovLyB0eXBlcy4gU28sIGluc3RlYWQgb2YgaW50LCBJbnRlZ2VyIHR5cGUKCi8vIGFycmF5IHdpbGwgYmUgdXNlZAoKQXJyYXlzLnNvcnQoYXJyLCBDb2xsZWN0aW9ucy5yZXZlcnNlT3JkZXIo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MF07CgovLyBGaWxsIHRhYmxlcyBUMVtdIGFuZCBUMltdIHVzaW5nCgovLyB0aGUgYWJvdmUgZ2l2ZW4gcmVjdXJzaXZlIHJlbGF0aW9ucwoKZm9yIChpID0gMTsgaSA8IE5VTV9TVEFUSU9OOyArK2kpCgp7CgpUMVtpXSA9IG1pbihUMVtpIC0gMV0gKyBhWzBdW2ldLAoKVDJbaSAtIDFdICsgdFsxXVtpXSArIGFbMF1baV0pOwoKVDJbaV0gPSBtaW4oVDJbaSAtIDFdICsgYVsxXVtpXSwKClQxW2kgLSAxXSArIHRbMF1baV0gKyBhWzFdW2ldKTsKCn0KCi8vIENvbnNpZGVyIGV4aXQgdGltZXMgYW5kIHJldHVybiBtaW5pbXVtCgpyZXR1cm4gbWluKFQxW05VTV9TVEFUSU9OLTFdICsgeFswXSwKClQyW05VTV9TVEFUSU9OLTFdICsgeFsxXSk7Cgp9CgovLyBEcml2ZXIgY29kZQoKcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpCgp7CgppbnQgYVtdW10gPSB7ezQsIDUsIDMsIDJ9LAoKezIsIDEwLCAxLCA0fX07CgppbnQgdFtdW10gPSB7ezAsIDcsIDQsIDV9LAoKezAsIDksIDIsIDh9fTsKCmludCBlW10gPSB7MTAsIDEyfSwgeFtdID0gezE4LCA3fTsKClN5c3RlbS5vdXQucHJpbnRsbihjYXJBc3NlbWJseShhLCB0LCBlLCB4KSk7Cgp9Cgp9
In the later rounds of the tech interview section, interviewers generally ask a single Amazon interview question that you must solve quickly, leaving them time for further discussion. Once the coding question is done, it is continued by a detailed discussion on previous work experience, internships, or projects and consists of just behavioral interview questions. The difficulty level of this Amazon interview question will be medium to high. Let's have a look at some such questions.
Q28. What is the code to sort an integer array or an array of characters?
import java.util.Arrays; class GFG { public static void main(String args[]) { int[] arr = { 5, -2, 23, 7, 87, -42, 509 }; System.out.println(
Q29. How do you rotate an input array by K?
import java.util.*; import java.lang.*; import java.io.*; class Array_Rotation { // Function to rightRotate array static void RightRotate(int a[], int n, int k) { // If rotation is greater // than size of array k=k%n; for(int i = 0; i < n; i++) { if(i<k) { // Printing rightmost // kth elements System.out.print(a[n + i - k] +
Q30. Design a Game of snakes using OOPS analysis and design techniques.
cHVibGljIGNsYXNzIENlbGwgewoKcHJpdmF0ZSBmaW5hbCBpbnQgcm93LCBjb2w7Cgpwcml2YXRlIENlbGxUeXBlIGNlbGxUeXBlOwoKcHVibGljIENlbGwoaW50IHJvdywgaW50IGNvbCkKCnsKCnRoaXMucm93ID0gcm93OwoKdGhpcy5jb2wgPSBjb2w7Cgp9CgpwdWJsaWMgQ2VsbFR5cGUgZ2V0Q2VsbFR5cGUoKQoKewoKcmV0dXJuIGNlbGxUeXBlOwoKfQoKcHVibGljIHZvaWQgc2V0Q2VsbFR5cGUoQ2VsbFR5cGUgY2VsbFR5cGUpCgp7Cgp0aGlzLmNlbGxUeXBlID0gY2VsbFR5cGU7Cgp9CgpwdWJsaWMgaW50IGdldFJvdygpCgp7CgpyZXR1cm4gcm93OwoKfQoKcHVibGljIGludCBnZXRDb2woKQoKewoKcmV0dXJuIGNvbDsKCn0KCn0=
Q31. Write the code to print all permutations of a string that is given using recursion.
ICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIEZ1bmN0aW9uIHRvIHByaW50IHBlcm11dGF0aW9ucyBvZiBzdHJpbmcKCi8vIFRoaXMgZnVuY3Rpb24gdGFrZXMgdGhyZWUgcGFyYW1ldGVyczoKCi8vIDEuIFN0cmluZwoKLy8gMi4gU3RhcnRpbmcgaW5kZXggb2YgdGhlIHN0cmluZwoKLy8gMy4gRW5kaW5nIGluZGV4IG9mIHRoZSBzdHJpbmcuCgp2b2lkIHBlcm11dGUoc3RyaW5nIGEsIGludCBsLCBpbnQgcikKCnsKCi8vIEJhc2UgY2FzZQoKaWYgKGwgPT0gcikKCmNvdXQ8PGE8PGVuZGw7Cgpl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IGNvdW50U2V0Qml0cyB7CgovKiBGdW5jdGlvbiB0byBnZXQgbm8gb2Ygc2V0CgpiaXRzIGluIGJpbmFyeSByZXByZXNlbnRhdGlvbgoKb2YgcG9zaXRpdmUgaW50ZWdlciBuICovCgpzdGF0aWMgaW50IGNvdW50U2V0Qml0cyhpbnQgbikKCnsKCmludCBjb3VudCA9IDA7Cgp3aGlsZSAobiA+IDApIHsKCmNvdW50ICs9IG4gJiAxOwoKbiA+Pj0gMTsKCn0KCnJldHVybiBjb3VudDsKCn0KCi8vIGRyaXZlciBwcm9ncmFtCgpwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcgYXJnc1tdKQoKewoKaW50IGkgPSA5OwoKU3lzdGVtLm91dC5wcmludGxuKGNvdW50U2V0Qml0cyhpKSk7Cgp9Cgp9
Q37. How do you detect a loop in a singly linked list?
import java.util.*; public class LinkedList { static Node head; // head of list /* Linked list Node*/ static class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Inserts a new Node at front of the list. */ static public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } // Returns true if there is a loop in linked // list else returns false. static boolean detectLoop(Node h) { HashSet<Node> s = new HashSet<Node>(); while (h != null) { // If we have already has this node // in hashmap it means their is a cycle // (Because you we encountering the // node second time). if (s.contains(h)) return true; // If we are seeing the node for // the first time, insert it in hash s.add(h); h = h.next; } return false; } /* Driver program to test above function */ public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.push(20); llist.push(4); llist.push(15); llist.push(10); /*Create loop for testing */ llist.head.next.next.next.next = llist.head; if (detectLoop(head)) System.out.println(
Q38. Find the smallest window in a string consisting of all the characters from another string
Ly8gSmF2YSBwcm9ncmFtIHRvIGZpbmQgc21hbGxlc3QKCi8vIHdpbmRvdyBjb250YWluaW5nCgovLyBhbGwgY2hhcmFjdGVycyBvZiBhIHBhdHRl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gc3RhcnRpbmcKCi8vIGZyb20gc3RhcnRfaW5kZXgKCi8vIGFuZCBsZW5ndGggbWluX2xlbgoKcmV0dXJuIHN0ci5zdWJzdHJpbmcoc3RhcnRfaW5kZXgsCgpzdGFydF9pbmRleCArIG1pbl9sZW4pOwoKfQoKLy8gRHJpdmVyIE1ldGhvZAoKcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykKCnsKClN0cmluZyBzdHIgPSAidGhpcyBpcyBhIHRlc3Qgc3RyaW5nIjsKClN0cmluZyBwYXQgPSAidGlzdCI7CgpTeXN0ZW0ub3V0LnByaW50KCJTbWFsbGVzdCB3aW5kb3cgaXMgOlxuICIKCisgZmluZFN1YlN0cmluZyhzdHIsIHBhdCkpOwoKfQoKfQ==
Q39. What is the minimum number of swaps required for arranging pairs?
Y2xhc3MgR0ZHIHsKCi8vIFRoaXMgZnVuY3Rpb24gdXBkYXRlcyBpbmRleGVzCgovLyBvZiBlbGVtZW50cyAnYScgYW5kICdiJwoKc3RhdGljIHZvaWQgdXBkYXRlaW5kZXgoaW50IGluZGV4W10sIGludCBhLAoKaW50IGFpLCBpbnQgYiwgaW50IGJpKQoKewoKaW5kZXhbYV0gPSBhaTsKCmluZGV4W2JdID0gYmk7Cgp9CgovLyBUaGlzIGZ1bmN0aW9uIHJldHVybnMgbWluaW11bSBudW1iZXIKCi8vIG9mIHN3YXBzIHJlcXVpcmVkIHRvIGFycmFuZ2UKCi8vIGFsbCBlbGVtZW50cyBvZiBhcnJbaS4ubl0gYmVjb21lIGFycmFuZ2VkCgpzdGF0aWMgaW50IG1pblN3YXBzVXRpbChpbnQgYXJyW10sIGludCBwYWlyc1tdLAoKaW50IGluZGV4W10sIGludCBpLCBpbnQgbikKCnsKCi8vIElmIGFsbCBwYWlycyBwcm9jZXNzZWQgc28KCi8vIG5vIHN3YXBwaW5nIG5lZWRlZCByZXR1cm4gMAoKaWYgKGkgPiBuKQoKcmV0dXJuIDA7CgovLyBJZiBj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ZpYihzICsgMSk7Cgp9CgovKiBEcml2ZXIgcHJvZ3JhbSB0byB0ZXN0IGFib3ZlIGZ1bmN0aW9uICovCgpwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcgYXJnc1tdKQoKewoKaW50IHMgPSA0OwoKU3lzdGVtLm91dC5wcmludGxuKCJOdW1iZXIgb2Ygd2F5cyA9ICIgKyBjb3VudFdheXMocykpOwoKfQoKfQ==
Q42. Write the function to show Maximum Subarray Problem.
cHVibGljIGludCBtYXhTdWJBcnJheShpbnRbXSBudW1zKSB7CiBpbnQgbiA9IG51bXMubGVuZ3RoOwogaW50IG1heGltdW1TdWJBcnJheVN1bSA9IEludGVnZXIuTUlOX1ZBTFVFOwogaW50IHN0YXJ0ID0gMDsKIGludCBlbmQgPSAwOwogZm9yIChpbnQgbGVmdCA9IDA7IGxlZnQgPCBuOyBsZWZ0KyspIHsKIGludCBydW5uaW5nV2luZG93U3VtID0gMDsKIGZvciAoaW50IHJpZ2h0ID0gbGVmdDsgcmlnaHQgPCBuOyByaWdodCsrKSB7CiBydW5uaW5nV2luZG93U3VtICs9IG51bXNbcmlnaHRdOwogaWYgKHJ1bm5pbmdXaW5kb3dTdW0gPiBtYXhpbXVtU3ViQXJyYXlTdW0pIHsKIG1heGltdW1TdWJBcnJheVN1bSA9IHJ1bm5pbmdXaW5kb3dTdW07CiBzdGFydCA9IGxlZnQ7CiBlbmQgPSByaWdodDsKIH0KIH0KIH0KIGxvZ2dlci5pbmZvKCJGb3VuZCBNYXhpbXVtIFN1YmFycmF5IGJldHdlZW4ge30gYW5kIHt9Iiwgc3RhcnQsIGVuZCk7CiByZXR1cm4gbWF4aW11bVN1YkFycmF5U3VtOwp9
Q43. Give the code that shows Palindrome Partitioning.
Ly8gUGFsaW5kcm9tZSBQYXJ0aXRpb25pbmdQcm9ibGVtCgpwdWJsaWMgY2xhc3MgR0ZHCgp7CgpzdGF0aWMgYm9vbGVhbiBpc1BhbGluZHJvbWUoU3RyaW5nIHN0cmluZywgaW50IGksIGludCBqKQoKewoKd2hpbGUoaSA8IGopCgp7CgppZihzdHJpbmcuY2hhckF0KGkpICE9IHN0cmluZy5jaGFyQXQoaikpCgpyZXR1cm4gZmFsc2U7CgppKys7CgpqLS07Cgp9CgpyZXR1cm4gdHJ1ZTsKCn0KCnN0YXRpYyBpbnQgbWluUGFsUGFydGlvbihTdHJpbmcgc3RyaW5nLCBpbnQgaSwgaW50IGopCgp7CgppZiggaSA+PSBqIHx8IGlzUGFsaW5kcm9tZShzdHJpbmcsIGksIGopICkKCnJldHVybiAwOwoKaW50IGFucyA9IEludGVnZXIuTUFYX1ZBTFVFLCBjb3VudDsKCmZvcihpbnQgayA9IGk7IGsgPCBqOyBrKyspCgp7Cgpjb3VudCA9IG1pblBhbFBhcnRpb24oc3RyaW5nLCBpLCBrKSArCgptaW5QYWxQYXJ0aW9uKHN0cmluZywgayArIDEsIGopICsgMTsKCmFucyA9IE1hdGgubWluKGFucywgY291bnQpOwoKfQoKcmV0dXJuIGFuczsKCn0KCi8vIERyaXZlciBjb2RlCgpwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcgYXJnc1tdKQoKewoKU3RyaW5nIHN0ciA9ICJhYmFiYmJhYmJhYmFiYSI7CgpTeXN0ZW0ub3V0LnByaW50bG4oIk1pbiBjdXRzIG5lZWRlZCBmb3IgIgoKKyAiUGFsaW5kcm9tZSBQYXJ0aXRpb25pbmcgaXMgIiArIG1pblBhbFBhcnRpb24oc3RyLCAwLCBzdHIubGVuZ3RoKCkgLSAxKSk7Cgp9Cgp9
Amazon is known to hire exceptional talent so your performance bar should be higher than the status quo. Any sort of poor performance while answering Amazon interview questions might hurt your chances of clearing the interview. At Unstop Pro, we have put together a detailed guide on the Amazon hiring drive. We'll cover the following in our interview preparation courses:
- Amazon’s hiring approach
- Application process and timeline
- Hiring Process
- Preparation tips
- Important questions
You can plan ahead and prepare for the position you aspire for. Present yourself with confidence and be open to feedback. #BeUnstoppable
You may also like to read:
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Shahnawaz shaan 8 months ago