List of questions which were asked during the online test
1. Optimizing Alexa Suggestions
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 nearest X vegetarian restaurants to the customer’s location.
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;
numRestaurents, an integer representing the number of nearby vegetarian restaurants that would be returned to the customer(X)
Return a list of elements where each element of the list represent recommended vegetarian restaurant relative to customer’s location.
The Customer begins at the location [0,0].
The returned output can be any order.
def kClosest(locations,k): def sqr_dist(location): return location*location+location*location locations.sort(key=sqr_dist) return locations[:k] alllocations=[[1,2][3,4][1,-1]] numRestaurents=2 print(kClosest(alllocations,numRestaurents))
2. Order Delivery Route
Amazon Fresh is a grocery delivery service that offers consumers the option of purchasing their groceries online and having them delivered on schedule. The Amazon Fresh tram is planning a route for a delivery truck to deliver customer orders in the city of TechIandia. The planner will create a delivery area for each order to effectively plan the route. The area is abstracted as a grid. Not all location locations are accessible by road. The truck only needs to make a single delivery.
Write an s]algorithm to determine the minimum distance required for the truck to deliver the order.
- Some places in the delivery area cannot be accessed by the driver, as there are no roads in those locations.
- The delivery area can be represented as a two-dimensional grid of integers, where each integer represents one cell.
- The truck must start from the top-left corner of the area which is always accessible and move on cell up, down, left, or right at a time.
- The truck must navigate around the areas without roads and cannot leave the area.
- The accessible area is represented as 1, areas without roads are represented by o and the order destination is represented by 0 and the order destination is represented by 9.
The input to the function /method consists of one argument:
area, representing the two-dimensional grid of integers.
Return an integer representing the total distance traversed to deliver the order else return-1
1<= rows, columns<=103
def minDistance(grid, cur, col): if grid[cur]==9: return 0 rInt=cur+1 dInt=cur+col if rInt<len(grid) and dInt<len(grid) and grid[rInt]>0 and grid[dInt]>0: return 1+min(minDistance(grid, rInt, col), minDistance(grid, dInt, col)) elif rInt<len(grid) and grid[rInt]>0: return 1+minDistance(grid, rInt, col) elif dInt<len(grid) and grid[dInt]>0: return 1+minDistance(grid, dInt, col) else: return len(grid) def findShortedtPath(grid): liPath= rows = len(grid) columns = len(grid) for i in grid: liPath.extend(i) val=minDistance(liPath, 0, columns) if val<=len(liPath): return val else: return -1 area = [[1,0,0],[1,0,0],[1,9,1]] print(findShortedtPath(area))