Code Question 1
Amazon's Alexa team is working on optimizing the customer experience for scenarios where customers ask generic questions. One example of a generic question is:
"What are good vegetarian restaurants nearby?"
In this example, Alexa would then search for a list of vegetarian restaurants in the city and select the nearest X vegetarian restaurants relative to the customer's location.
Given an array representing the locations of N vegetarian restaurants in the city, implement an algorithm to find the nearest X vegetarian restaurants to the customer's location.
Input
The input to the function/method consists of two arguments:
- allLocations: A list of elements where each element consists of a pair of integers representing the x and y coordinates of the vegetarian restaurant in the city.
- numRestaurants: An integer representing the number of nearby vegetarian restaurants that would be returned to the customer (X).
Output
Return a list of elements where each element of the list represents the x and y integer coordinates of the nearest recommended vegetarian restaurant relative to the customer's location.
- If there is one tie, use the location with the closest X coordinate.
- If no location is possible, return a list with an empty location - not just an empty list.
Constraints
- numRestaurants ≤ size(allLocations)
Note
- The customer begins at the location [0, 0].
- The distance from the customer's current location to a recommended vegetarian restaurant location (x, y) is calculated as: distance = sqrt(x² + y²)
- If there are ties, return any of the tied locations as long as you satisfy returning exactly X nearby vegetarian restaurants.
- The returned output can be in any order.
Example
Input:
allLocations = [[1, 2], [3, 4], [1, -1]]
numRestaurants = 2
Output:
[[1, -1], [1, 2]]
Explanation:
- The distance from the customer's current location [0, 0] to location [1, 2] is sqrt(5) ≈ 2.236.
- The distance to location [3, 4] is sqrt(25) = 5.
- The distance to location [1, -1] is sqrt(2) ≈ 1.414.
The required number of vegetarian restaurants is 2, hence the output is [[1, -1], [1, 2]]
.
我们长期稳定承接各大科技公司如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.