algoadvance

You are given a box with four parameters: length, width, height, and mass. Your task is to categorize the box based on the given criteria:

  1. Bulky: A box is considered bulky if any of the dimensions (length, width, or height) of the box is greater than or equal to (10^4) or if the volume of the box is greater than or equal to (10^9).

  2. Heavy: A box is considered heavy if its mass is greater than or equal to 100.

  3. Categories:

    • The box can belong to multiple categories.
    • It can be categorized as:
      • “Bulky” (if the box is bulky but not heavy)
      • “Heavy” (if the box is heavy but not bulky)
      • “Both” (if the box is both bulky and heavy)
      • “Neither” (if the box is neither bulky nor heavy)

You need to implement a function categorizeBox(length, width, height, mass) that returns the appropriate category of the box.

Clarifying Questions

  1. Should the values for dimensions always be positive numbers?
  2. Will the mass always be a positive number?
  3. Are we allowed to assume the inputs are integers?

Code

We’ll use simple conditional checks to determine the category of the box.

Strategy

  1. Check if the box is “Bulky”:
    • This can be done by comparing each dimension against (10^4) and calculating the volume.
  2. Check if the box is “Heavy”:
    • This can be done by directly comparing the mass to 100.
  3. Use the results of the above checks to determine and return the category:
    • If both “Bulky” and “Heavy” are true, return “Both”
    • If only “Bulky” is true, return “Bulky”
    • If only “Heavy” is true, return “Heavy”
    • If neither is true, return “Neither”

Time Complexity

def categorizeBox(length, width, height, mass):
    bulky = length >= 10**4 or width >= 10**4 or height >= 10**4 or (length * width * height) >= 10**9
    heavy = mass >= 100

    if bulky and heavy:
        return "Both"
    elif bulky:
        return "Bulky"
    elif heavy:
        return "Heavy"
    else:
        return "Neither"

Example

# Example test cases
print(categorizeBox(10000, 2, 2, 50))    # Output: Bulky
print(categorizeBox(100, 100, 100, 150)) # Output: Heavy
print(categorizeBox(10000, 10000, 1, 200)) # Output: Both
print(categorizeBox(1, 1, 1, 1))         # Output: Neither

This code efficiently categorizes the box according to the given criteria using simple conditional checks.

Try our interview co-pilot at AlgoAdvance.com