Taro Logo

Categorize Box According to Criteria

Easy
Asked by:
Zendesk
5 views
Topics:
Greedy Algorithms

Given four integers length, width, height, and mass, representing the dimensions and mass of a box, respectively, return a string representing the category of the box.

  • The box is "Bulky" if:
    • Any of the dimensions of the box is greater or equal to 104.
    • Or, the volume of the box is greater or equal to 109.
  • If the mass of the box is greater or equal to 100, it is "Heavy".
  • If the box is both "Bulky" and "Heavy", then its category is "Both".
  • If the box is neither "Bulky" nor "Heavy", then its category is "Neither".
  • If the box is "Bulky" but not "Heavy", then its category is "Bulky".
  • If the box is "Heavy" but not "Bulky", then its category is "Heavy".

Note that the volume of the box is the product of its length, width and height.

Example 1:

Input: length = 1000, width = 35, height = 700, mass = 300
Output: "Heavy"
Explanation: 
None of the dimensions of the box is greater or equal to 104. 
Its volume = 24500000 <= 109. So it cannot be categorized as "Bulky".
However mass >= 100, so the box is "Heavy".
Since the box is not "Bulky" but "Heavy", we return "Heavy".

Example 2:

Input: length = 200, width = 50, height = 800, mass = 50
Output: "Neither"
Explanation: 
None of the dimensions of the box is greater or equal to 104.
Its volume = 8 * 106 <= 109. So it cannot be categorized as "Bulky".
Its mass is also less than 100, so it cannot be categorized as "Heavy" either. 
Since its neither of the two above categories, we return "Neither".

Constraints:

  • 1 <= length, width, height <= 105
  • 1 <= mass <= 103

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Are length, width, height, and mass non-negative integers in the same units?
  2. Is a box bulky when any side is at least 10000, or when its volume is at least 1000000000?
  3. Is a box heavy when its mass is at least 100?
  4. Should a box that is both bulky and heavy be labeled Both?
  5. Are the four labels exactly Both, Bulky, Heavy, and Neither?

Brute Force Solution

Approach

Check the bulky rules and the heavy rule as two separate questions, then combine those answers into one label. A box is bulky when any side is very long or the volume is huge, and heavy when the mass is large. The combination of those two facts is the category.

Here's how the algorithm would work step-by-step:

  1. Start with bulky and heavy both unset.
  2. Mark the box bulky if any side is at least 10000 or the product of the sides is at least 1000000000.
  3. Mark the box heavy if the mass is at least 100.
  4. If both marks are set, return Both.
  5. If only bulky is set, return Bulky.
  6. If only heavy is set, return Heavy.
  7. Otherwise return Neither.

Code Implementation

class Solution:
    def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
        is_bulky = False
        is_heavy = False

        # First, we evaluate if the box meets the criteria for being classified as 'Bulky'.
        # A box is bulky if any dimension is large or if its volume is huge.
        if (length >= 10**4 or width >= 10**4 or height >= 10**4 or
                length * width * height >= 10**9):
            is_bulky = True

        # Next, we check if the box meets the criteria for being 'Heavy' based on its mass.
        if mass >= 100:
            is_heavy = True

        # Combine the bulky and heavy flags to determine the final category.
        if is_bulky and is_heavy:
            return "Both"
        elif is_bulky:
            return "Bulky"
        elif is_heavy:
            return "Heavy"
        else:
            return "Neither"

Big(O) Analysis

Time Complexity
O(1) – There are four input numbers and a fixed set of comparisons. Multiplying the three sides and comparing the product with the volume cutoff is a constant amount of arithmetic. No list is scanned. The category is decided with a handful of branches. The cost does not grow with the size of the numbers beyond that arithmetic.
Space Complexity
O(1) – Only the bulky flag, the heavy flag, and the volume are stored. The four inputs are read in place. No table of categories is built. Extra memory stays constant.

Optimal Solution

Approach

Compute the volume once, then apply the bulky test and the heavy test independently. Combining those two yes-or-no answers produces one of the four category names. The volume is separated so the bulky test reads a single number instead of multiplying inside the comparison.

Here's how the algorithm would work step-by-step:

  1. Multiply length, width, and height to get the volume.
  2. Mark the box bulky when any side is at least 10000 or the volume is at least 1000000000.
  3. Mark the box heavy when the mass is at least 100.
  4. Return Both when both marks are set.
  5. Return Bulky or Heavy when only one mark is set.
  6. Return Neither when neither mark is set.

Code Implementation

class Solution:
    def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
        is_bulky = False
        is_heavy = False

        volume = length * width * height

        # First, we must determine if the box meets the criteria for being 'Bulky'.
        # This is true if any dimension is large or if the volume is large.
        if length >= 10000 or width >= 10000 or height >= 10000 or volume >= 1000000000:
            is_bulky = True

        # Independently, we check if the box meets the 'Heavy' criteria based on its mass.
        if mass >= 100:
            is_heavy = True

        # We combine the two independent checks to determine the final category.
        if is_bulky and is_heavy:
            return "Both"
        elif is_bulky:
            return "Bulky"
        elif is_heavy:
            return "Heavy"
        else:
            return "Neither"

Big(O) Analysis

Time Complexity
O(1) – The volume is one multiplication of three numbers. Each cutoff is then a single comparison. Four branches choose the label. There is no loop and no search. The work stays constant for every box.
Space Complexity
O(1) – The volume and the two flags are the only extra values. Nothing is allocated that grows with the inputs. The category string is the required answer. Extra memory stays constant.

Edge Cases

Every side and the mass are small
How to Handle:
The box is neither bulky nor heavy, so the label is Neither.
One side is at least 10000 and the mass is small
How to Handle:
The box is bulky only, so the label is Bulky.
The sides are under 10000 but the volume reaches 1000000000
How to Handle:
Volume alone makes the box bulky, so the label is Bulky.
The mass is at least 100 and the box is not bulky
How to Handle:
The label is Heavy.
The box is both bulky and heavy
How to Handle:
The label is Both.