We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Categorize Box According to Criteria

Number: 2619

Difficulty: Easy

Paid? No

Companies: Zendesk


Problem Description

Given four integers representing the dimensions (length, width, height) and mass of a box, determine its category based on specific criteria. The box is considered "Bulky" if any of its dimensions is at least 10^4 or if its volume (length * width * height) is at least 10^9. The box is considered "Heavy" if its mass is at least 100. Depending on whether one or both conditions are met, or neither, return "Both", "Heavy", "Bulky", or "Neither" respectively.


Key Insights

  • Check if any of the dimensions is greater than or equal to 10,000.
  • Calculate the volume and check if it is greater than or equal to 1,000,000,000.
  • Use these checks to determine whether the box is "Bulky".
  • Check if the mass is greater than or equal to 100 to determine if it's "Heavy".
  • Combine the results of the two checks to decide the final category.

Space and Time Complexity

Time Complexity: O(1) – Only a fixed number of operations are performed. Space Complexity: O(1) – Only a constant amount of extra space is used.


Solution

We begin by checking if the box is considered "Bulky". This can be done by verifying if any dimension is at least 10,000 or if the computed volume (length multiplied by width multiplied by height) is at least 1,000,000,000. Next, we check if the box is "Heavy" by comparing its mass to 100. Finally, based on the outcomes of these checks, we return:

  • "Both" if the box is both "Bulky" and "Heavy".
  • "Heavy" if it's only heavy.
  • "Bulky" if it's only bulky.
  • "Neither" if it meets neither condition. The solution relies solely on simple conditional checks and arithmetic operations.

Code Solutions

# Define the function to categorize the box according to criteria
def categorize_box(length, width, height, mass):
    # Check if any dimension is greater or equal to 10^4
    is_bulky_dimension = (length >= 10000 or width >= 10000 or height >= 10000)
    # Check if the volume is greater or equal to 10^9
    volume = length * width * height
    is_bulky_volume = (volume >= 10**9)
    # The box is bulky if either the dimension or volume condition is met
    is_bulky = is_bulky_dimension or is_bulky_volume
    
    # Check if the box is heavy by comparing mass to 100
    is_heavy = (mass >= 100)
    
    # Determine the final category based on bulky and heavy conditions
    if is_bulky and is_heavy:
        return "Both"
    elif is_bulky:
        return "Bulky"
    elif is_heavy:
        return "Heavy"
    else:
        return "Neither"

# Example usage:
print(categorize_box(1000, 35, 700, 300))  # Output: Heavy
← Back to All Questions