412. Fizz Buzz
easyReturn a list of strings: 'Fizz' for multiples of 3, 'Buzz' for multiples of 5, 'FizzBuzz' for multiples of both, otherwise the number as a string. The legendary screening question — easy to write, easy to over-engineer.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
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.
Constraints
1 <= n <= 10^4
Examples
Example 1
n = 3["1","2","Fizz"]Example 2
n = 5["1","2","Fizz","4","Buzz"]Example 3
n = 15["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Order of checks matters: test 15 (i.e., both 3 AND 5) FIRST. If you check 3 alone first, multiples of 15 would print 'Fizz' instead of 'FizzBuzz'.
Hint 2
Equivalently: build the string by concatenating 'Fizz' if i % 3 == 0 and 'Buzz' if i % 5 == 0; if empty, use str(i). One pass, no nested if.
Hint 3
Avoid the modulo entirely with two counters mod 3 and mod 5: reset on hit. Micro-optimization but interviewers occasionally ask.
Hint 4
Loop 1..n, append result to a list, return.
Solution approach
Reveal approach
Loop i from 1 to n inclusive. For each i, determine the string by concatenation: piece = ''; if i % 3 == 0 then piece += 'Fizz'; if i % 5 == 0 then piece += 'Buzz'; if piece is empty, piece = str(i). Append piece to the result list. Return the list. O(n) time, O(n) space for the output. The concatenation pattern is one line shorter than the if/elif/elif/else cascade and naturally handles multiples of 15 — order doesn't matter because both checks fire. The modulo-free counter variant maintains f = 0 and b = 0; each iteration increments both; on f == 3 reset and append 'Fizz', on b == 5 reset and append 'Buzz'. Same O(n) but no division.
Complexity
- Time
- O(n)
- Space
- O(n)
Related patterns
- math
- simulation
Related problems
- 66. Plus One
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
- Microsoft
- Apple
Practice these live with InterviewChamp.AI
Drill Fizz Buzz and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →