Katkat

Solve Shipping and Bundle-Cost Problems

AmazonSoftware Engineer · Onsite

Last updated

This problem tests your ability to determine the minimum capacity required to complete tasks within a deadline and optimize the cost of purchasing items grouped in bundles.

Part 1: Minimum Shipping Capacity

You are given an array of package weights weights and an integer days. You need to ship all packages such that every day you ship packages in the order given in the array. You cannot exceed the maximum daily capacity C. Find the minimum C such that you can ship all packages within days days.

def min_shipping_capacity(weights: list[int], days: int) -> int:
    pass

Example: Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5 Output: 15 Explanation: A capacity of 15 allows shipping: [1,2,3,4,5], [6,7], [8], [9], [10].

Part 2: Bundle Cost Minimization

You have a list of individual item prices and a list of 'bundle' offers. A bundle consists of a specific set of items for a discounted price. Given a target list of items you need to purchase, calculate the minimum cost to acquire all required items, potentially using bundles.

def min_bundle_cost(items: list[str], prices: dict[str, int], bundles: list[tuple[list[str], int]]) -> int:
    pass

Example: Input: items = ['apple', 'apple', 'orange'], prices = {'apple': 10, 'orange': 5}, bundles = [(['apple', 'apple'], 15)] Output: 20 Explanation: Buying two apples as a bundle costs 15, plus 5 for the orange = 20. Buying individually costs 10+10+5 = 25.

Hints

Hint 1

For the shipping problem, notice that the answer is monotonic, suggesting binary search on the capacity range.

Hint 2

For the bundle problem, consider this as a variation of the change-making problem or knapsack problem where you need to reach a specific state.

Hint 3

Use recursion with memoization to explore purchasing items either individually or via available bundles.

Follow-up questions

  • How would you modify the shipping capacity algorithm if the order of packages could be rearranged?
  • What is the time complexity of the bundle minimization if the number of items and bundles grows significantly?
  • How would you handle a scenario where bundles have expiration dates or limited stock?