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 performance, crashes, or legacy code land on your desk. Part of Scenarios & Rapid Fire · 213 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), add core dumps with symbols, and bisect recent changes. Inspect the crashing stack for use-after-free, dangling views, data races (then TSan) and OOM-adjacent corruption. Fix the ownership/lifetime root cause, add a regression test, and ship the sanitizer 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 ownership, regress-test, harden CI.

💡 Say sanitizers before logs — that ordering signals production experience.

A hot loop is too slow. How do you approach optimizing C++ code?

Answer

Measure first (profiler: perf, Tracy, VTune) to find the true hotspot, then reduce algorithmic complexity, then memory traffic (contiguity, SoA, cache lines), then branches and allocations. Re-measure every change; unmeasured micro-opts (including blind keyword sprinkling) are how code gets slower and uglier.

Typical wins: vector over node containers, reserve, move instead of copy, views instead of copies, algorithms with execution policies.

Key points

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

You inherit C-style C++ (raw new/delete, char buffers, macros). How do you modernize safely?

Answer

Incrementally behind tests: replace char buffers with std::string/string_view, raw arrays with vector/array, owning raw pointers with unique_ptr, macros with constexpr/templates, and manual loops with algorithms. Each step keeps behavior identical (golden tests + sanitizers), then enables the next.

No big-bang rewrites: strangler-style migration with ABI boundaries respected where needed.

Key points

  1. 1Strings and containers first (memory safety).
  2. 2Ownership types next (leak elimination).
  3. 3Macros and loops last (readability).

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