const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Fizz Buzz: LeetCode 412 Solution Explained

The most famous coding interview screening question in history — simple on the surface, but interviewers use it to test whether you can write clean, extensible code under pressure.

5 min read|

The question that 95% of candidates reportedly cannot solve

Fizz Buzz — simple problem, but your code quality is what interviewers are really watching

Fizz Buzz: The Most Famous Coding Question Ever

Fizz Buzz is the most famous coding interview question in the history of software engineering. LeetCode #412 formalizes a problem that has been used as a screening question since at least the early 2000s — and the claim that "199 out of 200 applicants for every programming job can't write code at all" made it legendary in 2007.

The problem is deceptively simple: given an integer n, return a string array where each element represents the numbers from 1 to n, but multiples of 3 become "Fizz", multiples of 5 become "Buzz", and multiples of both become "FizzBuzz". That is the entire problem statement.

So why does fizz buzz leetcode remain one of the most searched interview questions a decade later? Because interviewers are not testing whether you can solve it — they are testing whether you can write clean, readable, extensible code under pressure. The difference between a passing answer and a great answer is design quality.

Understanding the Fizz Buzz Problem

The rules are straightforward. For every integer from 1 to n: if the number is divisible by both 3 and 5, output "FizzBuzz". If divisible by only 3, output "Fizz". If divisible by only 5, output "Buzz". Otherwise, output the number itself as a string.

Divisibility is checked with the modulo operator. If i % 3 === 0, the number is divisible by 3. If i % 15 === 0, it is divisible by both 3 and 5 — because 15 is the least common multiple of 3 and 5.

For n = 15 the output is: "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz". Notice that 15 is the first number that triggers the combined "FizzBuzz" output.

ℹ️

Did You Know

Fizz Buzz is reportedly the most common screening question in software engineering history — the claim that "95% of programming job candidates can't solve it" made it famous in 2007.

The Classic Fizz Buzz Solution

The most common approach uses a simple if-elif-else chain with modulo checks. Loop from 1 to n. For each number, check divisibility by 15 first, then by 3, then by 5, and fall through to the number itself. The order of checks is critical.

Why check 15 before 3 or 5? Because in an if-elif chain, only the first matching condition executes. If you check i % 3 === 0 first, a number like 15 — which is divisible by both 3 and 5 — will match the first condition and output "Fizz" instead of "FizzBuzz". This is the single most common mistake candidates make.

The time complexity is O(n) because you iterate through every number exactly once. The space complexity is O(1) if you exclude the output array — each iteration does a constant amount of work with modulo checks and string assignment.

This leetcode 412 solution is perfectly acceptable in an interview. It is correct, readable, and runs in optimal time. But there is a more elegant approach that interviewers love to see.

  • Time complexity: O(n) — one pass through all numbers from 1 to n
  • Space complexity: O(1) auxiliary — only the output array grows
  • Check divisibility by 15 BEFORE checking 3 and 5 separately
  • The modulo operator (%) is the core tool — i % k === 0 means i is divisible by k

The Extensible Fizz Buzz Solution

The string concatenation approach is what separates good candidates from great ones. Instead of checking 15 as a special case, you build the output string incrementally. Start with an empty string. If the number is divisible by 3, append "Fizz". If divisible by 5, append "Buzz". If the string is still empty after all checks, use the number itself.

This approach is elegant because it scales without nested conditionals. Imagine the interviewer says: "Now add Bazz for multiples of 7." With the classic approach, you would need to check 3*5, 3*7, 5*7, and 3*5*7 — an explosion of conditions. With string concatenation, you just add one more line: if divisible by 7, append "Bazz". Done.

The time and space complexity remain the same — O(n) time and O(1) auxiliary space. But the code is more maintainable, more readable, and demonstrates that you think about extensibility. In a real codebase, this is the version your team would thank you for writing.

Some interviewers take this further and ask you to use a list of (divisor, word) pairs that you iterate over. This makes the solution fully data-driven — you can add new rules without touching the logic at all. It is the fizz buzz solution that shows software engineering maturity.

