Capacity to ship within D days
Python data engineering interview problem. Difficulty: intermediate. Pattern: Binary Search. About 16 minutes. Part of the Pro drill bank.
Calculate the minimum ship weight capacity needed to ship all parcels within the required number of days. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement ship_within_days(weights: list[int], days: int) -> int to determine the minimum boat capacity required to ship all packages within days days. Packages arrive in the order given by weights and must be shipped in that exact sequence. Each day, the ship loads packages from the conveyor belt until adding the next package would exceed the boat's maximum weight capacity. The boat makes one trip per day.
Requirements
- Packages must be loaded in the exact given order.
- Return the smallest valid integer capacity.
Constraints
- 1 <= days <= len(weights) <= 50,000
- 1 <= weights[i] <= 500
Examples
Input: ship_within_days([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5) Output: 15 A capacity of 15 ships packages over 5 days: [1..5] (15), [6, 7] (13), [8] (8), [9] (9), [10] (10).
Topics: lakebench, python, answer-space, greedy.
More Python interview questions · All interview problems · Learn data engineering
Capacity to ship within D days
Interview-style drill: Calculate the minimum ship weight capacity needed to ship all parcels within the required number of days.
Implement `ship_within_days(weights: list[int], days: int) -> int` to determine the minimum boat capacity required to ship all packages within `days` days. - Packages arrive in the order given by `weights` and must be shipped in that exact sequence. - Each day, the ship loads packages from the conveyor belt until adding the next package would exceed the boat's maximum weight capacity. - The boat makes one trip per day.