Panda Guru LogoPanda
Guru

Amazon OA

Round 1

Questions: Given orders and shifts, return pending orders at the end of each shift.

Input:

n=5, orderProcessingTime=[2,4,5,1,1], m=5, shiftDuration=[1,5,1,5,2]

Output:

[5,3,3,1,0]

If an order is not fully processed by the end of a shift, it will be continued in the next shift.

Input:

n=5, orderProcessingTime=[1,2,4,1,2], m=5, shiftDuration=[3,10,1,1,1]

Output:

[3,0,4,4,3]
Candidate's Approach

Two pointers, one on the order processing time and the other on the shift. For every shift, check if we can finish the order and move the pointers accordingly. 11 out of 15 test cases passed, and others resulted in TLE.

List<Integer> pendingOrders(int n, int m, List<Integer> orders, List<Integer> shifts) { List<Integer> ans = new ArrayList<>(); int i = 0, j = 0, sum = 0; while (i<shifts.size()) { int dur = shifts.get(i); while (j<orders.size()) { if (sum==0) { sum+=orders.get(j); } if (sum>dur) { sum = sum-dur;break; } else { dur = dur-sum; sum = 0; j++; } } ans.add(orders.size()-j); if (j==orders.size()) j = 0; i++; } return ans; }
Interviewer's Feedback

No feedback provided.