💡

Pro Tip

The extensible approach: start with empty string, append "Fizz" if divisible by 3, append "Buzz" if divisible by 5. If string is empty, use the number. This scales cleanly to FizzBuzzBazz without nested ifs.

Visual Walkthrough of Fizz Buzz

Walking through the first 15 numbers makes the pattern crystal clear. For each number, apply the rules and observe how the modulo operator drives the output.

Numbers 1 and 2 are not divisible by 3 or 5, so they output "1" and "2". Number 3 is the first multiple of 3 — output "Fizz". Number 4 passes through — output "4". Number 5 is the first multiple of 5 — output "Buzz".

The pattern continues: 6 gives "Fizz" (6 % 3 === 0), 7 through 9 give "7", "8", "Fizz". Then 10 gives "Buzz" (10 % 5 === 0), 11 through 14 give "11", "Fizz", "13", "14". Finally 15 gives "FizzBuzz" — the first number divisible by both 3 and 5.

Notice the rhythm: "Fizz" appears every 3 numbers, "Buzz" every 5, and "FizzBuzz" every 15. This regularity is what makes the problem predictable — and what makes forgetting the 15-check so unforgivable in an interview.

  1. 11 -> "1" (not divisible by 3 or 5)
  2. 22 -> "2" (not divisible by 3 or 5)
  3. 33 -> "Fizz" (3 % 3 === 0)
  4. 44 -> "4" (not divisible by 3 or 5)
  5. 55 -> "Buzz" (5 % 5 === 0)
  6. 66 -> "Fizz" (6 % 3 === 0)
  7. 77-8 -> "7", "8"
  8. 89 -> "Fizz" (9 % 3 === 0)
  9. 910 -> "Buzz" (10 % 5 === 0)
  10. 1011 -> "11"
  11. 1112 -> "Fizz" (12 % 3 === 0)
  12. 1213-14 -> "13", "14"
  13. 1315 -> "FizzBuzz" (15 % 3 === 0 AND 15 % 5 === 0)

Edge Cases for Fizz Buzz

Fizz Buzz is simple enough that edge cases are minimal, but interviewers still expect you to mention them. Thinking about boundaries shows thoroughness even on easy problems.

When n = 1, the output is just ["1"] — no multiples of 3 or 5 exist yet. When n = 3, you get the first "Fizz". When n = 5, the first "Buzz" appears. And when n = 15, the first "FizzBuzz" shows up — this is the smallest input that exercises all four branches of your code.

What about n = 0? The problem constraints on LeetCode specify 1 <= n <= 10,000, so n = 0 is technically out of scope. But if asked, the correct answer is an empty array — there are no numbers from 1 to 0. Handling this gracefully shows defensive coding habits.

  • n = 1: Output is ["1"] — simplest valid input
  • n = 15: First input that includes a "FizzBuzz" — tests all branches
  • n = 0: Empty array (out of constraints but good to mention)
  • No negative numbers in the constraints — but your loop naturally handles this
⚠️

Common Mistake

Check divisibility by 15 BEFORE checking 3 and 5 separately — or use string concatenation. If you check 3 first in an if-elif chain, numbers divisible by both will only print "Fizz".

What Fizz Buzz Teaches About Clean Code

Fizz Buzz is not about algorithms — it is about code quality. The problem tests whether you understand the modulo operator, whether you structure conditionals correctly, and whether you think about extensibility before being asked. These are the habits that separate junior developers from senior ones.

The classic coding interview problem also teaches an important lesson about preparation. If you cannot write a correct fizz buzz solution in under two minutes, you are not ready for harder problems. It is the warm-up, the baseline, the minimum viable competency check.

Use Fizz Buzz as your canary. If you can solve it cleanly, explain the extensible version, and handle edge cases — you have the fundamentals locked in. From here, move on to problems like Two Sum, Contains Duplicate, and Valid Palindrome to build your pattern recognition with YeetCode flashcards.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now