FizzBuzz is a good problem for interviews because it allows you an adaptive scale of interesting things to ask. If the candidate struggles to get the most basic solution working, you can spend the whole hour working with them on improving their solution, and let them leave with a good feeling about your company. If the candidate is a super-literate programmer, you get to explore issues such as testing, readability, and maintainability. This is one possible exploration of maintainability.
Suppose that, instead of the specific game of FizzBuzz, we want to write a program that can play any variant of the FizzBuzz problem, for which FizzBuzz is one specific instance.
To do this, we need to define three things: the type
PeriodicReplacement, and the two functions used to create and execute periodic replacements. The type itself is simply to abstract away the difficulty of C array management.
ExecutePeriodicReplacement is simply the code from Part 2 modified slightly to use our
PeriodicReplacement type instead of a local array and magic number 15.
The meat of the problem is in the
CreatePeriodicReplacement function. It is passed any divisors and their replacements, along with the count, and does all the precomputation necessary to create an array like
rg in Part 2. In order to reduce the number of reallocations, the shortest possible
PeriodicReplacement.Length will be calculated as the least common multiple of the divisors.
This approach has the nice quality that precomputation can be saved in a static variable for future iterations, if necessary. I've ignored cleanup code and elided over
StrConcat, as they distract from the core of the problem.