Categorize Box According to Criteria solution and explanation
9 more videos on this problem
LeetCode 2525. Categorize Box According to Criteria - Interview Prep Ep 129Fisher Coder · 9:37 · 337 views
2525. Categorize Box According to Criteria | Biweekly Contest 95 | LeetCode | DCC NIT-ADCC NITA · 4:49 · 242 views
2525. Categorize Box According to Criteria (Leetcode Easy)Programming Live with Larry · 3:48 · 216 views
Leetcode Biweekly Contest 95| PROBLEM 2525, 2526, 2527code Explainer · 12:27 · 184 views
Categorize Box According to Criteria | Leetcode 2525 | Leetcode BiWeekly Contest 95Tech Courses · 3:37 · 181 views
2525 Categorize Box According to Criteria | LeetCode | C++Deep Tech · 3:48 · 175 views
Categorize Box According to Criteria | Leetcode Contest Biweekly 95 | Accepted SolutionYourhelpingFriend · 0:08 · 102 views
Categorize Box According to Criteria Biweekly Contest 95 LeetCode Solution with Explanation in C++Learn to Code · 7:35 · 84 views
2525. Categorize Box According to Criteria with Python | Best Explanation | Leetcode with PythonArsalan Ahmed · 6:35 · 67 views
Categorize Box According to Criteria Leetcode | Leetcode Question 2525 | Easy (Hindi) | JavascriptSolving It · 5:58 · 66 views
The problem
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.
- Any of the dimensions of the box is greater or equal to
- 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 <= 1051 <= 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.
def categorize_box(length, width, height, mass):
volume = length * width * height
bulky = length >= 10000 or width >= 10000 or height >= 10000 or volume >= 1000000000
heavy = mass >= 100
if bulky and heavy:
return "Both"
if bulky:
return "Bulky"
if heavy:
return "Heavy"
return "Neither"
if __name__ == "__main__":
print(categorize_box(1000, 35, 700, 300)) # Output: "Heavy"
print(categorize_box(200, 50, 800, 50)) # Output: "Neither"public class CategorizeBox {
public static String categorizeBox(int length, int width, int height, int mass) {
long volume = (long) length * width * height;
boolean bulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000L;
boolean heavy = mass >= 100;
if (bulky && heavy) return "Both";
if (bulky) return "Bulky";
if (heavy) return "Heavy";
return "Neither";
}
public static void main(String[] args) {
System.out.println(categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
System.out.println(categorizeBox(200, 50, 800, 50)); // Output: "Neither"
}
}#include <iostream>
using namespace std;
string categorizeBox(int length, int width, int height, int mass) {
long long volume = (long long)length * width * height;
bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000;
bool heavy = mass >= 100;
if (bulky && heavy) return "Both";
if (bulky) return "Bulky";
if (heavy) return "Heavy";
return "Neither";
}
int main() {
cout << categorizeBox(1000, 35, 700, 300) << endl; // Output: "Heavy"
cout << categorizeBox(200, 50, 800, 50) << endl; // Output: "Neither"
return 0;
}function categorizeBox(length, width, height, mass) {
let volume = length * width * height;
let bulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000;
let heavy = mass >= 100;
if (bulky && heavy) return "Both";
if (bulky) return "Bulky";
if (heavy) return "Heavy";
return "Neither";
}
console.log(categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
console.log(categorizeBox(200, 50, 800, 50)); // Output: "Neither";#include <stdio.h>
char* categorizeBox(int length, int width, int height, int mass) {
long volume = (long)length * width * height;
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000;
int heavy = mass >= 100;
if (bulky && heavy) return "Both";
if (bulky) return "Bulky";
if (heavy) return "Heavy";
return "Neither";
}
int main() {
printf("%s\n", categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
printf("%s\n", categorizeBox(200, 50, 800, 50)); // Output: "Neither"
return 0;
}using System;
public class CategorizeBox {
public static string CategorizeBoxMethod(int length, int width, int height, int mass) {
long volume = (long)length * width * height;
bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000;
bool heavy = mass >= 100;
if (bulky && heavy) return "Both";
if (bulky) return "Bulky";
if (heavy) return "Heavy";
return "Neither";
}
public static void Main() {
Console.WriteLine(CategorizeBoxMethod(1000, 35, 700, 300)); // Output: "Heavy"
Console.WriteLine(CategorizeBoxMethod(200, 50, 800, 50)); // Output: "Neither"
}
}In this Python solution, we calculate the volume and use it along with the dimensions and mass to determine "Bulky" and "Heavy". Conditional statements are used to return the category of the box. Python's handling of integers avoids overflow issues.
Time Complexity: O(1).
Space Complexity: O(1).
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.
def categorize_box(length, width, height, mass):
volume = length * width * height
is_bulky = length >= 10000 or width >= 10000 or height >= 10000 or volume >= 1000000000
is_heavy = mass >= 100
return "Both" if is_bulky and is_heavy else "Bulky" if is_bulky else "Heavy" if is_heavy else "Neither"
if __name__ == "__main__":
print(categorize_box(1000, 35, 700, 300)) # Output: "Heavy"
print(categorize_box(200, 50, 800, 50)) # Output: "Neither"public class CategorizeBox {
public static String categorizeBox(int length, int width, int height, int mass) {
long volume = (long) length * width * height;
boolean isBulky = (length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000L);
boolean isHeavy = (mass >= 100);
return (isBulky && isHeavy) ? "Both" :
(isBulky) ? "Bulky" :
(isHeavy) ? "Heavy" : "Neither";
}
public static void main(String[] args) {
System.out.println(categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
System.out.println(categorizeBox(200, 50, 800, 50)); // Output: "Neither"
}
}#include <iostream>
using namespace std;
string categorizeBox(int length, int width, int height, int mass) {
long long volume = (long long)length * width * height;
bool isBulky = (length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000);
bool isHeavy = (mass >= 100);
return (isBulky && isHeavy) ? "Both" :
(isBulky) ? "Bulky" :
(isHeavy) ? "Heavy" : "Neither";
}
int main() {
cout << categorizeBox(1000, 35, 700, 300) << endl; // Output: "Heavy"
cout << categorizeBox(200, 50, 800, 50) << endl; // Output: "Neither"
return 0;
}function categorizeBox(length, width, height, mass) {
let volume = length * width * height;
let isBulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000;
let isHeavy = mass >= 100;
return (isBulky && isHeavy) ? "Both" :
(isBulky) ? "Bulky" :
(isHeavy) ? "Heavy" : "Neither";
}
console.log(categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
console.log(categorizeBox(200, 50, 800, 50)); // Output: "Neither";#include <stdio.h>
char* categorizeBox(int length, int width, int height, int mass) {
long volume = (long)length * width * height;
int isBulky = (length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000);
int isHeavy = (mass >= 100);
return (isBulky && isHeavy) ? "Both" :
(isBulky) ? "Bulky" :
(isHeavy) ? "Heavy" : "Neither";
}
int main() {
printf("%s\n", categorizeBox(1000, 35, 700, 300)); // Output: "Heavy"
printf("%s\n", categorizeBox(200, 50, 800, 50)); // Output: "Neither"
return 0;
}using System;
public class CategorizeBox {
public static string CategorizeBoxMethod(int length, int width, int height, int mass) {
long volume = (long)length * width * height;
bool isBulky = (length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000);
bool isHeavy = (mass >= 100);
return (isBulky && isHeavy) ? "Both" :
(isBulky) ? "Bulky" :
(isHeavy) ? "Heavy" : "Neither";
}
public static void Main() {
Console.WriteLine(CategorizeBoxMethod(1000, 35, 700, 300)); // Output: "Heavy"
Console.WriteLine(CategorizeBoxMethod(200, 50, 800, 50)); // Output: "Neither"
}
}This Python solution employs a compact if-else structure with logical operators to determine whether the box is "Bulky", "Heavy", or both, and returns the appropriate category efficiently.
Time Complexity: O(1).
Space Complexity: O(1).
Approach 3: Simulation
We can simulate according to the problem description.
The time complexity is O(1), and the space complexity is O(1).
function categorizeBox(length: number, width: number, height: number, mass: number): string {
const v = length * width * height;
let i = 0;
if (length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000) {
i |= 1;
}
if (mass >= 100) {
i |= 2;
}
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
}func categorizeBox(length int, width int, height int, mass int) string {
v := length * width * height
i := 0
if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
i |= 1
}
if mass >= 100 {
i |= 2
}
d := [4]string{"Neither", "Bulky", "Heavy", "Both"}
return d[i]
}impl Solution {
pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String {
let v = (length as i64) * (width as i64) * (height as i64);
let mut i = 0;
if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
i |= 1;
}
if mass >= 100 {
i |= 2;
}
let d = vec!["Neither", "Bulky", "Heavy", "Both"];
d[i].to_string()
}
}Approach 4: Default Approach
function categorizeBox(length: number, width: number, height: number, mass: number): string {
const v = length * width * height;
const bulky = length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9;
const heavy = mass >= 100;
if (bulky && heavy) {
return 'Both';
}
if (bulky) {
return 'Bulky';
}
if (heavy) {
return 'Heavy';
}
return 'Neither';
}func categorizeBox(length int, width int, height int, mass int) string {
v := length * width * height
bulky := length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9
heavy := mass >= 100
if bulky && heavy {
return "Both"
}
if bulky {
return "Bulky"
}
if heavy {
return "Heavy"
}
return "Neither"
}impl Solution {
pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String {
let v = length * width * height;
let bulky = length >= 10000
|| width >= 10000
|| height >= 10000
|| (length as i64) * (width as i64) * (height as i64) >= 1000000000;
let heavy = mass >= 100;
if bulky && heavy {
return "Both".to_string();
}
if bulky {
return "Bulky".to_string();
}
if heavy {
return "Heavy".to_string();
}
"Neither".to_string()
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Simple Conditional Checks | Time Complexity: O(1). |
| Short-Circuit Evaluation and Combined Checks | Time Complexity: O(1). |
| Simulation | – |
| Default Approach | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Conditional Checks | O(1) | O(1) | Best for clarity and interviews where translating problem rules into conditions is expected |
| Short-Circuit Evaluation and Combined Checks | O(1) | O(1) | Useful when writing compact logic with fewer branches and cleaner boolean expressions |
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.
