AMAZON Generic Coding Question Solved

8 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

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one opera… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |