[Amazon] OA – 12 Dec 2024

1. Code Question 1

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 as |x - y|, i.e., the absolute difference between x and y. Determine the minimum lag to establish the entire network.


Example
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 center at location 1 makes the first connection to the server at location 2.
  • The center at location 2 makes the second connection to the server at location 4.
  • The center at location 2 makes the third connection to the server at location 5.

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):

  • int center[n]: the positions of data centers
  • int destination[n]: the positions of server destinations

Returns
long int: the minimum lag


Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ center[i], destination[i] ≤ 10^9

Sample Case 0

Sample Input

5  
3  
1  
6  
8  
9  
5  
2  
3  
1  
7  
9  

Sample Output

5

Explanation
You may create the connections as:
center = [1, 3, 6, 8, 9]
destination = [2, 3, 7, 9]

The minimum total distance is calculated as:
abs(1 - 1) + abs(2 - 3) + abs(3 - 6) + abs(7 - 8) + abs(9 - 9) = 0 + 1 + 3 + 1 + 0 = 5.


2. Code Question 2

AWS provides many services for outlier detection. A prototype small service to detect an outlier in an array of integers is under development.

Given an array of n integers, there are (n - 2) normal numbers and the sum of the (n - 2) numbers originally in this array. If a number is neither in the original numbers nor is it their sum, it is an outlier. Discover the potential outliers and return the greatest of them.

Note: It is guaranteed that the answer exists.


Example

n = 6
arr = [4, 1, 3, 16, 2, 10]

There are two potential outliers:

  • Remove 16 → arr' = [4, 1, 3, 2, 10]. The sum of [4, 1, 3, 2] is 10, so 16 is a potential outlier. (It is not an original number or their sum.)
  • Remove 4 → arr' = [1, 3, 16, 2, 10]. The sum of [1, 3, 2, 10] is 16, so 4 is a potential outlier.

The outlier is the greater of the two potential outliers, so 16 is the outlier.


Function Description
Complete the function getOutlierValue in the editor below.

getOutlierValue has the following parameter(s):

  • int arr[n]: value of (n - 2) numbers, their sum, and outlier value.

Returns
int: the outlier value


Constraints

  • 3 ≤ n ≤ 10^5
  • 1 ≤ arr[i] ≤ 10^9

Input Format For Custom Testing


Sample Case 0

Sample Input

6  
4  
1  
2  
1  
10  
3  

Sample Output

1

Explanation
The original list is [1, 2, 3, 4] with a sum of 10.


我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *