Fizzbuzz Go
Fast recall
Quick revision
Core answer
Whether you understand Fizzbuzz Go beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The solution, executed:
func fizzbuzz(n int) []string {
out := make([]string, n)
for i := 1; i <= n; i++ {
switch {
case i%15 == 0: out[i-1] = "FizzBuzz"
case i%3 == 0: out[i-1] = "Fizz"
case i%5 == 0: out[i-1] = "Buzz"
default: out[i-1] = fmt.Sprint(i)
}
}
return out
}
// fizzbuzz(7) → [1 2 Fizz 4 Buzz Fizz 7] — verifiedThe details interviewers check:
- 1The 15-before-3-and-5 order — i%15 must be checked first (or i%3 && i%5)
- 2The switch form (cleaner than if-else chains)
- 3No off-by-one in the output indexing
- 4fmt.Sprint for the number case (or strconv.Itoa)
The variants that probe deeper:
- The string-concat form (s := ""; if i%3==0 { s += "Fizz" }; ...) — handles the 15 case without a special case
- The map-driven form (map[int]string{3:"Fizz", 5:"Buzz", 15:"FizzBuzz"}) — extensible to arbitrary rules
- The performance question (irrelevant — the point is correctness, not speed)
What it actually tests: can you write a loop, a conditional, a modulo, and string/number conversion — the minimal coding bar. The candidate's problems are always: wrong order (15 missed), off-by-one, or hardcoded output.
Fizzbuzz Go
Direct Answer
Loop 1 to n: divisible by 15 → "FizzBuzz", by 3 → "Fizz", by 5 → "Buzz", else the number. Tests basic control flow, modulo, string building — the screen for 'can you code at all.'
Deep Dive & Architecture
The switch evaluation: Go's switch with no expression evaluates each case's condition top-down — the 15 case first guarantees it wins when both 3 and 5 divide. An if-else chain needs the same discipline (the else-if for 15 before the 3 and 5).
The concat variant's elegance: appending to the string independently for 3 and 5 handles the 15 case by construction (both appends fire → "FizzBuzz") — no special case. The cost: string concatenation allocations — irrelevant at FizzBuzz scale, but the pattern (build the result from independent conditions) generalizes to rule engines.
The Go-specific idiom: fmt.Sprint(i) is the concise number-to-string; strconv.Itoa is the faster (no reflection) form. In FizzBuzz, either; in a hot path, Itoa. The interview honesty: this question is about zero mistakes on the basics — not the clever variant. Get the switch right first.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Fizzbuzz Go in a real project?
- How would you compare Fizzbuzz Go with an alternative?
- What is a common mistake when using Fizzbuzz Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.