Interview Q&A · C · 8 questions in this topic

Scenario-Based Questions — C Interview Q&A

All C topics

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

  1. 1Capture — core dump, logs, exact build symbols.
  2. 2Reproduce — sanitizers (ASan/UBSan/TSan) over guessing.
  3. 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

  1. 1Confirm — RSS trend under steady load.
  2. 2Locate — LSan/valgrind with defect classification.
  3. 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

  1. 1Profile — find the 3% that matters.
  2. 2Algorithm, then layout, then micro-opts.
  3. 3Re-measure; keep only proven wins.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits