Write a program that generates a list of strings representing numbers from 1 to n (inclusive). However, for multiples of two, output "Fizz" instead of the number. For multiples of three, output "Buzz". For numbers which are multiples of both two and three, output "FizzBuzz".
(The problem often asks to print, but returning a list of strings is common in coding platforms).
You'll need to check divisibility for each number from 1 to n.
Looping: How do you iterate through numbers from 1 to n?
Divisibility Check: The modulo operator % gives you the remainder of a division. If number % divisor == 0, then number is a multiple of divisor.
Order of Conditions: A number can be a multiple of 2, a multiple of 3, or a multiple of both. Which condition should you check first to correctly print "FizzBuzz" for numbers divisible by both 2 and 3? (Hint: If you check for divisibility by 2 first, a number like 6 would print "Fizz" and the "Buzz" part might be missed if not handled correctly).
Solution Explanation & Interview Strategy
The Goal: We want to go through numbers, say from 1 to 10.
If a number is a multiple of 2 (like 2, 4, 8), we say "Fizz".
If it's a multiple of 3 (like 3, 9), we say "Buzz".
If it's a multiple of BOTH 2 and 3 (like 6), we say "FizzBuzz".
Otherwise, we just say the number itself (like 1, 5, 7).
The trickiest part is making sure "FizzBuzz" is handled correctly.
Approach 1: Standard If-Elif-Else
The most common way to solve this is to use a loop to iterate from 1 to n. Inside the loop, use conditional statements (if, elif, else) to check for divisibility. The key is the order of these checks:
First, check if the number is divisible by both 2 and 3 (i.e., divisible by 2*3 = 6). If so, output "FizzBuzz".
Else, check if it's divisible by 2. If so, output "Fizz".
Else, check if it's divisible by 3. If so, output "Buzz".
Else (if none of the above), output the number itself.
fromtypingimportListclassSolution:deffizzBuzz_standard(self, n:int) ->List[str]:result= []
foriinrange(1, n+1):# Loop from 1 to n (inclusive)ifi%2==0andi%3==0:# Check for divisibility by 2 AND 3 firstresult.append("FizzBuzz")
elifi%2==0:# Then check for divisibility by 2result.append("Fizz")
elifi%3==0:# Then check for divisibility by 3result.append("Buzz")
else:result.append(str(i)) # Otherwise, append the number as a stringreturnresult# Example Usage:# sol = Solution()# print(sol.fizzBuzz_standard(10)) # Output: ['1', 'Fizz', 'Buzz', 'Fizz', '5', 'FizzBuzz', '7', 'Fizz', 'Buzz', 'Fizz']
Complexity Analysis:
Time Complexity:O(N), where N is the input number. We loop N times, and operations inside the loop (modulo, comparison, append) are constant time.
Space Complexity:O(N) if we are returning a list of N strings. If we were just printing, it would be O(1) auxiliary space.
Approach 2: String Concatenation (Slightly More Flexible for More Conditions)
Instead of strict if-elif-else, we can build the output string conditionally. This approach can be more easily extended if you had more conditions (e.g., "Jazz" for multiples of 5).
fromtypingimportListclassSolution:deffizzBuzz_concat(self, n:int) ->List[str]:result= []
foriinrange(1, n+1):current_output=""divisible_by_2= (i%2==0)
divisible_by_3= (i%3==0)
ifdivisible_by_2:current_output+="Fizz"ifdivisible_by_3:current_output+="Buzz"ifnotcurrent_output:# If string is still empty, it's not divisible by 2 or 3current_output=str(i)
result.append(current_output)
returnresult
Complexity Analysis:
Time Complexity:O(N).
Space Complexity:O(N) for the result list.
Key Takeaways for Interviews:
Clarity and Correctness: The primary goal is to write clear, correct code. The standard if-elif-else is perfectly fine.
Order of Conditions: The most critical point is checking for divisibility by *both* numbers (e.g., 2 and 3, or their LCM which is 6) *before* checking for individual divisibility. Failing to do this is a common mistake.
Modulo Operator: Demonstrate understanding of %.
Looping Correctly: Ensure the loop covers the range 1 to n inclusive (e.g., range(1, n + 1)).
Extensibility (Optional Discussion): The string concatenation approach (Approach 2) can be mentioned if the interviewer asks about handling more complex rules or more divisors, as it scales a bit more cleanly without deeply nested elif chains.
Output Format: Pay attention to whether the problem asks to print directly or return a list of strings.