Question 1:
Amazon ships millions of packages every day. A large percentage of them are fulfilled by. Amazon, so it is important to minimize shipping costs. It has been found that moving a group of 3 packages to the shipping facility together is most efficient. The shipping process needs to be optimized at a new warehouse. There are the following types of queries or requests:
INSERT package id: insert package id in the queue of packages to be shipped
SHIP-: ship the group of 3 items that were in the queue earliest i.e. they are returned in the order they entered,
Perform q queries and return a list of lists, one for every SHIP - type query. The lists are
either; 3 package ID strings in the order they were queued. Or, if there are not enough packages in the queue to fulfill the query, the result is I"N/A"J.
Note:
•: Initially, the queue is empty.
•. The list of packages shipped per group should be in the order they were queued.
The function performQueries take List<List<>> of type String as a parameter which contains each query where
list.get(i).get(0) = INSERT | SHIP
list.get(i).get(1) = shipmentID | -
Example Test Case:
5
2
INSERT GT23513413
INSERT TQC2451340
SHIP -
INSERT VYP8561991
SHIP
Expected result:
[N/A]
[GT23513413, TQC2451340, VYP8561991]
from collections import deque
class Solution:
def __init__(self):
self.ship_queue = []
self.batch_shipment = deque([])
def insert_ship(self, insert_and_ship_orders:[[]]):
print(f'Input: {insert_and_ship_orders}')
for order in insert_and_ship_orders:
operation, code = order
if operation == 'INSERT':
self.batch_shipment.append(code)
elif operation == 'SHIP':
if len(self.batch_shipment) < 3:
self.ship_queue.append(['N/A'])
else:
batch_of_three = []
counter = 3
while counter > 0:
batch_of_three.append(self.batch_shipment.popleft())
counter -= 1
self.ship_queue.append(batch_of_three)
def get_result(self):
return self.ship_queue
def main():
s = Solution()
s.insert_ship(insert_and_ship_orders = [['INSERT','GT23513413'], ['INSERT', 'TQC2451340'], ['SHIP','-'], ['INSERT','VYP8561991'], ['SHIP','-']])
print(f'Result: {s.get_result()}')
s = Solution()
s.insert_ship(insert_and_ship_orders = [["INSERT", "UIAHSIHGFG"] , ["INSERT", "RGSRTWR"] , ["SHIP", "-"] , ["INSERT", "EYJTRJYEYTJ"] , ["INSERT", "QERGFSFDS"], ["SHIP", "-"]])
print(f'Result: {s.get_result()}')
s = Solution()
s.insert_ship(insert_and_ship_orders = [["INSERT", "UIAHSIHGFG"] , ["INSERT", "RGSRTWR"] , ["SHIP", "-"] , ["INSERT", "EYJTRJYEYTJ"] , ["INSERT", "QERGFSFDS"], ["SHIP", "-"], ["INSERT", "QERGFSFDS2"], ["SHIP", "-"], ["INSERT", "QERGFSFDS3"], ["SHIP", "-"]])
print(f'Result: {s.get_result()}')
if __name__ == '__main__':
main()Input: [['INSERT', 'GT23513413'], ['INSERT', 'TQC2451340'], ['SHIP', '-'], ['INSERT', 'VYP8561991'], ['SHIP', '-']]
Result: [['N/A'], ['GT23513413', 'TQC2451340', 'VYP8561991']]
Input: [['INSERT', 'UIAHSIHGFG'], ['INSERT', 'RGSRTWR'], ['SHIP', '-'], ['INSERT', 'EYJTRJYEYTJ'], ['INSERT', 'QERGFSFDS'], ['SHIP', '-']]
Result: [['N/A'], ['UIAHSIHGFG', 'RGSRTWR', 'EYJTRJYEYTJ']]
Input: [['INSERT', 'UIAHSIHGFG'], ['INSERT', 'RGSRTWR'], ['SHIP', '-'], ['INSERT', 'EYJTRJYEYTJ'], ['INSERT', 'QERGFSFDS'], ['SHIP', '-'], ['INSERT', 'QERGFSFDS2'], ['SHIP', '-'], ['INSERT', 'QERGFSFDS3'], ['SHIP', '-']]
Result: [['N/A'], ['UIAHSIHGFG', 'RGSRTWR', 'EYJTRJYEYTJ'], ['N/A'], ['QERGFSFDS', 'QERGFSFDS2', 'QERGFSFDS3']]Question:2
Q2. Given an array ranks of ranks of students in a school. All students need to be split into groups k. Find the total 'imbalance' of all groups. An imabalance of a group can be found as :
Sorting each group in the order of their ranks.
A group contributes to imbalance if any 2 students in the sorted array have a rank difference of more than 1.
Find the total sum of imbalance of all such groups.
This is the example that was given :
[4,1,3,2]
[1] contributes 0 to imbalance
[2] contributes 0 to imbalance
[3] contributes 0 to imbalance
[4] contributes 0 to imbalance
[4,1] contributes 1 to imbalance
[4,3] contributes 0 to imbalance
[4,2] contributes 1 to imbalance
[4,1,3,2] contributes 0 to imbalance
[1,3] contributes 1 to imbalance
[1,2] contributes 0 to imbalance
[3,2] contributes 0 to imbalance
Answer = 1 + 1 + 1 = 3
solved the Quesion with O(n) complexity
https://leetcode.com/playground/eWkHLSGDAll the testcases passed for both the Questions
Still waiting for the mail/call for the further steps/result.
How much time does it take for the interview schedule call after the OA ?