SDE Tech Prep Resource
SDE Tech Prep Resource
Start by asking clarifying questions – make sure you understand the problem by gathering
the requirements and discussing use cases
Don't worry; this problem can be medium - hard complexity. You’ll have 30-35 minutes to
work on it.
Your priorities are:
o Correctness - Code should be syntactically/logically correct.
o Code must be simple – easy to read, easy to test, maintain, debug, and enhance
o No Brute Force or Pseudocode Solution, you must come up with the best optimal
solution (final goal). In case you start with Brute Force or pseudocode make sure
you are optimizing the solution by the end of the coding round. If you are only
able to come up with a solution that requires Brute Force - code your solution
because a Brute Force Solution is better than no solution at all.
o Verify/Validate your code. Consider all the edge cases - as you identify edge cases
ask your interviewer if the edge case needs to be addressed - this can save you
code and brain power
You are being evaluated based on your ability to problem solve, write maintainable and
scalable code and how close to production ready your solution is
It’s ok to ask questions if you are stuck, listen to the interviewer’s hints when given.
Be ready to explain your thought process before, during and after the coding portion of
the interview
When you are working to solve technical questions it is best to have a process to work through –
here is a methodology that many candidates have found successful:
1. Clarify the Question - ask clarifying questions to make sure you understand all the
variables and address any ambiguity – Ideal time - 2 minutes
2. Rephrase the Question - Repeat the question back in your own words so the interviewer
knows you understand the problem, highlight the signature, the inputs you are given and
what type of method or function you will be using – Ideal time 1 minute
3. Layout the Algorithm - In order to save time and optimize communication write down
your algorithm either before or as you review it with your interviewer. Make sure you
state time complexity and have your interviewer agree – Ideal time 3 - 4 minutes
4. Write your code — I have found it is hard to talk and code so for best performance let
the interviewer know you are going to focus on Coding for the next 10-15 minutes.
5. Review your code - Optimize, validate, identify edge cases, fix any bugs, test your code
and discuss where future enhancements might arise with your interviewer. Ideal time – 5
minutes
1
Amazon Confidential
2
Amazon Confidential
These questions are designed to test your Problem Solving Ability, knowledge of data structures &
algorithms and if you can write logical and maintainable code.
A covert agent has some crucial information stored in the form of an array of integers. The array
contains sensitive information and it must not be revealed to anyone. However, there are few
things about the array which are known.
An array is said to be analogous to the secret array if all of the following conditions are true:
The length of the array is equal to the length of the secret array.
Each integer in the array lies in the interval [lowerBound, upperBound].
The difference between each pair of consecutive integers of the array must be equal to the
difference between the respective pair of consecutive integers in the secret array. In other
words, let the secret array be [s[0], s[1],...s[n-1]]and let the analogous array be [a[0], a[1],...a[n-
1]], then (a[i-1] - a[i]) must be equal to (s[i-1] - s[i]) for each i from 1 to n-1.
Given the value of the integers lowerBound and upperBound, inclusive, and the array of
differences between each pair of consecutive integers of the secret array, find the number of
arrays that are analogous to the secret array. If there is no array analogous to the secret array,
return 0.
For example:
consecutiveDifference = [-2, -1, -2, 5]
lowerBound=3
upperBound=10
The logic to create an analogous array starting from the lower bound is:
Start with a value of 3.
Subtract consecutiveDistances[0], 3 - (-2) = 5
Subtract consecutiveDistances[1], 5 - (-1) = 6
Subtract consecutiveDistances[2], 6 - (-2) = 8
Subtract consecutiveDistances[3], 8 - 5 = 3
Note that none of the values is out of bounds. All possible analogous arrays are:
3
Amazon Confidential
[3, 5, 6, 8, 3]
[4, 6, 7, 9, 4]
[5, 7, 8, 10, 5]
The answer is 3.
Function Description
Complete the function countAnalogousArrays in the editor below.
Constraints
-109 ≤ lowerBound ≤ upperBound ≤ 109
1 ≤ n ≤ 105
-2*109 ≤ consecutiveDifference[i] ≤ 2*109
An Amazon Fulfillment Associate has a set of items that need to be packed into two boxes. Given an integer
array of the item weights (arr) to be packed, divide the item weights into two subsets, A and B, for packing into
the associated boxes, while respecting the following conditions:
Return the subset A in increasing order where the sum of A's weights is greater than the sum of B's weights. If
more than one subset A exists, return the one with the maximal total weight.
4
Amazon Confidential
Example
n=5
arr = [3, 7, 5, 6, 2]
The 2 subsets in arr that satisfy the conditions for A are [5, 7] and [6, 7] :
A is minimal (size 2)
Sum(A) = (5 + 7) = 12 > Sum(B) = (2 + 3 + 6) = 11
Sum(A) = (6 + 7) = 13 > Sum(B) = (2 + 3 + 5) = 10
The intersection of A and B is null and their union is equal to arr.
The subset A where the sum of its weight is maximal is [6, 7].
Function Description
Complete the minimalHeaviestSetA function in the editor below.
Returns:
int[] : an integer array with the values of subset A
Constraints
1 ≤ n ≤ 105
1 ≤ arr[i] ≤ 105 (where 0 ≤ i < n)
3. Robot Rodeo
An Amazon Fulfillment Associate has a set of items that need to be packed into two boxes. Given an integer
array of the item weights (arr) to be packed, divide the item weights into two subsets, A and B, for packing into
the associated boxes, while respecting the following conditions:
Return the subset A in increasing order where the sum of A's weights is greater than the sum of B's weights. If
more than one subset A exists, return the one with the maximal total weight.
5
Amazon Confidential
Example
n=5
arr = [3, 7, 5, 6, 2]
The 2 subsets in arr that satisfy the conditions for A are [5, 7] and [6, 7] :
A is minimal (size 2)
Sum(A) = (5 + 7) = 12 > Sum(B) = (2 + 3 + 6) = 11
Sum(A) = (6 + 7) = 13 > Sum(B) = (2 + 3 + 5) = 10
The intersection of A and B is null and their union is equal to arr.
The subset A where the sum of its weight is maximal is [6, 7].
Function Description
Complete the minimalHeaviestSetA function in the editor below.
Returns:
int[] : an integer array with the values of subset A
Constraints
1 ≤ n ≤ 105
1 ≤ arr[i] ≤ 105 (where 0 ≤ i < n)
4. Shopping Options
An Amazon customer wants to buy a pair of jeans, a pair of shoes, a skirt, and a top but has a limited budget in
dollars. Given different pricing options for each product, determine how many options our customer has to
buy 1 of each product. You cannot spend more money than the budgeted amount.
Example
priceOfJeans = [2, 3]
priceOfShoes = [4]
priceOfSkirts = [2, 3]
priceOfTops = [1, 2]
budgeted = 10
6
Amazon Confidential
The customer must buy shoes for 4 dollars since there is only one option. This leaves 6 dollars to spend on the
other 3 items. Combinations of prices paid for jeans, skirts, and tops respectively that add up to 6 dollars or
less are [2, 2, 2], [2, 2, 1], [3, 2, 1], [2, 3, 1]. There are 4 ways the customer can purchase all 4 items.
Function Description
Complete the getNumberOfOptions function in the editor below. The function must return an integer which
represents the number of options present to buy the four items.
Constraints
1 ≤ a, b, c, d ≤ 103
1 ≤ dollars ≤ 109
1 ≤ price of each item ≤ 109
Note: a, b, c and d are the sizes of the four price arrays
5. Algorithm Swap
You're a new Amazon Software Development Engineer (SDE). You're reading through your team's code and
find an old sorting algorithm. The following algorithm is used to sort an array of distinct n integers:
Try to find the smallest pair of indices 0 ≤ i < j ≤ n-1 such that arr[i] > arr[j]. Here smallest means usual
alphabetical ordering of pairs, i.e. (i1, j1) < (i2, j2) if and only if i1 < i2 or (i1 = i2 and j1 < j2).
If there is no such pair, stop.
Otherwise, swap a[i] and a[j] and repeat finding the next pair.
7
Amazon Confidential
The algorithm seems to be correct, but the question is how efficient is it? Write a function that returns the
number of swaps performed by the above algorithm.
For example, if the initial array is [5,1,4,2], then the algorithm first picks pair (5,1) and swaps it to produce
array [1,5,4,2]. Next, it picks pair (5,4) and swaps it to produce array [1,4,5,2]. Next, pair (4,2) is picked and
swapped to produce array [1,2,5,4], and finally, pair (5,4) is swapped to produce the final sorted
array [1,2,4,5], so the number of swaps performed is 4.
Function Description
Complete the function howManySwaps in the editor below. The function should return an integer that
denotes the number of swaps performed by the proposed algorithm on the input array.
Constraints
1 ≤ n ≤ 105
1 ≤ arr[i] ≤ 109
all elements of arr are unique
An Amazon Fulfillment Associate has a set of items that need to be packed into two boxes. Given an integer
array of the item weights (arr) to be packed, divide the item weights into two subsets, A and B, for packing into
the associated boxes, while respecting the following conditions:
8
Amazon Confidential
Return the subset A in increasing order where the sum of A's weights is greater than the sum of B's weights. If
more than one subset A exists, return the one with the maximal total weight.
Example
n=5
arr = [3, 7, 5, 6, 2]
The 2 subsets in arr that satisfy the conditions for A are [5, 7] and [6, 7] :
A is minimal (size 2)
Sum(A) = (5 + 7) = 12 > Sum(B) = (2 + 3 + 6) = 11
Sum(A) = (6 + 7) = 13 > Sum(B) = (2 + 3 + 5) = 10
The intersection of A and B is null and their union is equal to arr.
The subset A where the sum of its weight is maximal is [6, 7].
Function Description
Complete the minimalHeaviestSetA function in the editor below.
Returns:
int[] : an integer array with the values of subset A
Constraints
1 ≤ n ≤ 105
1 ≤ arr[i] ≤ 105 (where 0 ≤ i < n)
7. Storage Optimization
Amazon is experimenting with a flexible storage system for their warehouses. The storage unit consists of a
shelving system which is one meter deep with removable vertical and horizontal separators. When all
separators are installed, each storage space is one cubic meter (1' x 1' x 1'). Determine the volume of the
largest space when a series of horizontal and vertical separators are removed.
Example
n=6
9
Amazon Confidential
m=6
h = [4]
v = [2]
Consider the diagram below. The left image depicts the initial storage unit with n = 6 horizontal and m = 6
vertical separators, where the volume of the largest storage space is 1 × 1 x 1. The right image depicts that unit
after the fourth horizontal and second vertical separators are removed. The maximum storage volume for that
unit is then 2 × 2 x 1 = 4 cubic meters:
Function Description
Complete the function storage in the editor below.
Constraints
1 ≤ n, m ≤ 105
0<x≤n
0<y≤m
10
Amazon Confidential
1 ≤ h[i] ≤ n, where 1 ≤ i ≤ n.
1 ≤ v[j] ≤ m, where 1 ≤ j ≤ m.
The values in array h are distinct.
The values in array v are distinct.
I run a parking garage that provides valet parking service. When a customer drives up to the entrance
they are given a ticket they can use to later collect their car and their car is parked for them somewhere
in the garage.
Design a program that can track usage of the garage and issue these tickets.
11