Undefined Behaviour Traps That Appear in C++ Interviews
Undefined behaviour lets the compiler assume a program never triggers it, so the traps interviewers use are the ones that compile clean and fail silently.
Undefined behaviour traps that appear in C++ interviews are chosen specifically because they compile without warning, often run correctly under one compiler and set of flags, and fail unpredictably under another, which is exactly the property that makes undefined behavior dangerous in production rather than merely incorrect. The C++ standard leaves certain operations entirely unspecified, and the compiler is permitted to assume a well-formed program never triggers them, which means the compiler's optimizer can and does generate code that behaves in surprising ways once that assumption is violated, rather than simply producing the wrong number.
Signed integer overflow is undefined behavior in C++, unlike unsigned overflow, which wraps predictably. A candidate handed a snippet that increments a signed integer counter without any bound and asked whether it is safe should recognize that the compiler is free to assume the overflow never happens, and optimizing compilers have historically used that assumption to eliminate an overflow check a programmer wrote defensively, since from the compiler's point of view a check for a condition the standard says cannot occur is dead code. Trading systems that count messages or ticks in a tight loop are a natural place for this trap to surface, and interviewers use it because it looks harmless in…
Reading or writing past the end of an array or a vector's allocated storage is undefined behavior, and it frequently does not crash immediately, instead silently corrupting adjacent memory that only causes a visible failure much later and far from the actual bug. A closely related trap is iterator invalidation: appending to a std::vector can trigger a reallocation that moves every element to a new block of memory, silently invalidating every iterator, pointer and reference into the old storage, so code that holds a reference into a vector across an insert into that same vector is reading freed memory without any compiler warning at the point of the mistake.
Interviewers at firms like IMC Trading commonly present a short snippet that holds a reference obtained before a push_back and dereferences it afterward, since the bug is invisible unless the candidate specifically knows the reallocation rule.
Use after free occurs when memory is accessed through a pointer or reference after the object it pointed to has been destroyed, and the danger is that the memory is frequently still intact and readable for some period afterward, so the bug can pass every test run in a development environment and only manifest once the freed memory is reused for something else in production under real load. A move constructor that fails to reset the source object correctly, discussed in the piece on move semantics and ownership, is one common way this pattern gets introduced without the author noticing.