Programming Fundamentals · Interview question

You added a printf to debug, the bug went away, and removing the printf brings it back. What's likely going on?

A strong answer

Almost certainly undefined behavior, usually uninitialized memory, a buffer overflow, a race condition, or a timing-sensitive bug. The printf is changing something the bug depends on: stack layout (so a buffer overflow lands in different memory), timing (so a race goes the other way), or initialization state (because printf indirectly causes a malloc or syscall that touches the same memory). The bug isn't "fixed" by the printf, it's hidden. The right move is to run under AddressSanitizer (-fsanitize=address) and UndefinedBehaviorSanitizer (-fsanitize=undefined), which will usually flag the root cause precisely. Treat the printf-disappears phenomenon as evidence of UB, not as a fix.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Errors & Debugging

Errors are messages from the compiler trying to help. Learn to read them, distinguish the three kinds of bugs, and build the mindset that turns debugging from frustration into a skill.

More Errors & Debugging questions

Browse all 472 interview questions