FizzBuzz: One Simple Interview Question
Why Code Review Stack Exchange recommends it
Code Review Stack Exchange users link it when reviewing FizzBuzz solutions, as a good explanation of better approaches and of building an expandable rule-based version.
Recommended by 2 people across 2 different months, 2020 to 2021.
What it is
Tom Scott explains the FizzBuzz interview question, why it works as a basic filter for programming candidates, and why it trips people up. He walks through common approaches and a clean solution, filmed in front of the Megaprocessor at the Centre for Computing History.
What people said
Recommends J.G. · 2020-07 · Code Review Stack Exchange · on “Solving Fizz Buzz using Java”
Imagine a variant on FizzBuzz with \$n\$ different say-this-syllable-if rules. Would you want to check \$2^n\$ cases? That will lead to either a lot of code ( \$O(2^n)\$ lines) or deeply nested code (with \$O(n)\$ maximum indentation). Luckily, the problem statement does you a favour: on divisibility by 15, it asks you to print FizzBuzz, not Fizz Buzz with a space as you tried. So the clean-code approach (with \$O(n)\$ lines, and \$O(1)\$ maximum indentation) is more like this: public class FizzBuzz { public static void main(String[] args) { for (int number = 1; number <= 100; number++) { String toPrint = ""; if(number % 3 == 0) { toPrint += "Fizz"; } if(number % 5 == 0) { toPrint += "Buzz";
Recommends Lovethenakedgun · 2021-03 · Code Review Stack Exchange · on “Logs 'Fizz' for multiples of three, 'Buzz', for multiples of 5, and 'Fizz Buzz' for multiples of 3 & 5”
Aside from fizzBuzzNumbers[i] always being identical to i (as others pointed out), I think this is a fantastic first attempt. I.e. if num was 3, fizzBuzzNumbers would be [1,2,3] and i would go from 1-to-2-to 3 without even needing to reference the array at all beyond length , which is just your input num . Your array could actually be ['big', 'bad', 'wolf'] and the output would be the same for an input of 3. One resource that I think is worth a watch after trying this problem is Tom Scott's FizzBuzz: One Simple Interview Question [this video] on YouTube. He breaks down some common ways that people approach the problem, as well as why some approaches are better than others.
Cites user211881 · 2020-02 · Code Review Stack Exchange · on “Small FizzBuzz program”
After watching this video by Tom Scott [this video] , I decided to make a program that follows that idea. This is what I came up with: # If a number is divisible by the key, add the value RULES = { 3: "Fizz", 5: "Buzz" } def is_divisible(num: int) -> list: output = [] for divider in RULES: if (num % divider) == 0: output.append(RULES[divider]) return output for num in range(100): output = is_divisible(num + 1) # Increment num by 1 so it is 1-100 not 0-99 if not output: print(num + 1) else: print("".join(output)) My whole idea behind the way I made this is because I wanted it to be as expandable as possible. That way if you wanted to change the code to do something different (like the examples at