0 like 0 dislike
2,395 views

edited | 2,395 views

0 like 0 dislike

Hey everyone. I just gave my Amazon OA today. Here are the questions I received.

1.

Given a List of Orders , where each Order is a string . For example : [zld 93 12 ] [ fp kindle book] are two orders. Each order has an ID which is first part of the order ( ID of order 1 = aax , ID2 = ff ). The remaining part of the order is referred to as MetaData. There are two types of orders, Prime orders ( which contain non numeric Meta Data) and Non-Prime Orders (which contain Only-Numeric Meta Data). Sort the List of Orders as given below :
a. Prime Orders before NonPrime Orders
b. Prime Order with lexicographically smaller MetaData comes first.
c. In Case of tie in (b) , the one with lexicographically smaller ID comes first.
d. The relative order of NonPrime Orders remains the same.

2.

A combination of Amazon Fresh deliveries and Demolition Robot, where the problem statement is almost the same as Amazon Fresh Deliveries but you are asked to find the minimum path to the destination. Input and output is exactly the same as Demolition Robot:
Given a matrix with values 0 (trenches) , 1 (flat) , and 9 (destination) you have to find minimum distance to reach 9 (destination). If not possible then return -1.
The demolition robot must start at the top left corner of the matrix, which is always flat, and can move on block up, down, right, left.
The demolition robot cannot enter 0 trenches and cannot leave the matrix.
Sample Input :
[1, 0, 0],
[1, 0, 0],
[1, 9, 1]]
Sample Output :
3
Should use BFS for the second question as you may encounter TLE with DFS

by Expert (111,350 points)
0 like 0 dislike
``````import functools
def sortOrders(orderList):
p,np = [],[]
for order in orderList:
splitted = order.split(' ',1)
if splitted[1][0].isdigit():
np.append(order)
else:
p.append([splitted[0],splitted[1]])
def compare(order1,order2):
if(order1[1] < order2[1]):
return -1
elif(order1[1] == order2[1]):
if(order1[0] < order2[0]):
return -1
return 1
p = sorted(p,key = functools.cmp_to_key(compare))
return [order[0] + ' ' + order[1] for order in p] + np``````

by Expert (111,350 points)