TRT
Sign in
mediumCorecoding

How would you code a checkout that fails without residue?

Build a LibraryError family, validate before mutation, stage on a copy, and commit without failure.

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

The Runtime Theory Team2 min read
Learn it firstProject: Failing Library Operations CleanlyPro — members onlyBuild a LibraryError hierarchy, validate a checkout before mutating, and commit with noexcept swaps so failures leave no residue.

A strong answer

A strong candidate frames How would you code a checkout that fails without residue? around the exact memory layout, compiler semantics, and hardware execution model covered in Project: Failing Library Operations Cleanly. Build a LibraryError family, validate before mutation, stage on a copy, and commit without failure.

Checkout touches three records at once: the member file, the book shelf, and the loan ledger. A naive flow decrements the shelf, appends the ledger, then checks fines, so a fine violation throws after the shelf already changed. The next patron sees a missing book with no loan to explain it. The hook describes exactly that residue: three simultaneous problems, one vague log, and a count that no longer matches reality.

A capstone failure design answers two questions together. Which precise type names each violation so the desk can respond helpfully. And what state promise holds after each throw so retry is safe. As shown in the trace above, validation stays read-only, mutation targets a copy, then a non-throwing commit installs the result.

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: Design a LibraryError hierarchy rooted in runtime_error.
  • Mastery of core invariant: Validate checkout preconditions before mutating shared state.
  • Mastery of core invariant: Commit loans with strong safety and centralized handling.
  • 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. 01Where would you place try blocks so logs name the failed gate?
  2. 02How would you scale staging when a full copy costs too much?

Not started

Sign in to save your learning progress.

Sign in to save