AMAZON Generic Coding Question Solved

4 Live
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].

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |