Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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.
"Bulky" if:
104.109.100, it is "Heavy"."Bulky" and "Heavy", then its category is "Both"."Bulky" nor "Heavy", then its category is "Neither"."Bulky" but not "Heavy", then its category is "Bulky"."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 <= 103Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1000 35 700 300
200 50 800 50
fizz-buzz)find-winner-on-a-tic-tac-toe-game)best-poker-hand)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2525: Categorize Box According to Criteria
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];
}
}
// Accepted solution for LeetCode #2525: Categorize Box According to Criteria
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]
}
# Accepted solution for LeetCode #2525: Categorize Box According to Criteria
class Solution:
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
v = length * width * height
bulky = int(any(x >= 10000 for x in (length, width, height)) or v >= 10**9)
heavy = int(mass >= 100)
i = heavy << 1 | bulky
d = ['Neither', 'Bulky', 'Heavy', 'Both']
return d[i]
// Accepted solution for LeetCode #2525: Categorize Box According to Criteria
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()
}
}
// Accepted solution for LeetCode #2525: Categorize Box According to Criteria
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];
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.