Amazon Previous Year Coding Questions
Amazon is a global leader in technology, renowned for its rigorous interview process for Software Development Engineer (SDE) roles, including SDE-1, SDE-2, and other technical positions. Securing a role at Amazon demands strong technical proficiency in data structures and algorithms (DSA), problem-solving skills, and alignment with Amazon’s 14 Leadership Principles. This guide provides a detailed overview of Amazon’s hiring process, a curated list of free preparation resources with direct links, and 50 commonly asked coding interview questions to help you excel in technical rounds.
Amazon’s Hiring Process for SDE Roles
Amazon’s hiring process is structured to evaluate both technical expertise and cultural fit. Below are the typical stages, based on candidate experiences and publicly available information:
1. Online Application
- Description: Candidates apply via Amazon’s job portal or through referrals. Your resume should highlight relevant projects, technical skills (e.g., programming languages, algorithms), and measurable impact.
- Tip: Tailor your resume to reflect Amazon’s Leadership Principles (e.g., Ownership, Invent and Simplify) by quantifying achievements (e.g., “Optimized API response time by 30%”).
2. Online Assessment (OA)
- Description: Typically includes 1-3 coding problems to be solved in 90-120 minutes, often hosted on platforms like HackerRank. Questions focus on DSA topics such as arrays, strings, trees, and graphs. Some OAs include debugging or work simulation tasks.
- Tip: Practice medium-difficulty problems under timed conditions (20-30 minutes per problem). Focus on edge cases and code clarity.
3. Phone Screen (Optional)
- Description: A 45-60 minute virtual interview with one coding problem and behavioral questions. You’ll code in a shared editor while explaining your approach.
- Tip: Practice live coding and verbalizing your thought process. Be ready to discuss time/space complexity and optimize solutions.
4. Onsite/Virtual Loop
- Description: Consists of 4-6 rounds (60 minutes each), including:
- Coding Rounds (2-3): Solve 1-2 medium-to-hard DSA problems per round (e.g., linked lists, dynamic programming).
- System Design Rounds (for SDE-2 and above): Design scalable systems (e.g., distributed cache, URL shortener).
- Behavioral Rounds: Answer questions tied to Amazon’s Leadership Principles (e.g., “Tell me about a time you demonstrated Customer Obsession”).
- Tip: Use the STAR (Situation, Task, Action, Result) method for behavioral questions. For coding, explain your approach clearly and optimize on the fly.
5. Bar Raiser Round
- Description: A unique Amazon round conducted by a trained interviewer to ensure candidates meet Amazon’s high hiring standards. It assesses technical depth and cultural fit, often diving deeper into problem-solving or past experiences.
6. Offer Decision
- Description: The hiring team evaluates performance across all rounds. Offers are extended to candidates who meet the bar.
- Tip: Be patient, as decisions may take a few days to a week. Follow up politely if needed.
Resources
- A to Z Placement Kit
- Amazon SDE Internship / Full-Time Jobs
- Amazon Leadership Principles
- GeeksforGeeks – Amazon Coding Questions
- InterviewBit – Amazon Interview Questions
- DSA Roadmap & Questions
- Aptitude roadmap & resources
- IndiaBix – Amazon Aptitude
- PrepInsta – Aptitude + Reasoning
- System Design Roadmap
- GitHub – System Design Primer
- YouTube – Amazon Interview Experiences
- YouTube – Amazon System Design Prep
- Amazon Leadership Principles (Core Guide)
- InterviewBit – Amazon HR/Behavioral Questions
Arrays
1. Two Sum
Problem Statement: Given an array of integers nums
and an integer target
, return the indices of two numbers such that they add up to target
. Each input has exactly one solution, and you cannot use the same element twice.
Example: Input: nums = [2,7,11,15], target = 9
→ Output: [0,1]
(because nums[0] + nums[1] = 2 + 7 = 9
).
Solution Approach: Use a hash map to store numbers and their indices. For each number, check if target - num
exists in the hash map. Time complexity: O(n), Space complexity: O(n).
2. Maximum Subarray
Problem Statement: Given an array of integers nums
, find the contiguous subarray with the largest sum and return its sum.
Example: Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
→ Output: 6
(subarray [4,-1,2,1]
).
Solution Approach: Use Kadane’s algorithm to track the maximum sum subarray by maintaining a current sum and global maximum. Time complexity: O(n), Space complexity: O(1).
3. Merge Two Sorted Arrays
Problem Statement: Given an array of integers where the first half and second half are sorted, merge them into a single sorted array.
Example: Input: arr = [1,3,5,2,4,6]
→ Output: [1,2,3,4,5,6]
.
Solution Approach: Use a two-pointer technique to merge the sorted halves in-place or into a new array. Time complexity: O(n), Space complexity: O(n) or O(1) for in-place.
4. Find Triplet Sum
Problem Statement: Given an array of n
distinct integers, find a triplet (a, b, c)
such that a + b = c
. Return the triplet in a container.
Example: Input: arr = [1,2,3,4,5]
→ Output: [1,2,3]
(because 1 + 2 = 3
).
Solution Approach: Sort the array, then use two pointers to find pairs summing to each element. Time complexity: O(n²), Space complexity: O(1).
5. Kth Largest Element in an Array
Problem Statement: Given an integer array nums
and an integer k
, find the kth largest element in the array. Assume k
is valid.
Example: Input: nums = [3,2,1,5,6,4], k = 2
→ Output: 5
.
Solution Approach: Use a min-heap of size k
or sort the array and pick the kth element from the end. Time complexity: O(n log k) with heap, O(n log n) with sorting. Space complexity: O(k) or O(1).
6. Product of Array Except Self
Problem Statement: Given an array nums
, return an array where each element is the product of all numbers except the element at that index. Do not use division.
Example: Input: nums = [1,2,3,4]
→ Output: [24,12,8,6]
.
Solution Approach: Compute left and right product arrays and multiply them for each index. Time complexity: O(n), Space complexity: O(1) excluding output array.
7. Rotate Array
Problem Statement: Given an array nums
and an integer k
, rotate the array to the right by k
steps.
Example: Input: nums = [1,2,3,4,5,6,7], k = 3
→ Output: [5,6,7,1,2,3,4]
.
Solution Approach: Reverse the entire array, then reverse the first k
elements and the remaining n-k
elements. Time complexity: O(n), Space complexity: O(1).
8. Find Missing Number
Problem Statement: Given an array nums
containing n
distinct numbers from 0
to n
, find the missing number.
Example: Input: nums = [3,0,1]
→ Output: 2
.
Solution Approach: Use the sum of first n
natural numbers formula or XOR all numbers and indices. Time complexity: O(n), Space complexity: O(1).
9. Subarray Sum Equals K
Problem Statement: Given an array of integers nums
and an integer k
, find the total number of continuous subarrays whose sum equals k
.
Example: Input: nums = [1,1,1], k = 2
→ Output: 2
.
Solution Approach: Use a hash map to store cumulative sums and their frequencies. Time complexity: O(n), Space complexity: O(n).
Strings
10. Longest Palindromic Substring
Problem Statement: Given a string s
, find the longest substring that is a palindrome. Assume the maximum length of the string is 1000.
Example: Input: s = "babad"
→ Output: "bab"
or "aba"
.
Solution Approach: Use the center-expansion technique, treating each character and gap as a potential palindrome center. Time complexity: O(n²), Space complexity: O(1).
11. Check if Strings are Rotations
Problem Statement: Given two strings s1
and s2
, check if s2
is a rotation of s1
.
Example: Input: s1 = "ABCD", s2 = "CDAB"
→ Output: true
.
Solution Approach: Concatenate s1
with itself and check if s2
is a substring. Time complexity: O(n), Space complexity: O(n).
12. Longest Common Prefix
Problem Statement: Given an array of strings strs
, return the longest common prefix among all strings. If none exists, return an empty string.
Example: Input: strs = ["flower","flow","flight"]
→ Output: "fl"
.
Solution Approach: Compare characters vertically or sort and compare first and last strings. Time complexity: O(n * m) where m
is the shortest string length, Space complexity: O(1).
13. Run-Length Encoding
Problem Statement: Given a string, return its run-length encoding.
Example: Input: s = "aaaaabb"
→ Output: "a5b2"
.
Solution Approach: Iterate through the string, counting consecutive characters, and append count and character to the result. Time complexity: O(n), Space complexity: O(n).
14. Valid Parentheses
Problem Statement: Given a string s
containing only (
, )
, {
, }
, [
, ]
, determine if it is valid (all brackets are properly closed).
Example: Input: s = "({[]})"
→ Output: true
.
Solution Approach: Use a stack to match opening and closing brackets. Time complexity: O(n), Space complexity: O(n).
15. Group Anagrams
Problem Statement: Given an array of strings strs
, group all anagrams together. Anagrams are strings with the same characters but in different orders.
Example: Input: strs = ["eat","tea","tan","ate","nat","bat"]
→ Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
.
Solution Approach: Sort each string and use it as a key in a hash map to group anagrams. Time complexity: O(n k log k) where k
is max string length, Space complexity: O(n * k).
16. String to Integer (atoi)
Problem Statement: Implement a function to convert a string to an integer, handling whitespace, signs, and overflow (32-bit integer limits).
Example: Input: s = " -42"
→ Output: -42
.
Solution Approach: Parse the string, handle edge cases like overflow, and return the result. Time complexity: O(n), Space complexity: O(1).
17. Most Common Word
Problem Statement: Given a paragraph and a list of banned words, return the most frequent word that is not banned.
Example: Input: paragraph = "Bob hit a ball, the hit BALL flew far", banned = ["hit"]
→ Output: "ball"
.
Solution Approach: Use a hash map to count word frequencies, ignoring banned words, and return the most frequent. Time complexity: O(n), Space complexity: O(n).
18. Reverse Words in a String
Problem Statement: Given a string s
, reverse the order of words. Words are separated by spaces, and there may be multiple spaces.
Example: Input: s = " hello world "
→ Output: "world hello"
.
Solution Approach: Split the string into words, reverse the list, and join with a single space. Time complexity: O(n), Space complexity: O(n).
Linked Lists
19. Reverse a Linked List
Problem Statement: Given the head of a singly linked list, reverse the list and return the new head.
Example: Input: 1->2->3->4->NULL
→ Output: 4->3->2->1->NULL
.
Solution Approach: Use three pointers (prev, current, next) to iteratively reverse links or use recursion. Time complexity: O(n), Space complexity: O(1) or O(n) for recursion.
20. Detect and Find Loop Size
Problem Statement: Given a singly linked list, detect if it has a loop. If a loop exists, find its size.
Example: Input: 1->2->3->4->2 (loops back to 2)
→ Output: 3
(loop size).
Solution Approach: Use Floyd’s cycle detection (two pointers) to detect the loop, then count nodes in the loop. Time complexity: O(n), Space complexity: O(1).
21. Merge K Sorted Linked Lists
Problem Statement: Given an array of k
sorted linked lists, merge them into a single sorted linked list. Assume 1 ≤ k ≤ 10^4
and total nodes ≤ 10^5
.
Example: Input: [1->4->5, 1->3->4, 2->6]
→ Output: 1->1->2->3->4->4->5->6
.
Solution Approach: Use a min-heap to manage the smallest nodes from each list. Time complexity: O(n * log k), Space complexity: O(k).
22. Add Two Numbers
Problem Statement: Given two non-empty linked lists representing non-negative integers (digits in reverse order), return their sum as a linked list.
Example: Input: l1 = 2->4->3, l2 = 5->6->4
→ Output: 7->0->8
(342 + 465 = 807).
Solution Approach: Iterate through both lists, add digits with carry, and build a new list. Time complexity: O(max(n,m)), Space complexity: O(max(n,m)).
23. Remove Nth Node From End of List
Problem Statement: Given a linked list and an integer n
, remove the nth node from the end and return the head.
Example: Input: head = 1->2->3->4->5, n = 2
→ Output: 1->2->3->5
.
Solution Approach: Use two pointers with a gap of n
to find and remove the target node. Time complexity: O(n), Space complexity: O(1).
24. Intersection of Two Linked Lists
Problem Statement: Given the heads of two singly linked lists, find the node where they intersect (if any).
Example: Input: listA = 1->2->3->4, listB = 6->4, intersect at 4
→ Output: 4
.
Solution Approach: Use two pointers, switching lists when one reaches the end, to find the intersection. Time complexity: O(n+m), Space complexity: O(1).
25. Swap Nodes in Pairs
Problem Statement: Given a linked list, swap every two adjacent nodes and return the head.
Example: Input: head = 1->2->3->4
→ Output: 2->1->4->3
.
Solution Approach: Iteratively or recursively swap pairs of nodes. Time complexity: O(n), Space complexity: O(1) or O(n) for recursion.
26. Palindrome Linked List
Problem Statement: Given the head of a singly linked list, determine if it is a palindrome.
Example: Input: head = 1->2->2->1
→ Output: true
.
Solution Approach: Find the middle using two pointers, reverse the second half, and compare. Time complexity: O(n), Space complexity: O(1).
Trees
27. Invert Binary Tree
Problem Statement: Given the root of a binary tree, invert the tree (swap left and right children of each node) and return its root.
Example: Input: root = [4,2,7,1,3,6,9]
→ Output: [4,7,2,9,6,3,1]
.
Solution Approach: Recursively swap left and right subtrees or use BFS/DFS. Time complexity: O(n), Space complexity: O(h) where h
is tree height.
28. Maximum Path Sum in Binary Tree
Problem Statement: Find the maximum sum of a path in a binary tree. The path can be between any two nodes and doesn’t need to pass through the root.
Example: Input: root = [1,2,3]
→ Output: 6
(path 2->1->3
).
Solution Approach: Use recursion to compute the maximum path sum, tracking the maximum contribution from each subtree. Time complexity: O(n), Space complexity: O(h).
29. Check if Binary Tree is BST
Problem Statement: Given a binary tree, determine if it is a valid Binary Search Tree (BST).
Example: Input: root = [2,1,3]
→ Output: true
.
Solution Approach: Perform an inorder traversal or validate each node’s value within a range. Time complexity: O(n), Space complexity: O(h).
30. Lowest Common Ancestor of a Binary Tree
Problem Statement: Given a binary tree and two nodes p
and q
, find their lowest common ancestor (LCA).
Example: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
→ Output: 3
.
Solution Approach: Use DFS to find paths to p
and q
, and return the first common node. Time complexity: O(n), Space complexity: O(h).
31. Binary Tree Level Order Traversal
Problem Statement: Given a binary tree, return the level-order traversal of its nodes’ values (left to right, level by level).
Example: Input: root = [3,9,20,null,null,15,7]
→ Output: [[3],[9,20],[15,7]]
.
Solution Approach: Use BFS with a queue to process nodes level by level. Time complexity: O(n), Space complexity: O(w) where w
is max width.
32. Validate Binary Search Tree
Problem Statement: Given the root of a binary tree, determine if it is a valid Binary Search Tree (BST).
Example: Input: root = [5,1,4,null,null,3,6]
→ Output: false
(4’s subtree is invalid).
Solution Approach: Use DFS with range validation for each node. Time complexity: O(n), Space complexity: O(h).
33. Serialize and Deserialize Binary Tree
Problem Statement: Design an algorithm to serialize a binary tree to a string and deserialize it back to the original tree.
Example: Input: root = [1,2,3,null,null,4,5]
→ Output: same tree after serialization/deserialization.
Solution Approach: Use preorder traversal for serialization and reconstruct using a queue. Time complexity: O(n), Space complexity: O(n).
34. Path Sum
Problem Statement: Given a binary tree and a target sum, determine if there exists a root-to-leaf path with that sum.
Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], target = 22
→ Output: true
.
Solution Approach: Use DFS to track the running sum and check leaf nodes. Time complexity: O(n), Space complexity: O(h).
35. Longest Path with Same Value
Problem Statement: Given a binary tree, find the length of the longest path where each node has the same value.
Example: Input: root = [5,5,5,1,1,5]
→ Output: 3
(path 5->5->5
).
Solution Approach: Use DFS to track the longest univalue path through each node. Time complexity: O(n), Space complexity: O(h).
Dynamic Programming
36. Climbing Stairs
Problem Statement: Given a staircase with n
steps, you can climb 1 or 2 steps at a time. Find the number of distinct ways to reach the top.
Example: Input: n = 3
→ Output: 3
(ways: [1,1,1], [1,2], [2,1]
).
Solution Approach: Use dynamic programming or Fibonacci-like recursion with memoization. Time complexity: O(n), Space complexity: O(1) or O(n).
37. House Robber
Problem Statement: Given an array nums
representing money in houses, find the maximum amount you can rob without robbing adjacent houses.
Example: Input: nums = [2,7,9,3,1]
→ Output: 12
(rob houses 2, 9, 1).
Solution Approach: Use DP to track max money with/without robbing the current house. Time complexity: O(n), Space complexity: O(1).
38. Word Break
Problem Statement: Given a string s
and a dictionary of words, determine if s
can be segmented into a space-separated sequence of dictionary words.
Example: Input: s = "leetcode", wordDict = ["leet","code"]
→ Output: true
.
Solution Approach: Use DP to check if prefixes can be formed from the dictionary. Time complexity: O(n²), Space complexity: O(n).
39. Maximum Product Subarray
Problem Statement: Given an array of integers nums
, find the contiguous subarray with the largest product.
Example: Input: nums = [2,3,-2,4]
→ Output: 6
(subarray [2,3]
).
Solution Approach: Track max and min products at each step to handle negative numbers. Time complexity: O(n), Space complexity: O(1).
40. Longest Increasing Subsequence
Problem Statement: Given an array of integers nums
, find the length of the longest strictly increasing subsequence.
Example: Input: nums = [10,9,2,5,3,7,101,18]
→ Output: 4
(subsequence [2,3,7,101]
).
Solution Approach: Use DP or binary search to build the subsequence. Time complexity: O(n log n) with binary search, Space complexity: O(n).
41. Coin Change
Problem Statement: Given an array of coin denominations and a target amount, find the minimum number of coins needed to make the amount.
Example: Input: coins = [1,2,5], amount = 11
→ Output: 3
(5+5+1).
Solution Approach: Use DP to compute min coins for each amount up to target. Time complexity: O(amount * n), Space complexity: O(amount).
Graphs
42. Minimum Swaps to Sort Array
Problem Statement: Given an array of n
distinct elements, find the minimum number of swaps required to sort the array in increasing order.
Example: Input: arr = [4,3,1,2]
→ Output: 3
.
Solution Approach: Build a graph where edges represent swaps needed to place elements in sorted positions, then count cycles. Time complexity: O(n log n), Space complexity: O(n).
43. Gas Station Problem
Problem Statement: Given two arrays A
(gas at each station) and B
(cost to travel to next station), find the starting station to complete a circular tour. Assume N
stations and an unlimited gas tank.
Example: Input: A = [1,2,3], B = [2,3,1]
→ Output: 2
.
Solution Approach: Track total gas and deficit to find a valid starting point. Time complexity: O(n), Space complexity: O(1).
44. Course Schedule
Problem Statement: Given n
courses and prerequisites as pairs [a,b]
(take b
before a
), determine if you can finish all courses.
Example: Input: n = 2, prerequisites = [[1,0]]
→ Output: true
.
Solution Approach: Use topological sort (DFS or BFS) to detect cycles in the graph. Time complexity: O(V+E), Space complexity: O(V+E).
45. Number of Islands
Problem Statement: Given a 2D grid of 1
s (land) and 0
s (water), count the number of islands (connected 1
s).
Example: Input: grid = [["1","1","0"],["1","0","0"],["0","0","1"]]
→ Output: 2
.
Solution Approach: Use DFS or BFS to mark connected components as visited. Time complexity: O(m*n), Space complexity: O(m*n).
46. Clone Graph
Problem Statement: Given a reference to a node in a connected undirected graph, return a deep copy of the graph.
Example: Input: node = [[2,4],[1,3],[2,4],[1,3]]
→ Output: copied graph.
Solution Approach: Use DFS or BFS with a hash map to track copied nodes. Time complexity: O(V+E), Space complexity: O(V).
Heaps
47. Top K Frequent Elements
Problem Statement: Given an array nums
and an integer k
, return the k
most frequent elements.
Example: Input: nums = [1,1,1,2,2,3], k = 2
→ Output: [1,2]
.
Solution Approach: Use a hash map to count frequencies, then a min-heap to get top k
. Time complexity: O(n log k), Space complexity: O(n).
48. K Closest Points to Origin
Problem Statement: Given an array of points [x,y]
and an integer k
, return the k
points closest to the origin (0,0) based on Euclidean distance.
Example: Input: points = [[1,3],[-2,2]], k = 1
→ Output: [-2,2]
.
Solution Approach: Use a max-heap of size k
to track points by distance. Time complexity: O(n log k), Space complexity: O(k).
Hashing
49. Count Distinct Elements in Matrix
Problem Statement: Given an NxN
matrix M
, find the count of distinct elements common to all rows.
Example: Input: M = [[1,2,3],[2,3,4],[2,3,5]]
→ Output: 2
(elements 2,3
).
Solution Approach: Use a hash map to track elements in the first row, then update counts for subsequent rows. Time complexity: O(N²), Space complexity: O(N).
Miscellaneous
50. LRU Cache Implementation
Problem Statement: Implement a Least Recently Used (LRU) cache with get
and put
operations. The cache should support O(1) time complexity for both operations.
Example: put(1,1), put(2,2), get(1) → 1, put(3,3)
(evicts key 2
).
Solution Approach: Use a hash map and a doubly linked list to track key-value pairs and their order. Time complexity: O(1), Space complexity: O(capacity).
Preparation Tips
- Practice Platforms: Use LeetCode (Amazon-tagged problems), GeeksforGeeks, HackerRank, or InterviewBit. Aim for 100+ problems, focusing on medium and hard difficulty.
- Time Management: Solve online assessment problems in 20-30 minutes. Onsite rounds include 4-6 interviews (coding + system design).
- Code Quality: Write clean code, handle edge cases, and explain time/space complexity.
- Leadership Principles: Align solutions with Amazon’s principles (e.g., Customer Obsession, Ownership).
- Mock Interviews: Practice with Interviewing.io or peers to simulate Amazon’s coding rounds.
Join Telegram group for any doubts & discussions!