In-feeds

Total views

Amazon SDE-1 Test and Solutions

SDE-1 Coding Test and Solutions



Firstly this is neither an on-campus nor off-campus test, this test is for those who are applying through Company's career portals. 

Secondly, this test is solely for Software Development Engineer - 1 role and should not be confused with tests for other roles.

If the position for the SDE-1 role is there and we applied for it then we may get in contact with any one of the HRs of the team, which will take our application forward.

Then they will ask to complete this online exercise

Overview of Assessment

The 3 sections of the assessment are:
  1. Coding challenge (2 scenarios 70 mins) 
  2. Workstyle survey (1 section) - 15 minutes
  3. Feedback survey - 2 minutes


So without spending more time on details of the test, let's jump on the questio ns

Code Question 1

The developers at Company XYZ employ multiple encryption methods so as to keep user data protected and to ensure maximum security.
'Pascal Triangle' is one method for encryption. When an array of digits is fed to this system, It sums the adjacent digits. It then takes the rightmost digit (least significant digit) of each addition for the next step. Thus, the number of digits in each step is reduced by 1. 
This procedure is repeated until there are only 2 digits left, and this sequence of 2 digits forms the encrypted number.

Find the encrypted number, given the initial array of the digits of numbers. You should report a string of digits representing the encrypted number.

Example
numbers = (4, 5,6, 7)

Illustration of example








Function Description

Complete the function getEncryptedNumber(int[] numbers)

getEncryptedNumber has the following parameter:
  • 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
Sample Case 0
numbers =  [4, 3, 2, 1]
Output = 28
Explanation: The encryption occurs as follows: [4, 3, 2, 1] -> [7, 5, 3] -> [2, 8] 

Sample Case 1
numbers =  [5, 4]
Output = 54
Explanation: the number of digits in the sequence is already equal to 2 so the number remains unchanged.

    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

Millions of packages are shipped by Company XYZ regularly. There are a number of parcels that need to be shipped. 
Find the minimum possible sum of transportation costs incurred in the shipment of additional parcels in the following scenario.
  • 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
The parcel id is also the cost to ship that parcel.

Given the parcel IDs which are already added in the shipment, find the minimum possible cost of shipping the items added to complete the load.

Example
parcels = [2, 3, 6, 10, 11]
k = 9

Parcel ids range from 1 through infinity. After reviewing the current manifest, the remaining parcels to choose from are [1, 4, 5, 7, 8, 9, 12, 13, ..].There are 5 parcels already on the truck, and it can carry k= 9 parcels when fully loaded. Choose 4 more packages to include:(1, 4, 5, 7]. Their shipping cost is  1+4+5+7= 17, which is minimal. Return 17.

Function Description

Complete the function getMinimumCost(int[] parcels, int k)
getMinimumCost() has the following parameters:
  • int[n] parcels: the parcels already in the shipment.
  • int k: the truck's capacity
  • Returns
    • long_ int: the minimum additional transportation cost Incurred
Constraints
  • 1 <= n <= 2 * 10^5
  • 1 <= parcels[i]<= 10^6
  • n <= k <= 10^6
Sample Case 0

parcels = [3, 4, 1, 5, 6]
k = 7
Output = 9
Explanation: The shipment already has 5 parcels and needs 2 more. The parcels [2, 7] can be added to have the shipment of exactly 7 parcels, [1, 2, 3, 4, 5, 6, 7]. The additional cost is 2+7=9.

Sample Case 1

parcels = [7, 4, 5, 11]
k = 4
Output= 0
Explanation: The truck is already filled to capacity.

    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;
  }



If you got some help please likeđź‘Ť the button at the top. Also poll your programming choice at the bottom đź‘‡ of article

Comments

Popular posts from this blog

Solutions LTI Infinity Coding Challenge 2021

LTI Infinity Coding Challenge 2021

Codenation CodeAgon 2021