AMAZON Generic Coding Question Solved
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 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 the square root of xΒ² + yΒ².
If there are ties then return any of the 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 of the customer's current location from location [1, 2] is β5 β 2.236
The distance from location [3, 4] is β25 = 5
The distance from location [1, -1] is β2 β 1.414
The required number of vegetarian restaurants is 2, hence the output is [1, -1] and [1, 2].