Warehouse Allocation (Distribute)
Updated
Problem statement
Amazon has a warehouse that stores piles of boxes containing goods to be shipped. There are n piles numbered 1, 2, ..., n, where the i-th pile has boxes[i] boxes.
To achieve an even distribution of boxes, the caretaker can perform the following operation any number of times (possibly zero):
- Choose two distinct piles
iandjsuch thatboxes[i] > 0. - Remove one box from pile
iand place it on pilej(incrementboxes[j]by 1 and decrementboxes[i]by 1).
The caretaker wishes to minimize the difference between the maximum and the minimum number of boxes among the piles. Call this minimum achievable difference d.
Complete the function findMinimumOperations, which returns the minimum number of operations required to reach a configuration whose difference between the maximum and minimum number of boxes equals d.
Function
findMinimumOperations(boxes: int[]) → longExamples
Example 1
boxes = [5, 5, 8, 7]return = 2
Consider the number of piles to be n = 4 and the boxes in them are boxes = [5, 5, 8, 7]. The minimum possible difference that can be achieved is 1 by transforming the piles into [6, 6, 7, 6] as below. Hence the answer is 2.
Example 2
boxes = [2, 4, 1]return = 1Move a box from pile 2 to pile 3: [2, 4, 1] -> [2, 3, 2]
Example 3
boxes = [4, 4, 4, 4, 4]return = 0Constraints
1 <= n <= 1051 <= boxes[i] <= 109
More Amazon interview questions
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Server InvestmentRippling · OARippling interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Delete and Freeze GameRubrik · OARubrik interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions