Arslan Ahmad

Unlock the Top 20 Coding Questions to Pass Amazon Interview

The 20 coding problems you must know to clear Amazon's interview

Do you want to secure a job at Amazon?

Whether you're a fresh graduate or an experienced software engineer, getting a job here can be a game-changer for your career.

Amazon is not just a tech giant; it's a company known for its innovative projects and dynamic work environment. However, the competition is tough, and the interview process can be daunting.

You need sharp coding skills and smart preparation to clear Amazon’s technical interview—and this is where most candidates fail.

Therefore, in this blog, we'll guide you through the top 20 coding questions that can help you crack the Amazon interview.

By the end of this blog, you’ll have a clear idea of what to expect and how to prepare effectively.

Let’s get started and help you take the first step towards your dream job at Amazon!

Section 1: Understanding Amazon's Interview Process

Amazon, founded by Jeff Bezos in 1994, started as an online bookstore but has grown into a global tech giant. Its influence spans across e-commerce, cloud computing, AI, and more.

Working at Amazon is a dream for many software engineers due to its innovative projects, dynamic work environment, and strong company culture.

The interview process at Amazon is designed to test your problem-solving skills, coding ability, and how well you fit into their culture.

Format of the Interview

The interview process at Amazon typically includes several stages.

First, there's an online assessment, which tests your basic coding skills. If you pass this, you move on to phone interviews, where you’ll solve coding problems live with an interviewer.

Finally, there's the onsite interview, which consists of multiple rounds, including coding, system design, and behavioral questions.

Types of Questions

Amazon focuses on various types of questions to assess different skills:

  • Coding Problems: These test your knowledge of algorithms and data structures.

  • System Design: You may be asked to design a scalable system.

  • Behavioral Questions: These assess your fit with Amazon's leadership principles and how you handle work situations.

What Amazon Looks For

Amazon seeks candidates who can solve problems efficiently and think on their feet. They value clear and logical thinking, the ability to write clean and efficient code, and a strong grasp of data structures and algorithms.

Additionally, they look for cultural fit, which means aligning with their leadership principles like customer obsession, ownership, and bias for action.

Learn about the Amazon system design interview essentials.

Section 2: 20 Coding Problems To Pass Amazon Interview

1. Longest Common Prefix

Problem Statement: Given an array of strings str, return the longest common prefix string amongst an array of strings. If there is no longest common prefix string exists, return an empty string. Example:

  • Input: strs = ["digital","dig","dinner"]
  • Expected Output: "di"
  • Justification: The strings share the initial fragment "di", but not beyond that. "dinner" diverges starting from the third character, making "di" the longest common prefix.

Difficulty Level: Easy

Solution: Longest common prefix

2. Leaf-Similar Trees

Problem Statement: Given the two root nodes root1 and root2 of two different binary trees, return true if both binary trees are leaf-similar trees. Otherwise, return fasle. All leaves of the binary tree in left-to-right order create a leaf sequence. If both trees have same leaf sequence, they are considered as leaf-similar tree.


  • Input: Input: root1 = [5,2,7,1,3,6,8], root2 = [7,5,2,1,3,6,8]
  • Expected Output: True
  • Justification: Both trees have the same leaf sequence [1,3,6,8], making them leaf-similar despite their different structures.
Leaf Tree

Difficulty Level: Easy

Solution: Leaf-Similar Trees

3. Path Crossing

Problem Statement: You are given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing the movement of one unit in North, South, East, and West direction, respectively. You start the journey on the 2D plane from origin (0, 0), and move towards the directions given in path string. Return true, if your path crosses itself at any point, that is, if you ever visit the same point twice. Otherwise, return false.


  • Input: "NESW"
  • Expected Output: false
  • Justification: The path moves north, east, south, and then west, returning to the origin. This forms a loop, crossing at the starting point.

Difficulty Level: Easy

Solution: Path Crossing

4. Relative Sort Array

Problem Statement: You are given two integers arrays arr1 and arr2. All elements of the arr2 are distinct, and present in arr1. Sort the array arr1 such that relative orders of elements of arr1 should be the same as in arr2. If any elements in arr1 not found in arr2 should come at the end, sorted in ascending order.


  • Input: arr1 = [8, 1, 2, 3, 4, 8, 9], arr2 = [4, 3, 2, 1]
  • Expected Output: [4, 3, 2, 1, 8, 8, 9]
  • Justification: The elements in arr1 that are also in arr2 are arranged in the arr2 order first (4, 3, 2, 1), followed by the remaining elements of arr1 in ascending order (8, 8, 9).

Difficulty Level: Easy

Solution: Relative Sort Array

5. Transpose Matrix

Problem Statement: Given a 2D array matrix, return the transpose of the matrix.

In the transpose matrix, the first row of the original matrix becomes the first column, the second row becomes the second column, and so on.

Example: - Input:

  • Expected Output:
