TRT
Sign in
mediumCoreconcept

What is a sparse file and how does lseek create one?

Explain sparse files, file holes, and how seeking past EOF then writing allocates only the written blocks.

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

The Runtime Theory Team2 min read
Learn it firstFile Offsets, Sparse Files, and lseekPro — members onlyManipulate file offsets with lseek, understand sparse files and unallocated disk holes, and navigate non-seekable streams.

A strong answer

A strong candidate frames What is a sparse file and how does lseek create one? around the exact memory layout, compiler semantics, and hardware execution model covered in File Offsets, Sparse Files, and lseek. Explain sparse files, file holes, and how seeking past EOF then writing allocates only the written blocks.

By default, every read() and write() operation advances a file's read/write position sequentially. If you open a 2 GB database file and need to update a 16-byte record at the 1.5 GB mark, reading through the first 1.5 GB of data to get there wastes hundreds of millions of CPU cycles.

Now consider another scenario: creating a disk image container for a virtual machine that reserves a 100 GB virtual drive. Writing 100 GB of real zeros to disk takes several seconds and immediately consumes 100 GB of physical SSD storage.

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: Reposition read and write file offsets using SEEK_SET, SEEK_CUR, and SEEK_END.
  • Mastery of core invariant: Inspect current file positions and file sizes without transferring payload data.
  • Mastery of core invariant: Construct sparse files with filesystem holes and analyze logical versus physical disk usage.
  • 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. 01How does the compiler, OS kernel, or hardware memory model enforce the invariants of File Offsets, Sparse Files, and lseek under optimization (`-O2`/`-O3`)?
  2. 02What production bug or undefined behavior occurs when File Offsets, Sparse Files, and lseek is misused across module or thread boundaries, and which sanitizer (`ASan`, `UBSan`, `TSan`) catches it?

Not started

Sign in to save your learning progress.

Sign in to save