KatInterview

Generate Number Sequence with Divisibility Rules

StringsAuto-graded · 15 test cases
EasyAuto-gradedStringsconditionalsmoduloiteration

Last updated

Tests basic control flow, modular arithmetic, and string formatting by transforming numbers according to divisibility conditions.

Create a function that generates a sequence of strings from 1 through n, where each position follows specific transformation rules based on divisibility:

  • When a number is evenly divisible by 3, output "Fizz"
  • When a number is evenly divisible by 5, output "Buzz"
  • When a number is evenly divisible by both 3 and 5, output "FizzBuzz"
  • Otherwise, output the number itself as a string

Return the complete sequence as a list of strings in ascending order.

Constraints

  • 1 <= n <= 10^4
  • All numbers must be represented as strings in the output
  • The sequence must include all integers from 1 to n inclusive

Examples

Example 1

Input: number_sequence(5)

Output: ['1', '2', 'Fizz', '4', 'Buzz']

Example 2

Input: number_sequence(15)

Output: ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']

Hints

Hint 1

Check divisibility by 15 before checking 3 or 5 individually to handle the combined case correctly

Hint 2

You can also build the output string incrementally by concatenating 'Fizz' and 'Buzz' separately based on each condition

Hint 3

Remember that the result must contain strings, not integers, so convert numbers to strings when no divisibility rule applies