Amazon SDE-1 Test and Solutions
SDE-1 Coding Test and Solutions
Overview of Assessment
- Coding challenge (2 scenarios 70 mins)
- Workstyle survey (1 section) - 15 minutes
- Feedback survey - 2 minutes
Code Question 1
Illustration of example |
Function Description
- int[] numbers: the initial sequence of digits
- Returns
- string: the encrypted number represented as a string of 2 characters.
Constraints
- 2 <= numbers.length <= 5 * 10^3
- 0 <= numbers[i] <= 9
public static String
getEncryptedNumber(List<Integer> numbers){
//Write your code here
int repeat = numbers.size() - 2;
int j = 2;
while(repeat-- > 0)
{
int len = numbers.size() - j;
for (int i = 0 ; i <= len ;
i++){
if(numbers.get(i) > 9)
numbers.set(i,numbers.get(i) % 10);
if(numbers.get(i+1) > 9)
numbers.set(i+1,numbers.get(i+1) % 10);
numbers.set(i,
numbers.get(i) + numbers.get(i+1)) ;
}
j++;
}
if(numbers.get(0) > 9)
numbers.set(0,
numbers.get(0)%10);
if (numbers.get(1) > 9)
numbers.set(1,
numbers.get(1)%10) ;
return numbers.get(0).toString() +
numbers.get(1).toString();
}
Code Question 2
- k parcels are carried by fully loaded truck
- It is most efficient for the truck to be fully loaded.
- There are a number of parcels already on the truck as listed in parcels.
- There are parcels with a unique id that ranges from 1through infinity
Function Description
- int[n] parcels: the parcels already in the shipment.
- int k: the truck's capacity
- Returns
- long_ int: the minimum additional transportation cost Incurred
- 1 <= n <= 2 * 10^5
- 1 <= parcels[i]<= 10^6
- n <= k <= 10^6
public static long getMinimumCost(List<Integer> parcels,
int k) {
//Write your code here
long cost = 0;
long reqd = k -
parcels.size() ;
if(reqd <= 0)
return cost;
HashSet<Integer>
hs = new HashSet<>();
hs.addAll(parcels) ;
int i = 1;
while(reqd > 0){
if( !hs.contains(i) ){
cost += i;
reqd--;
}
i++;
}
return cost;
}
Comments
Post a Comment
We need people who give us feedback and this is how we grow. Please do comment if you find something faulty, wrong , helpful, valuable in anyway. You can comment anonymously. Thanks for your contribution.