06 · Mindset & Problem Solving
#412 Fizz Buzz
frequently reported atNVIDIAAmazonBloombergMicrosoftGoogleApple
the problem
In plain words
Return a list of strings for 1..n: "FizzBuzz" if i is divisible by 3 and 5, "Fizz" if by 3, "Buzz" if by 5, otherwise i itself.
in n = 3
out ["1","2","Fizz"]
in n = 5
out ["1","2","Fizz","4","Buzz"]
in n = 15
out […, "14", "FizzBuzz"]
- 1 ≤ n ≤ 10⁴
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternSimulation
Order of checks matters: the most specific rule (divisible by both) must win. Building the word from parts avoids the special case entirely.
solutions
From brute force to optimal
Test 15, then 3, then 5, else the number.
- Loop i = 1..n.
- i % 15 == 0 → FizzBuzz; i % 3 → Fizz; i % 5 → Buzz; else String.valueOf(i).
Constant work per number.
The output list (O(1) extra).
class Solution { public List<String> fizzBuzz(int n) { List<String> answer = new ArrayList<>(n); for (int i = 1; i <= n; i++) { if (i % 15 == 0) answer.add("FizzBuzz"); // check the combined case FIRST else if (i % 3 == 0) answer.add("Fizz"); else if (i % 5 == 0) answer.add("Buzz"); else answer.add(String.valueOf(i)); } return answer; }}Dry run
Append "Fizz" if divisible by 3, then "Buzz" if by 5; empty → the number.
- StringBuilder per i.
- Each rule appends its word.
- Empty builder → the number.
Constant work per number.
The output list.
class Solution { public List<String> fizzBuzz(int n) { List<String> answer = new ArrayList<>(n); for (int i = 1; i <= n; i++) { StringBuilder sb = new StringBuilder(); if (i % 3 == 0) sb.append("Fizz"); // each rule adds its word... if (i % 5 == 0) sb.append("Buzz"); // ...so a new rule is one more line answer.add(sb.isEmpty() ? String.valueOf(i) : sb.toString()); } return answer; }}Count up to 3 and 5 and reset — no division at all.
- three++, five++ each step.
- three == 3 → "Fizz", reset; five == 5 → "Buzz", reset.
Constant work per number.
The output list.
class Solution { public List<String> fizzBuzz(int n) { List<String> answer = new ArrayList<>(n); int three = 0, five = 0; // count up instead of dividing for (int i = 1; i <= n; i++) { three++; five++; String word = ""; if (three == 3) { word += "Fizz"; three = 0; } if (five == 5) { word += "Buzz"; five = 0; } answer.add(word.isEmpty() ? String.valueOf(i) : word); } return answer; }}in the room
How to say it in the interview
talk track
- It's a warm-up: they're checking clean code, correct ordering and that you test n = 15.
- Offer the rule-building version as the extensible one: "adding 7 → Bazz is one line."
traps
- Checking i % 3 before i % 15 — 15 prints "Fizz".
- Starting the loop at 0 instead of 1.
what they ask next
Follow-ups
Make the rules configurable?
Keep a LinkedHashMap<Integer, String> of divisor → word (insertion order matters) and loop over it for each i.
before you submit
Edge cases to test
- n = 1 or 2: no Fizz or Buzz at all.
- 15, 30, 45…: divisible by both, so "FizzBuzz" — never just "Fizz".
- The list starts at "1", not "0".
practise next
Similar problems
- #1480Running Sum of 1d ArrayEasyin Mindset
- #2073Time Needed to Buy TicketsEasyin Queues