Cargo Order Allocation Across Departing Planes
OptiverSoftware Engineer · Technical ScreenLast updated
This question evaluates competence in deterministic resource allocation, stateful simulation, and capacity-management under time constraints, exercising algorithmic reasoning and data-structure handling within the Coding & Algorithms domain.
A freight company assigns incoming shipping orders to a fixed fleet of cargo planes. You must implement the order-processing logic: decide whether each order can be accepted, and if so, allocate its cargo across planes and update the fleet's remaining capacity.
You are given planes, where planes[i] = [departure_i, capacity_i]: plane i departs at time departure_i and can carry at most capacity_i units of cargo. Each plane's departure time and capacity are fixed; its remaining capacity only decreases as cargo is assigned to it and never resets.
Orders arrive as a sequence and must be processed strictly in the given order. orders[j] = [time_j, items_j], where time_j is the time the order is placed and items_j is a non-empty list of positive integers — the sizes of the cargo pieces in the order. Let total_j be the sum of items_j.
Rules
- Eligibility: cargo from order
jmay only be loaded onto planes that depart strictly aftertime_j(a plane withdeparture_i > time_j). - Splitting: cargo is divisible — an order's cargo may be split across multiple eligible planes in integer units. Individual piece boundaries do not restrict the split; only the order's total size
total_jmatters for allocation. - Acceptance: order
jis accepted if and only if, at the moment it is processed, the sum of the remaining capacities of all eligible planes is at leasttotal_j. - All-or-nothing: if an order is rejected, no plane's remaining capacity changes. A rejected order is never retried.
- Allocation policy (deterministic): when an order is accepted, fill eligible planes in ascending order of departure time, breaking ties by ascending plane index. Assign to each such plane
min(remaining cargo to place, plane's remaining capacity)units, skipping planes whose remaining capacity is0, until the fulltotal_junits are placed. Then commit the capacity updates.
Output
Return an array result with one element per order, in input order:
- If order
jis rejected,result[j]is an empty array[]. - If order
jis accepted,result[j]is an array of[i, amount]pairs — plane index and the number of units assigned to that plane — listed in the order the planes were filled, including only planes that receivedamount > 0.
Example
planes = [[5, 10], [10, 8], [3, 6]]
orders = [[2, [4, 5]], [4, [12]], [6, [9]], [0, [30]]]
- Order 0 (time 2, total 9): eligible planes (departure > 2) are 0, 1, 2. Fill by ascending departure: plane 2 (departs 3) takes 6, plane 0 (departs 5) takes 3. Result:
[[2, 6], [0, 3]]. Remaining capacities: plane 0 → 7, plane 1 → 8, plane 2 → 0. - Order 1 (time 4, total 12): eligible planes are 0 (7 left) and 1 (8 left); 7 + 8 = 15 ≥ 12, accepted. Plane 0 takes 7, plane 1 takes 5. Result:
[[0, 7], [1, 5]]. Remaining: plane 0 → 0, plane 1 → 3. - Order 2 (time 6, total 9): only plane 1 is eligible (departs 10) with 3 left; 3 < 9, rejected. Result:
[]. State unchanged. - Order 3 (time 0, total 30): all planes eligible, remaining sum is 0 + 3 + 0 = 3 < 30, rejected. Result:
[].
result = [[[2, 6], [0, 3]], [[0, 7], [1, 5]], [], []]
Constraints
1 <= planes.length <= 2 * 10^51 <= orders.length <= 2 * 10^5- The total number of cargo pieces across all orders is at most
4 * 10^5. 0 <= departure_i, time_j <= 10^91 <= capacity_i <= 10^9; each cargo piece size is between1and10^9.- All capacity and cargo sums fit in a signed 64-bit integer.
- Orders must be processed in the given sequence; each order's acceptance decision uses the fleet state left by all previous orders.
A solution that re-scans and re-sorts the whole fleet for every order will be too slow for the largest inputs; aim for roughly O((n + m) log n) overall, where n is the number of planes and m is the number of orders.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.