Skip to main content

Categorize Box According to Criteria - Solution & Explanation

EasyMath18 min readAsked at: Zendesk
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You receive the length, width, height, and mass of a box. The task is to classify it as Bulky, Heavy, Both, or Neither based on size and weight rules. A box is bulky if any dimension is at least 10,000 or its volume is at least 1,000,000,000. It is heavy if its mass is at least 100.

Approach 1: Simple Conditional Checks (O(1) time, O(1) space)

This approach directly translates the problem rules into conditional checks. First compute the box volume using length * width * height. Then determine two boolean flags: one for the bulky condition (dimension ≥ 10000 or volume ≥ 1e9) and another for the heavy condition (mass ≥ 100). Once those flags are known, use a small set of if conditions to return "Both", "Bulky", "Heavy", or "Neither". Since only a few arithmetic operations and comparisons are performed, the algorithm runs in constant time and uses constant space.

This solution is essentially a rule-based classification problem from math and simulation. The main goal is translating constraints into precise boolean logic without missing edge cases.

Approach 2: Short-Circuit Evaluation and Combined Checks (O(1) time, O(1) space)

This variation keeps the same logic but reduces branching by combining conditions using boolean expressions. Instead of multiple nested checks, compute isBulky and isHeavy in a single expression each. Short-circuit evaluation ensures unnecessary checks are skipped once a condition becomes true. For example, the bulky condition can be evaluated as (length ≥ 10000 || width ≥ 10000 || height ≥ 10000 || volume ≥ 1e9).

After evaluating the two flags, return the correct label using compact logic such as checking both flags first, then each individual flag. The complexity remains constant, but the code becomes shorter and easier to scan during interviews. This pattern is common in problems involving rule evaluation and logical grouping.

Recommended for interviews: The simple conditional checks approach is exactly what interviewers expect. It shows you correctly interpret constraints and translate them into clean boolean logic. The short-circuit variant is slightly cleaner and demonstrates comfort with concise condition evaluation, but both run in O(1) time and O(1) space.

Approach 1: Simple Conditional Checks

This approach involves straightforward conditional checks to determine whether a box is "Bulky" or "Heavy". For the "Bulky" condition, check if any dimension is >= 10,000 or if the volume (length * width * height) is >= 1,000,000,000. Then, check if the mass is >= 100 for the "Heavy" condition. Based on these conditions, deduce the category of the box.

This C solution checks whether a box is "Bulky" based on its dimensions and volume, and whether it is "Heavy" based on its mass. It then uses if-else statements to determine the category of the box based on these flags. This approach makes use of a long data type to handle potential overflow in volume calculation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because we only perform basic arithmetic operations and comparisons.
Space Complexity: O(1) since no extra space is used apart from a few variables.

Try this approach in the editor →

Approach 2: Short-Circuit Evaluation and Combined Checks

This approach uses short-circuit evaluation to efficiently determine if the box is "Bulky" or "Heavy" by combining checks into a single return statement. The approach leverages logical operators to decide the category in a more concise manner.

This solution in C evaluates whether the box is "Bulky" or "Heavy" through single-line conditional checks using the ternary operator. Logical and relational operations help form a single statement for determining the outcome.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation

We can simulate according to the problem description.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 4: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Conditional Checks

Time Complexity: O(1) because we only perform basic arithmetic operations and comparisons.
Space Complexity: O(1) since no extra space is used apart from a few variables.

Short-Circuit Evaluation and Combined Checks

Time Complexity: O(1).
Space Complexity: O(1).

Simulation
Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Conditional ChecksO(1)O(1)Best for clarity and interviews where translating problem rules into conditions is expected
Short-Circuit Evaluation and Combined ChecksO(1)O(1)Useful when writing compact logic with fewer branches and cleaner boolean expressions

Video Solution

LeetCode 2525. Categorize Box According to Criteria - Interview Prep Ep 129Fisher Coder337 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Categorize Box According to Criteria easy or hard?
Categorize Box According to Criteria is an Easy-level problem. The challenge is understanding the classification rules and implementing them correctly with condition checks, not designing complex algorithms.
Categorize Box According to Criteria Python/Java solution
Both Python and Java implementations follow the same idea: compute volume, evaluate bulky and heavy conditions with boolean expressions, and return the correct string result. Since the algorithm is constant time, the code is typically just a few conditional statements.
How to solve Categorize Box According to Criteria in O(1)?
Calculate the volume using length × width × height. Create two boolean flags: one for the bulky condition (any dimension ≥ 10000 or volume ≥ 1,000,000,000) and another for the heavy condition (mass ≥ 100). Use simple conditional logic to return "Both", "Bulky", "Heavy", or "Neither".
What is the best approach for Categorize Box According to Criteria?
The best approach is using simple conditional checks. Compute the volume, evaluate whether the box is bulky (dimension ≥ 10000 or volume ≥ 1e9) and whether it is heavy (mass ≥ 100), then return the correct label. The solution runs in O(1) time and O(1) space because it only performs constant arithmetic and comparisons.
Is Categorize Box According to Criteria asked at Google/Amazon/Meta?
This type of rule-based classification problem appears frequently in interviews at companies like Amazon and Google for early-stage screening or easy-level rounds. It tests careful reading of constraints and precise conditional logic rather than complex algorithms.
What data structure is used in Categorize Box According to Criteria?
No special data structure is required. The solution relies on basic arithmetic calculations and boolean variables to evaluate the bulky and heavy conditions.
What is the time complexity of Categorize Box According to Criteria?
The time complexity is O(1). The algorithm performs a fixed number of arithmetic operations and conditional checks regardless of input values. No loops, recursion, or additional data structures are required.

Ready to solve this problem?

Practice Categorize Box According to Criteria with our built-in code editor and test cases.

Practice on FleetCode