Skip to content

Calculate Fabric Waste

codepath-wiki-review[bot] edited this page Sep 28, 2026 · 7 revisions

TIP103 Unit 4 Session 1 (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the structure of the input?

    • A: The input consists of two lists: one list of tuples where each tuple contains a clothing item name (string) and its required fabric length (integer), and another list of integers representing the lengths of available fabric rolls.
  • Q: What is the output?

    • A: The output is an integer representing the total amount of fabric waste after producing all the clothing items.
  • Q: How should the fabric rolls be allocated to clothing items?

    • A: Each clothing item should be made with the smallest available roll that is large enough (a best-fit choice). Using the smallest sufficient roll keeps the waste on each item low and leaves the larger rolls available for items that need them.
  • Q: What should happen if a clothing item cannot be made with the available fabric rolls?

    • A: The problem assumes that there are enough fabric rolls to produce all items, so this scenario does not need to be handled explicitly.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Sort the fabric rolls in ascending order and match each clothing item with the smallest roll that meets or exceeds its required fabric length. Calculate the waste for each roll used.

1) Sort the `fabric_rolls` list in ascending order.
2) Initialize a variable `total_waste` to 0.
3) For each `item, required_length` in `items`:
   a) Iterate over the `fabric_rolls` to find the first roll that is greater than or equal to `required_length`. Because the list is sorted in ascending order, this is the smallest roll that fits.
   b) Subtract the `required_length` from the chosen roll and add the difference to `total_waste`.
   c) Mark the chosen roll as used by setting it to 0 or removing it from the list.
4) Return the `total_waste`.

⚠️ Common Mistakes

  • Forgetting to update the fabric roll after it has been used.
  • Mismanaging the fabric rolls, leading to inefficient allocation and increased waste.
  • Greedily grabbing the largest available roll for each item. This inflates the waste and can strand a later item with no roll big enough: with items [("a", 3), ("b", 10)] and rolls [10, 3], giving the 10-roll to "a" leaves no roll that fits "b", while the best-fit choice (the 3-roll for "a", the 10-roll for "b") produces both items with zero waste.

I-mplement

def calculate_fabric_waste(items, fabric_rolls):
    fabric_rolls.sort()
    total_waste = 0

    for item, required_length in items:
        for i in range(len(fabric_rolls)):
            if fabric_rolls[i] >= required_length:
                total_waste += fabric_rolls[i] - required_length
                fabric_rolls[i] = 0  # Use up the roll
                break

    return total_waste

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(m log m + n * m) where n is the number of clothing items and m is the number of fabric rolls. The initial sort is O(m log m), and the nested loop scans up to m rolls for each of the n items.
  • Space Complexity: O(m) auxiliary space in the worst case: the fabric rolls list is sorted in place, but Python's built-in sort (Timsort) can allocate a temporary array of up to m/2 elements while merging. The scan afterward adds no additional data structures.

Clone this wiki locally