AI summary by TwinMind
FizzBuzz: One Simple Interview Question
Tom Scott
7m 18s
Background and Context
- 10 years ago, friend Imran wrote about hiring programmers
- Observed CS degree holders knew theory but couldn't code in real world
- Proposed simple test to verify actual coding ability
- Hiring challenges: lateral thinking questions ineffective, "culture fit" problematic
- Need practical test to ensure candidates can actually code
FizzBuzz Game Rules
- Traditional children's game played between two people
- Count up in turns: 1, 2, 3, 4...
- Multiple of 3: say "Fizz" instead of number
- Multiple of 5: say "Buzz" instead of number
- Multiple of both (15): say "FizzBuzz"
- Example sequence: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz...
Programming Test Requirements
- Write program outputting FizzBuzz for numbers 1 to 100
- Test reveals multiple insights:
- Many possible approaches available
- Shows programmer's style and problem-solving approach
- Reveals if they "bodge things together" vs plan for future
- Contains tricky logic structure: 4 different conditions to handle
Implementation Approach 1: Basic Solution
- Language: JavaScript (chosen for ease of getting started)
- Step 1: Create for loop (i=1; i<=100; i++)
- Variable 'i' used by tradition
- Runs 100 times, incrementing i each iteration
- Step 2: Add console.log(i) for output
- Step 3: Add conditional logic using modulo operator (%)
- if (i % 3 === 0): output "Fizz"
- if (i % 5 === 0): output "Buzz"
- if neither: output number
Problem with Initial Approach
- At number 15: outputs "Fizz" and "Buzz" on separate lines
- Should output "FizzBuzz" on single line
- Solution: Add separate test for multiples of both 3 and 5
- Results in code repetition and complexity
Improved Implementation (Else-If Structure)
- Test for both conditions first: if (i % 3 === 0 && i % 5 === 0)
- Use else-if statements for individual conditions
- Cleaner logic flow but still has maintenance issues
- Problem: changing requirements (e.g., 7 instead of 5) requires multiple changes
Optimal Solution: String Building Approach
- Create empty string variable called "output"
- If multiple of 3: add "Fizz" to output string
- If multiple of 5: add "Buzz" to output string
- If output string empty: set output to current number
- Single console.log(output) at end
- Advantages:
- Only one place to change numbers for new requirements
- Easy to extend (add 7s, 11s, 13s by copying lines)
- Avoids repetitive else-if nightmare
- More maintainable code
Programming Philosophy Discussion
- CS degrees focus on theory, miss practical skills
- Two approaches: bodging vs long-term planning
- Author favors "bodging things together" that works
- Important: don't leave mess for future maintainers
- Acknowledgment that even better solutions exist but "good enough" principle
Current Usage and Limitations
- Some companies still use FizzBuzz for hiring
- Often used by interviewers who can't think of better tests
- Highlights gaps in computer science education
- Practical coding skills vs theoretical knowledge disparity
