Back to blog
Oct 29, 2024
3 min read

Categorize Box According to Criteria

Determine if a box is Bulky, Heavy, Both, or Neither based on its dimensions and mass.

Difficulty: Easy | Acceptance: 39.00% | Paid: No Topics: Math

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 10⁴.
  • Or, the volume of the box is greater or equal to 10⁹.

If the mass of the box is greater or equal to 100, it is “Heavy”. If the box is both “Bulky” and “Heavy”, then you must return “Both”. If the box is neither “Bulky” nor “Heavy”, then you must return “Neither”. If the box is “Bulky” but not “Heavy”, then you must return “Bulky”. If the box is “Heavy” but not “Bulky”, then you must return “Heavy”.

Note that the volume of the box is calculated as length * width * height.

Examples

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 10^4. Its volume = 24500000 <= 10^9. 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 10^4. Its volume = 8 * 10^6 <= 10^9. 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 <= 10^5
- 1 <= mass <= 10^3

Direct Conditional Logic

Intuition We simply evaluate the conditions for “Bulky” and “Heavy” independently using boolean flags, then combine the results to determine the final category string.

Steps

  • Calculate the volume of the box. Note that in languages like Java or C++, the product of three integers up to 10⁴ can exceed the limit of a 32-bit integer (2³¹ - 1), so we must use a 64-bit integer type (long/long long) for the volume calculation.
  • Check if the box is “Bulky” by verifying if the volume is greater than or equal to 10⁹ OR if any of the dimensions (length, width, height) is greater than or equal to 10⁴.
  • Check if the box is “Heavy” by verifying if the mass is greater than or equal to 100.
  • Use conditional logic to return “Both”, “Bulky”, “Heavy”, or “Neither” based on the boolean flags.
python
class Solution:
    def categorizeBox(self, length: int, width: int, height: int, mass: int) -&gt; str:
        is_bulky = False
        is_heavy = False
        
        volume = length * width * height
        
        if volume &gt;= 1000000000 or length &gt;= 10000 or width &gt;= 10000 or height &gt;= 10000:
            is_bulky = True
            
        if mass &gt;= 100:
            is_heavy = True
            
        if is_bulky and is_heavy:
            return "Both"
        if is_bulky:
            return "Bulky"
        if is_heavy:
            return "Heavy"
        return "Neither"

Complexity

  • Time: O(1)
  • Space: O(1)
  • Notes: The solution involves a constant number of arithmetic operations and comparisons. The only potential pitfall is integer overflow in languages with fixed-size integers, which is handled by using 64-bit integers.