TRT
Sign in
easyFoundationsconcept

What happens when extraction fails and how do you recover?

Describe stream state after bad input, the clear and ignore repair, and the leftover newline with line input.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstReading Input and Surviving Bad InputRead values with std::cin, see what a failed read does to the variable and to the stream, and build the clear-and-discard loop that survives bad input.

A strong answer

A strong candidate frames What happens when extraction fails and how do you recover? around the exact memory layout, compiler semantics, and hardware execution model covered in Reading Input and Surviving Bad Input. Describe stream state after bad input, the clear and ignore repair, and the leftover newline with line input.

A program that only prints is a calculator with no keys. Reading input is its first contact with the outside world, and the outside world does not follow your assumptions. Here is a program that asks for an age:

Type 31 and it answers Next year: 32. Type abc and it answers Next year: 1. There is no crash, no error message and no exit status to warn anyone: the program carried on with a wrong value as if nothing had happened. Making a program survive bad input means knowing exactly what a read does when it fails.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Explain what an extraction like std::cin >> age consumes and what it leaves unread.
  • Mastery of core invariant: Describe the stream state after a failed read and why later reads do nothing.
  • Mastery of core invariant: Apply clear and ignore, in the right order, to recover from a failed read.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Why does a retry loop spin forever if you clear without discarding the bad line?

Not started

Sign in to save your learning progress.

Sign in to save