← Questions

Debug a staged C++ project with AI tools allowed

0
NvidiaCodingLast asked Feb 2026

A coding interview question asked in Nvidia's practical or debugging round, last asked Feb 2026. It tests c++, debugging and concurrency.

You get a small C++ project and a build command. The program runs in stages, and each stage hides one bug: a logic error, a memory bug or a data race. Build it, run it, observe what goes wrong, and fix the stages one by one. You may use an AI coding assistant, but you must explain your reasoning and be able to defend every fix.

For example, a stage might look like this:

// Stage 3: count events from 4 worker threads
int total = 0;
std::vector<std::thread> workers;
for (int t = 0; t < 4; ++t)
    workers.emplace_back([&] { for (int i = 0; i < 100000; ++i) ++total; });
for (auto& w : workers) w.join();
std::cout << total << "\n";   // expected 400000, prints less, and differs run to run

Requirements

  • Every stage produces its expected output, the program exits cleanly, and the fixes are minimal and correct rather than workarounds.
  • Explain for each bug what the symptom was, how you found the cause, and why the fix is right.

Clarifying questions

  • Is the expected output of each stage given, or must you infer it from the code?
  • Which tools are available: a debugger, AddressSanitizer, ThreadSanitizer, Valgrind?
  • May you change interfaces between stages, or only the bodies?
  • Is performance part of the bar, or only correctness?

What a strong answer covers

  • A method: reproduce, read the output, form a hypothesis, confirm it with a tool, then fix. Not changing code at random.
  • Compiling with warnings on (-Wall -Wextra) and with sanitizers: ASan for out-of-bounds, use-after-free and leaks; TSan for races; UBSan for undefined behavior.
  • Typical fixes: an off-by-one loop bound, a dangling reference or iterator invalidated by push_back, a missing delete replaced by RAII and smart pointers, a shared counter made std::atomic or protected by a mutex, a lock-order deadlock.
  • Using the AI assistant critically: checking its suggestions against the evidence and catching a plausible fix that only hides the symptom.
  • Confirming each fix by rerunning, several times for races.

Follow-ups

  • The race only shows up once in a thousand runs. How do you make it reproducible?
  • Which of these bugs would a code review catch, and which need tools?
  • How would you add tests so these bugs cannot come back?

Where it's asked

What it tests

c++ · debugging · concurrency

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding