Interview Q&A · C · 8 questions in this topic
Scenario-Based Questions — C Interview Q&A
Judgment questions — what you would actually do when crashes, leaks, or legacy code land on your desk. Part of Scenarios & Rapid Fire · 202 questions in the C bank.
Your C service segfaults in production once a day. How do you debug it?
Answer
Reproduce under sanitizers first (ASan/UBSan usually catch it in minutes), enable core dumps built with symbols, and bisect recent changes. Read the crashing stack for use-after-free, buffer overflow, data races (then TSan) and double-free. Fix the ownership/bounds root cause, add a regression test, and put sanitizers into CI.
Narrate hypothesis-first: read the stack, form one theory, prove it with a sanitizer — not printf roulette.
Key points
- 1Capture — core dump, logs, exact build symbols.
- 2Reproduce — sanitizers (ASan/UBSan/TSan) over guessing.
- 3Fix root cause, regress-test, harden CI.
💡 Say sanitizers before logs — that ordering signals production experience.
Memory usage grows steadily (suspected leak). How do you find it?
Answer
Confirm with metrics (RSS over time under steady load), then LeakSanitizer/valgrind --leak-check on a workload, reviewing still-reachable vs definitely-lost. Common sources: missing free on error paths, growing caches without eviction, forgotten fclose/thread handles. Fix ownership, add allocation-count tests, and gate CI on leak-clean runs.
Steady-growth plus flat traffic is the leak signature; spikes are usually caching, not leaking.
Key points
- 1Confirm — RSS trend under steady load.
- 2Locate — LSan/valgrind with defect classification.
- 3Prevent — ownership rules plus CI gates.
A hot loop is too slow. How do you approach optimizing C code?
Answer
Measure first (perf, gprof, Tracy) to find the true hotspot, then reduce algorithmic complexity, then memory traffic (contiguity, SoA, cache lines, restrict), then branches and allocations. Re-measure every change; unmeasured micro-opts are how code gets slower and uglier.
Typical wins: SoA layout, restrict on hot kernels, pooled allocation, branchless idioms where proven.
Key points
- 1Profile — find the 3% that matters.
- 2Algorithm, then layout, then micro-opts.
- 3Re-measure; keep only proven wins.
You inherit legacy C (K&R prototypes, gets, raw globals). How do you modernize safely?
Answer
Incrementally behind tests: prototype everything (-Wstrict-prototypes), replace gets with fgets/getline, bound every string op, encapsulate globals behind accessors or opaque types, and lift warnings to -Wall -Wextra -Werror plus sanitizers. Each step keeps behavior identical (golden tests), then enables the next.
No big-bang rewrites: strangler-style migration with ABI boundaries respected where needed.
Key points
- 1Prototypes and gets-removal first (correctness).
- 2Bounds and ownership next (safety).
- 3Encapsulation last (design).
Two threads corrupt a shared structure intermittently. Walk me through the fix.
Answer
Confirm with ThreadSanitizer, then decide the sharing model: mutex-guarded structure for compound operations (check-then-act must be atomic as a unit), or sharding/thread-local plus merge for scale. Guard every access path (including iteration and destruction), keep critical sections short and ordered, and regress with stress tests under TSan.
Per-field atomics cannot fix multi-step structure updates — the transaction boundary needs the mutex.
How do you expose C to Python (or another language)?
Answer
A stable C ABI (extern linkage, plain structs, explicit sizes) is the boundary every FFI speaks: ctypes/cffi load shared libraries directly, extension modules wrap init logic. Keep ownership explicit per function (who frees, in which runtime), avoid exceptions/threads surprises across the line, and version the ABI.
C is the interop lingua franca precisely because its symbols and layout are predictable.
How do you handle out-of-memory in C?
Answer
Check every allocation (NULL means failure), unwind partial state via the cleanup path, and propagate the error up — the layer that can shed load, degrade, or exit decides. Libraries return errors; only programs exit. Emergency reserves and allocators with caps help services fail gracefully instead of crashing mid-request.
OOM handling is designed, not improvised: decide the policy before the failure.
Example
int *buf = malloc(n * sizeof *buf); if (!buf) goto out; /* unwind partial state, propagate */
How do you review a C pull request? What do you check first?
Answer
Lifetimes and bounds first (who frees, buffer sizes proven, NULL checks), then const-correctness and error paths, then API shape and naming, then tests plus sanitizer-clean CI. Mechanical gates (format, -Werror, ASan/UBSan, coverage of new branches) should already be green before human review starts.
A review that debates style while missing an unbounded strcpy has failed.
Key points
- 1Memory — ownership, bounds, NULL discipline.
- 2Correctness — const, edge cases, error paths.
- 3Design — API shape, tests, sanitizer evidence.
💡 Bounds before style is the reviewer credo worth stating out loud.
Recommended resourcesAffiliate links · may earn us a commission