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.Example 1:
Input: n = 3 Output: ["1","2","Fizz"]
Example 2:
Input: n = 5 Output: ["1","2","Fizz","4","Buzz"]
Example 3:
Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints:
1 <= n <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of consecutive numbered boxes starting from one up to a given ending number. The brute force strategy involves walking through every single box one by one, looking at its number, and writing down the correct answer on a notepad based on simple counting rules.
Here's how the algorithm would work step-by-step:
def fizz_buzz(ending_number):
notepad_results = []
for current_number in range(1, ending_number + 1):
# Check both divisibility conditions first to capture multi-ples of fifteen
if current_number % 3 == 0 and current_number % 5 == 0:
notepad_results.append("FizzBuzz")
# Check divisibility by three alone before falling back to other rules
elif current_number % 3 == 0:
notepad_results.append("Fizz")
# Check divisibility by five alone since it failed previous strict tests
elif current_number % 5 == 0:
notepad_results.append("Buzz")
# Default fallback to record the raw number when no rules apply
else:
notepad_results.append(str(current_number))
return notepad_resultsThe optimal strategy is to go through a sequence of numbers one by one, deciding what to say based on whether the number is a multiple of certain special values. Instead of doing complicated math, we just check each number individually and write down the correct word or the number itself.
Here's how the algorithm would work step-by-step:
def fizz_buzz(upper_limit):
result_sequence = []
for current_number in range(1, upper_limit + 1):
# Checking both conditions first prevents missing numbers that need the combined word.
if current_number % 3 == 0 and current_number % 5 == 0:
result_sequence.append("FizzBuzz")
elif current_number % 3 == 0:
result_sequence.append("Fizz")
# Checking the second condition independently ensures multiples of five are properly labeled.
elif current_number % 5 == 0:
result_sequence.append("Buzz")
else:
result_sequence.append(str(current_number))
# Returning the fully populated sequence satisfies the output requirement for the caller.
return result_sequence| Case | How to Handle |
|---|---|
| Input n equals 1 | The loop runs once and returns a single-element array containing the string "1". |
| Inputs less than 1 such as zero or negative integers | The problem constraints typically specify n >= 1, but if encountered, return an empty array. |
| Maximum-sized inputs like n equals 10 to the 4th or 10 to the 5th power | The linear time O(n) approach ensures the algorithm executes well within time limits without performance degradation. |
| Memory limits with large n values | Pre-allocating the resulting array of strings prevents dynamic resizing overhead and memory fragmentation. |
| Numbers that are multiples of both 3 and 5 | Checking divisibility by 15 or both 3 and 5 first prevents premature matching of "Fizz" or "Buzz". |
| Language-specific integer overflow | Standard 32-bit or 64-bit integers easily accommodate typical interview constraints for n without overflow risks. |