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.
"Bulky" if:
104.109.100, it is "Heavy"."Bulky" and "Heavy", then its category is "Both"."Bulky" nor "Heavy", then its category is "Neither"."Bulky" but not "Heavy", then its category is "Bulky"."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 <= 1051 <= mass <= 103When 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:
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:
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"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:
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"| Case | How to Handle |
|---|---|
| Every side and the mass are small | The box is neither bulky nor heavy, so the label is Neither. |
| One side is at least 10000 and the mass is small | The box is bulky only, so the label is Bulky. |
| The sides are under 10000 but the volume reaches 1000000000 | Volume alone makes the box bulky, so the label is Bulky. |
| The mass is at least 100 and the box is not bulky | The label is Heavy. |
| The box is both bulky and heavy | The label is Both. |