Panda Guru LogoPanda
Guru

AMAZON CODING ROUND Q2

Round 2

Questions: An AWS client has brought servers and databases from data centers in different parts of the world for their application. For simplicity, let's assume all the servers and data centers are located on a 1-dimensional line.

You have been given the task of optimizing the network connection. Each data center must be connected to a server. The positions of n data centers and n servers are given in the form of arrays. Any particular data center, center[i], can deliver to any particular server destination, destination[j]. The lag is defined as the distance between a data center at location x and a server destination at location y, i.e., the absolute difference between x and y. Determine the minimum lag to establish the entire network.

There are n = 3 connections, the positions of data centers, center = [1, 2, 2], and the positions of the server destinations, destination = [5, 2, 4].

The most efficient deliveries are:

The minimum total lag is = abs(1-2) + abs(2-4) + abs(2-5) = 1 + 2 + 3 = 6.

Function Description: Complete the function getMinDistance in the editor below.

getMinDistance has the following parameter(s):

Returns:

Solution:

def getMinDistance(center, destination): # Sort both lists to match nearest elements center.sort() destination.sort() # Calculate the minimum lag total_lag = 0 for i in range(len(center)): total_lag += abs(center[i] - destination[i]) return total_lag
Candidate's Approach

The candidate sorted both the center and destination arrays to ensure that the nearest elements were matched. Then, they calculated the total lag by summing the absolute differences between the corresponding elements in the sorted arrays.

Interviewer's Feedback

No feedback provided.