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 an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz" if i is divisible by 3 and 5.answer[i] == "Fizz" if i is divisible by 3.answer[i] == "Buzz" if i is divisible by 5.answer[i] == i (as a string) if none of the above conditions are true.Example 1:
Input: n = 3 Output: ["1","2","Fizz"]
Example 2:
Input: n = 5 Output: ["1","2","Fizz","4","Buzz"]
Example 3:
Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints:
1 <= n <= 104Problem summary: Given an integer n, return a string array answer (1-indexed) where: answer[i] == "FizzBuzz" if i is divisible by 3 and 5. answer[i] == "Fizz" if i is divisible by 3. answer[i] == "Buzz" if i is divisible by 5. answer[i] == i (as a string) if none of the above conditions are true.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
3
5
15
fizz-buzz-multithreaded)categorize-box-according-to-criteria)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #412: Fizz Buzz
class Solution {
public List<String> fizzBuzz(int n) {
List<String> ans = new ArrayList<>();
for (int i = 1; i <= n; ++i) {
String s = "";
if (i % 3 == 0) {
s += "Fizz";
}
if (i % 5 == 0) {
s += "Buzz";
}
if (s.length() == 0) {
s += i;
}
ans.add(s);
}
return ans;
}
}
// Accepted solution for LeetCode #412: Fizz Buzz
func fizzBuzz(n int) []string {
ans := make([]string, 0, n)
for i := 1; i < n+1; i++ {
switch {
case i%15 == 0:
ans = append(ans, "FizzBuzz")
case i%3 == 0:
ans = append(ans, "Fizz")
case i%5 == 0:
ans = append(ans, "Buzz")
default:
ans = append(ans, strconv.Itoa(i))
}
}
return ans
}
# Accepted solution for LeetCode #412: Fizz Buzz
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans = []
for i in range(1, n + 1):
if i % 15 == 0:
ans.append('FizzBuzz')
elif i % 3 == 0:
ans.append('Fizz')
elif i % 5 == 0:
ans.append('Buzz')
else:
ans.append(str(i))
return ans
// Accepted solution for LeetCode #412: Fizz Buzz
struct Solution;
impl Solution {
fn fizz_buzz(n: i32) -> Vec<String> {
let mut res = vec![];
for i in 1..=n {
let fizz = i % 3 == 0;
let buzz = i % 5 == 0;
let s = match (fizz, buzz) {
(true, true) => "FizzBuzz".to_string(),
(true, false) => "Fizz".to_string(),
(false, true) => "Buzz".to_string(),
(false, false) => format!("{}", i),
};
res.push(s);
}
res
}
}
#[test]
fn test() {
let output: Vec<String> = vec_string![
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14",
"FizzBuzz"
];
assert_eq!(Solution::fizz_buzz(15), output);
}
// Accepted solution for LeetCode #412: Fizz Buzz
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #412: Fizz Buzz
// class Solution {
// public List<String> fizzBuzz(int n) {
// List<String> ans = new ArrayList<>();
// for (int i = 1; i <= n; ++i) {
// String s = "";
// if (i % 3 == 0) {
// s += "Fizz";
// }
// if (i % 5 == 0) {
// s += "Buzz";
// }
// if (s.length() == 0) {
// s += i;
// }
// ans.add(s);
// }
// return ans;
// }
// }
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.