basics · beginner · ~15 min
Combine a for-loop with chained conditionals — and learn that case ORDER matters.
Build the classic FizzBuzz sequence for 1..n as a single text buffer.
Implement void fizzbuzz(int n, char *out, size_t out_sz) that writes the FizzBuzz lines for 1..n into the caller-provided buffer out (capacity out_sz bytes, including the terminating NUL). No main — the grader calls it.
n: the upper bound, inclusive. May be 0 (produce nothing).out: a writable buffer of out_sz bytes that you fill with a NUL-terminated string.Nothing is returned. After the call out holds one entry per line for i = 1..n, each followed by a \n:
i divisible by 15 -> FizzBuzzFizzBuzzfizzbuzz(5, out, sz) -> out = "1\n2\nFizz\n4\nBuzz\n"
n = 0: out becomes the empty string "".out_sz; use bounded formatting (e.g. snprintf with a running offset).The most-asked screening question in the world. Get it precise once and it's a freebie forever.
n (inclusive upper bound) plus a writable buffer out of out_sz bytes.
out filled with the FizzBuzz lines for 1..n, each ending in newline, NUL-terminated.
Check divisibility by 15 first; never write past out_sz.
#include <stdio.h>
#include <string.h>
void fizzbuzz(int n, char *out, size_t out_sz) {
/* TODO */
out[0] = '\0';
}
Wrong case order (15 misclassified as Fizz). Off-by-one (100 missing). Forgetting else.
i=15, 30, 45, 60, 75, 90 → FizzBuzz. i=100 must be included (loop bound is <= 100).
O(N).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.