[[1,3], [2,4]]
  • Justification: The first row [1,2] becomes the first column [1,3], and the second row [3,4] becomes the second column [2,4].

Difficulty Level: Easy

Solution: Transpose Matrix

6. Find Common Characters

Problem Statement: Given a strings array words, return a character array results containing all characters (including duplicates), which are common in all strings of words. You may return characters in any order.


  • Input: words = ["danday","candya","ahand"]
  • Expected Output: ["a","a","d","n"]
  • Justification: The letters 'a', 'a', 'd', and 'n' are common in all three words, each appearing at least once in every word.

Difficulty Level: Easy

Solution: Find Common Characters

7. Largest Odd Number in String

Problem Statement: Given a large integer num represented as string, return the largest-valued odd integer (represented as a string) that is a non-empty substring of num. If no such number exists, the result should be an empty string.

A substring is defined as a contiguous sequence of characters within a string.


  • Input: num = "238946"
  • Expected Output: "2389"
  • Justification: The largest odd number in the string is "2389".

Difficulty Level: Easy

Solution: Largest Odd Number in String

8. Best Time to Buy and Sell

Problem Statement: You are given an array prices where prices[i] is the price of a given stock on the day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.


  • Input: [8, 6, 5, 2, 1]
  • Expected Output: 0
  • Justification: Prices are continuously dropping, so no profit can be made.

Difficulty Level: Easy

Solution: Best Time to Buy and Sell Stock

9. Maximum Length of Subarray With Positive Product

Problem Statement: Given an integer array nums, find the maximum length of a subarray where the product of all its elements is greater than 0.

A subarray is a sequence of consecutive elements from the original array.


  • Input: nums = [1, -2, 3, 4]
  • Expected Output: 2
  • Justification: The longest subarray with a positive product is [3, 4], with a length of 2.

Difficulty Level: Medium

Solution: Maximum Length of Subarray With Positive Product

10. Count the Number of Good Subarrays

Problem Statement: Given an array of integers nums, and an integer k, find the count of "good" subarrays within nums.

A subarray is considered "good" if it contains at least k pairs of elements (i, j) where i < j and nums[i] == nums[j].

A subarray is a contiguous sequence of elements in the original array.


  • Input: nums = [2, 2, 2, 3, 3], k = 3
  • Expected Output: 3
  • Justification: There are 3 good subarrays that meet the criteria: [2, 2, 2, 3], [2, 2, 2, 3, 3], and [2, 2, 2]. Each of these subarrays has at least 3 pairs of equal elements.

Difficulty Level: Medium

Solution: Count the Number of Good Subarrays

11. Minimum Suffix Flips

Problem Statement: You are given a target binary string of length n. Initially, you also have another string str of length n containing all 0s. You need to make str equal to the target by performing some number of operations.

In one operation, you can take an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Here, Flip means changing '1' to '0' and '0' to '1'.

Return the minimum number of operations required to make string str equal to target.


  • Input: target = "001"
  • Expected Output: 1
  • Justification: Flipping the last character in the str string from 0 to 1 will make the string "001", achieving the goal in one operation.

Difficulty Level: Medium

Solution: Minimum Suffix Flips

12. Make Sum Divisible by P

Problem Statement: Given an array nums containing the positive integers, remove the smallest subarray (maybe empty) from nums such that the sum of the remaining elements is divisible by p. You are not allowed to remove the whole array.

Return the length of the smallest subarray that you need to remove, or -1 if it's not possible.

A subarray is a contiguous sequence of elements in the array.


  • Input: nums = [5, 9, 7, 8], P = 5
  • Expected Output: 1
  • Justification: Removing the subarray [9] results in a new array [5, 7, 8] whose sum is 20, which is divisible by 5.

Difficulty Level: Medium

Solution: Make Sum Divisible by P

13. Rotting Oranges

Problem Statement: You are given an m x n matrix in which each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

At each minute, any fresh orange becomes rotten if it is 4-directionally adjacent to a rotten orange.

Return the minimum number of minutes that should be passed until all the orange gets rotten. If it is impossible, return -1.


  • Input: grid =
[[2,1,1], [1,1,0], [0,1,2]]
  • Expected Output: 2
  • Justification: The rotten oranges spread the rot to all fresh oranges in 2 minutes, successfully spoiling all of them.

Difficulty Level: Medium

Solution: Rotting Oranges

14. Minimum Adjacent Swaps to Make a Valid Array

Problem Statement: Given an integer array nums, return the minimum swaps required to make nums a valid array.

You can swap any two adjacent elements of the array.

An array is a valid array if it meets the following conditions:

  • The largest element is at the rightmost position in the array.
  • The smallest element is at the leftmost position in the array.
  • If there are multiple smallest and largest elements, any of the smallest elements should be at the leftmost position and any of the largest should be at the rightmost position.


  • Input: [3, 1, 2]
  • Expected Output: 2
  • Justification: The first swap moves 3 to the second position by swapping with 1, and the second swap moves 3 to the third position by swapping with 2, positioning the smallest element (1) at the beginning and the largest element (3) at the end.

