File I/O interview problems

File I/O questions are about reading and writing files the way production code does. You might tail a log, process a file too large for memory line by line, write output atomically, walk a directory tree, or merge sorted files. The tools are open with an explicit encoding, iterating the file object for lines, f.read(n) for fixed-size chunks, pathlib.Path with iterdir, rglob and stat, tempfile for scratch files, and os.replace for an atomic rename on the same filesystem.

Interviewers see f.read() on a file that might be gigabytes, files opened without with so handles leak on error, and text mode used where bytes were needed. They also see writes straight to the destination path, which leaves a truncated file if the process dies halfway. A strong answer writes to a temporary file in the same directory, calls flush and os.fsync if durability matters, then uses os.replace. It handles a final line without a newline and treats encoding errors deliberately with errors="replace" or a clear failure.

1 open problem

  1. 31Chunked File Copy with Bounded Memoryeasy

    Copy a file of any size in fixed-size chunks so memory stays flat, and refuse inputs that would destroy data.

8 locked

These open in order of demand. Open one and press I want this one to ask for it.

  1. 32File Chunk Iterator Lockedeasy

    Write a lazy generator that reads a byte range of a file in fixed-size chunks without ever loading the whole file into memory.

  2. 33Streaming File Checksum Lockedeasy

    Compute and verify a SHA-256 checksum of a large file by hashing it in chunks, rejecting malformed expected digests.

  3. 34Split and Merge Files Lockedmedium

    Split a large file into fixed-size numbered parts and merge parts back together, streaming in chunks with bounded memory.

  4. 35Atomic File Copy Lockedmedium

    Copy a file through a temp file and atomic rename so readers never see a partial destination, with progress callbacks and cleanup on failure.

  5. 41Atomic Async Download Lockedhard

    Save an async byte stream to disk atomically, enforcing an expected size and cleaning up on errors, callback failures and cancellation.

  6. 42Chunked Upload with Retries Lockedmedium

    Upload a large file in fixed-size chunks through an async send function, retrying each chunk independently and never loading the whole file.

  7. 44Resumable Download Lockedmedium

    Resume an interrupted file download from the bytes already on disk, append streamed chunks, report progress, and reject wrong-sized data.

  8. 46Disk-Backed LRU Artifact Cache Lockedmedium

    Build a byte-budgeted LRU cache that stores model artifacts as files on disk, evicting least recently used entries and keeping only metadata in memory.