You know the classic FizzBuzz: for numbers 1 to n, print "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both, and the number otherwise.
For example, for n = 15 the output includes: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz.
Beyond the basic solution, explain how you would optimize or extend FizzBuzz if new rules are added (e.g., "Jazz" for multiples of 7). What are the design concerns with the naive if-else chain approach, and how would you make it more maintainable?
How to approach it
- Hint 1
The naive approach uses a chain of if-else statements. What happens when you add a 4th or 5th rule?
- Hint 2
Consider building the output string by concatenating parts: check each rule independently and append its word if the condition matches.
- Hint 3
A data-driven approach uses a list of (divisor, word) pairs, making it trivial to add new rules without changing logic.
Ready to answer it out loud?
Record your answer in 1:00 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.