Difficulty Level: Medium

Solution: Minimum Adjacent Swaps to Make a Valid Array

15. The kth Factor of n

Problem Statement: Given two integers, n and k, return the kth smallest factor of n. If n has fewer factors than k factors, return -1. A factor of a number is an integer that can divide that number without leaving a remainder.


  • Input: n = 12, k = 5
  • Expected Output: 6
  • Justification: The factors of 12 are 1, 2, 3, 4, 6, and 12. The fifth smallest factor is 6.

Difficulty Level: Medium

Solution: The kth Factor of n

16. Count Vowels Permutation

Problem Statement: Given a positive integer n, return the number of strings of length n can be formed by following rules:

  • Each character of the string should be only a lowercase vowel ('a', 'e', 'i', 'o', 'u').
  • The vowel 'a' is only allowed to be followed by the vowel 'e'.
  • The vowel 'e' can be followed by either 'a' or 'i'.
  • The vowel 'i' cannot be followed by another 'i'.
  • The vowel 'o' can be followed by either 'i' or 'u'.
  • The vowel 'u' must be followed by 'a'. Return the answer modulo 10^9 + 7, as it could be very large.


  • Input: n=1
  • Expected Output: 5
  • Justification: There's only one position, and each vowel can occupy it without restrictions, resulting in 5 possible strings: 'a', 'e', 'i', 'o', 'u'.

Difficulty Level: Hard

Solution: Count Vowels Permutation

17. Concatenated Words

Problem Statement: Given a words array containing non-duplicate strings, return all the concatenated words that are in the words array.

A concatenated word is a string that is formed entirely of at least two shorter words (May be same) of the given array.


  • Input: words = ["tree","cat","cattree","dog","catdog"]
  • Expected Output: ["cattree", "catdog"]
  • Justification: 'cattree' is formed by concatenating 'cat' and 'tree'. Similarly, 'catdog' is a combination of 'cat' and 'dog'. Both 'cattree' and 'catdog' consist entirely of shorter words found in the array.

Difficulty Level: Hard

Solution: Concatenated Words

18. Count All Valid Pickup and Delivery Options

Problem Statement: You are given a positive integer n, representing total orders where each order consists of a pickup and a delivery service.

Count all valid pickup/delivery sequences such that delivery(x) is always after of pickup(x).

Return the answer modulo 10^9 + 7.


  • Input: n=1
  • Expected Output: 1
  • Justification: With just one order, there's only one valid sequence: pickup and then delivery. Thus, the expected output is 1.

Difficulty Level: Hard

Solution: Count All Valid Pickup and Delivery Options

19. Minimum Replacements to Sort the Array

Problem Statement: Given a 0-indexed integer array nums, return the minimum number of operations to make an array that is sorted in a non-decreasing order.

In a single operation, you can pick any element of the array and replace it with any two elements that sum to it.

  • For example, consider nums = [4,3,5]. In one operation, you can replace nums[0] with 1 and 3 and convert nums to [1,3,3,5].


  • Input: [4, 3, 2]
  • Expected Output: 4
  • Justification: Replace 4 with two 2s (1 operation), resulting in [2, 2, 3, 2]. Then, to maintain non-decreasing order, replace the first two elements with 1 and 1 (2 operations), and the 3 with 1 and 2 (1 operation), totaling 4 operations.

Difficulty Level: Hard

Solution: Minimum Replacements to Sort the Array

20. Path with Maximum Sum

Problem Statement: Find the path with the maximum sum in a given binary tree. Write a function that returns the maximum sum.

A path can be defined as a sequence of nodes between any two nodes and doesn’t necessarily pass through the root. The path must contain at least one node.


  • Expected Output: 16
  • Justification: The path with the maximum sum is = [4,2,1,3,6]
Pair with Maximum Sum

Difficulty Level: Hard

Solution: Path with Maximum Sum

Find more Amazon interview coding questions with solutions.

Final Tips

  • Stay Calm and Focused: During the interview, stay calm and think clearly. It’s okay to take a moment to gather your thoughts before answering.

  • Communicate Your Thought Process: Explain your reasoning as you solve problems. This shows interviewers your approach to problem-solving.

  • Practice Regularly: Consistency is key. Practice coding problems regularly to keep your skills sharp.

  • Review Basic Concepts: Make sure you have a strong understanding of fundamental concepts in algorithms and data structures.

Check out the guide to master FAANG interviews.


Getting a job at Amazon is a dream for many, but it takes preparation and practice to succeed.

By working through these top 20 coding questions, you'll be better equipped to tackle the challenges of the Amazon interview process.

Practice regularly, review fundamental concepts, and make use of the resources and tips we've shared.

Coding Interview
Coding Interview Questions
Get instant access to all current and upcoming courses through subscription.
billed yearly ($242)
Recommended Course
Join our Newsletter
Read More