Implement FizzBuzz
Company: Uber
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates basic programming and algorithmic reasoning, including control flow, modular arithmetic, string handling, and the ability to analyze time and space complexity.
Constraints
- 1 <= n <= 10^4
- Output each value as a string (numbers are stringified).
Examples
Input: (15,)
Expected Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
Explanation: The canonical n=15 example: multiples of 3 -> Fizz, of 5 -> Buzz, and 15 -> FizzBuzz.
Input: (1,)
Expected Output: ["1"]
Explanation: Single element; 1 is divisible by neither 3 nor 5, so it stays "1".
Input: (3,)
Expected Output: ["1", "2", "Fizz"]
Explanation: Boundary at the first Fizz.
Input: (5,)
Expected Output: ["1", "2", "Fizz", "4", "Buzz"]
Explanation: Boundary at the first Buzz.
Input: (30,)
Expected Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz"]
Explanation: Two full cycles; both 15 and 30 produce FizzBuzz.
Hints
- Check divisibility by 15 (i.e., by both 3 and 5) FIRST, before checking 3 or 5 individually — otherwise a multiple of 15 would be caught by the 3-only branch.
- Equivalently, check `i % 3 == 0` and `i % 5 == 0` separately and concatenate "Fizz"/"Buzz", falling back to str(i) when neither matches.
- Use range(1, n + 1) so the loop is inclusive of n.