Fizz Buzz
The FizzBuzz problem requires a simple loop and some conditional logic. We need to iterate through numbers from 1 to n
, and for each number, we check if it’s divisible by 3 and 5, only 3, only 5, or neither.


This has a time complexity of O(n), as we are iterating through all numbers from 1 to n
, and a space complexity of O(n), as we are storing the results for all these numbers.




Problem Classification
This problem falls into the following categories:
Looping or Iteration: You’re working through a range from 1 to n, performing actions on each number in the sequence. This involves understanding and manipulating loop constructs.
Conditionals: The problem is about testing whether certain conditions are met (i.e., divisibility by 3, 5, or 15). Each condition results in a different output. This involves understanding ifelse statements or switch cases.
Array Manipulation: The output is an array (or a list), and you’re asked to generate its elements. This involves operations such as creation, indexing, and assignment of arrays or lists.
String Manipulation: The output for each element in the list is a string. This involves understanding how to convert integers to strings, and how to work with string literals.
Arithmetic Operations: The problem involves basic arithmetic operations of modulus to check divisibility.
Problem Solving: Given the rules, you must formulate an algorithm that will correctly classify each number.
Overall, this problem provides practice with control flow (loops and conditionals), data structures (arrays and strings), arithmetic operations, and algorithmic thinking.
Language Agnostic Coding Drills
Here are the problemsolving steps and corresponding drills for the FizzBuzz problem:
Understanding Looping Constructs: This problem involves looping through numbers from 1 to n. The learner should know how to create a loop that iterates through a range of numbers. The drill can involve writing a loop that prints numbers from 1 to n.
Understanding and Working with Arrays: You’re creating an array of strings that represent numbers from 1 to n. The drill can involve creating an array of strings from an array of integers.
Understanding String Manipulation and Conversion: The problem requires the conversion of integers to strings and assignment of string literals to array elements. The drill can involve converting a list of integers to a list of strings.
Understanding Conditionals and Arithmetic Operations: This problem involves checking the divisibility of a number by 3, 5, and 15 (3*5). The learner needs to know how to use the modulus operator to check for divisibility and use conditionals to execute code based on the outcome. The drill can involve writing a function that takes an integer and returns “Divisible by 3”, “Divisible by 5”, “Divisible by 15”, or “Not divisible” based on the input.
Combining Elements: The final step is to combine all these skills. This involves using a loop to iterate through an array, checking for divisibility conditions for each element, and updating the array elements based on the outcome. This needs an understanding of how loops, conditionals, arithmetic operations, and array manipulations work together.
The approach to solving the problem would be to initially generate an array of strings from 1 to n. Then, iterate over the array, checking for divisibility by 3, 5, and 15 at each index. If the current index (plus one, due to zerobased indexing) is divisible by 3, replace the corresponding element with “Fizz”. Similarly, replace with “Buzz” for divisibility by 5, and “FizzBuzz” for divisibility by 15. The key insight is that numbers divisible by 15 are also divisible by 3 and 5, so the check for divisibility by 15 should be done first, or the check for divisibility by 3 and 5 should ignore numbers divisible by 15.
Targeted Drills in Python
Understanding Looping Constructs:
Problem: Write a function
print_numbers(n)
that prints numbers from 1 to n.1 2 3
def print_numbers(n): for i in range(1, n+1): print(i)
Understanding and Working with Arrays:
Problem: Write a function
numbers_to_strings(n)
that returns an array of strings that represent numbers from 1 to n.1 2
def numbers_to_strings(n): return [str(i) for i in range(1, n+1)]
Understanding String Manipulation and Conversion:
Problem: Write a function
convert_to_string(n)
that takes an integer n and returns a string representation of it.1 2
def convert_to_string(n): return str(n)
Understanding Conditionals and Arithmetic Operations:
Problem: Write a function
check_divisibility(n)
that takes an integer and returns “Divisible by 3”, “Divisible by 5”, “Divisible by 15”, or “Not divisible” based on the input.1 2 3 4 5 6 7 8 9
def check_divisibility(n): if n % 15 == 0: return "Divisible by 15" elif n % 3 == 0: return "Divisible by 3" elif n % 5 == 0: return "Divisible by 5" else: return "Not divisible"
Combining Elements:
Problem: Write a function
fizz_buzz(n)
that implements the final solution using all the above drills.1 2 3 4 5 6 7 8 9 10 11
def fizz_buzz(n): arr = numbers_to_strings(n) for i in range(n): check = check_divisibility(i+1) if check == "Divisible by 15": arr[i] = "FizzBuzz" elif check == "Divisible by 3": arr[i] = "Fizz" elif check == "Divisible by 5": arr[i] = "Buzz" return arr
Each of these drills helps understand a specific concept needed for the final solution. Together, they provide the skills and understanding necessary to implement the FizzBuzz solution.