Categorize Box According to Criteria

Easy
#2305Time: O(1) - The solution consists of a fixed number of arithmetic operations and comparisons. The runtime does not depend on the magnitude of the input values, making it constant time.Space: O(1) - The algorithm uses a constant amount of extra space for a few variables (two booleans and one long for volume), regardless of the input values.1 company
Patterns
Companies

Prompt

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

Approaches

1 approach with complexity analysis and trade-offs.

The problem asks us to categorize a box based on a set of rules related to its dimensions and mass. This can be solved by directly implementing the given logic. We first determine if the box qualifies as "Bulky" and if it qualifies as "Heavy" by checking their respective conditions. These two properties can be stored in boolean flags. Then, based on the combination of these two flags, we can use a simple conditional structure (if-else if-else) to return the correct category string.

Algorithm

  • Create two boolean flags, isBulky and isHeavy, initialized to false.
  • Evaluate the "Bulky" condition:
    • Check if length >= 10000, width >= 10000, or height >= 10000.
    • Calculate the volume using a long data type to prevent overflow: long volume = (long)length * width * height;.
    • Check if volume >= 10^9.
    • If any of the above conditions are true, set isBulky to true.
  • Evaluate the "Heavy" condition:
    • Check if mass >= 100.
    • If true, set isHeavy to true.
  • Use a series of if-else statements to determine the final category based on the boolean flags:
    • If isBulky and isHeavy are both true, return "Both".
    • If only isBulky is true, return "Bulky".
    • If only isHeavy is true, return "Heavy".
    • If neither is true, return "Neither".

Walkthrough

This approach is a direct simulation of the problem's requirements. It's the most straightforward and efficient way to solve the problem.

First, we need to determine the two primary properties of the box: whether it's "Bulky" and whether it's "Heavy".

  1. Bulky Check: A box is bulky if any of its dimensions (length, width, height) is at least 10,000, OR its volume is at least 10<sup>9</sup>. A critical detail here is that the dimensions can be up to 10<sup>5</sup>, so their product can be up to (10<sup>5</sup>)<sup>3</sup> = 10<sup>15</sup>. This value will overflow a standard 32-bit integer. Therefore, we must use a 64-bit integer type (long in Java) for the volume calculation.

  2. Heavy Check: A box is heavy if its mass is at least 100. This is a simple comparison.

After determining these two boolean states, we can map the four possible outcomes to the required category strings:

  • isBulky = true, isHeavy = true -> "Both"
  • isBulky = true, isHeavy = false -> "Bulky"
  • isBulky = false, isHeavy = true -> "Heavy"
  • isBulky = false, isHeavy = false -> "Neither"

This logic is perfectly suited for an if-else cascade.

Here is the Java implementation:

class Solution {    public String categorizeBox(int length, int width, int height, int mass) {        // Use long for volume to prevent integer overflow        long volume = (long) length * width * height;         // Determine if the box is Bulky        boolean isBulky = (length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000);         // Determine if the box is Heavy        boolean isHeavy = (mass >= 100);         // Categorize based on the boolean flags        if (isBulky && isHeavy) {            return "Both";        } else if (isBulky) {            return "Bulky";        } else if (isHeavy) {            return "Heavy";        } else {            return "Neither";        }    }}

Complexity

Time

O(1) - The solution consists of a fixed number of arithmetic operations and comparisons. The runtime does not depend on the magnitude of the input values, making it constant time.

Space

O(1) - The algorithm uses a constant amount of extra space for a few variables (two booleans and one long for volume), regardless of the input values.

Trade-offs

Pros

  • Optimal performance with constant time and space complexity.

  • The code is simple, readable, and a direct translation of the problem statement.

  • Easy to implement and debug.

Cons

  • A potential pitfall is integer overflow when calculating the volume. This must be handled by using a 64-bit integer type (long in Java).

Solutions

class Solution {public  String categorizeBox(int length, int width, int height, int mass) {    long v = (long)length * width * height;    int bulky =        length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000            ? 1            : 0;    int heavy = mass >= 100 ? 1 : 0;    String[] d = {"Neither", "Bulky", "Heavy", "Both"};    int i = heavy << 1 | bulky;    return d[i];  }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.