Memory interview problems
Memory questions put a limit on how much you can hold and ask you to stay inside it. Process a file bigger than RAM, keep only the last N events, track distinct items in bounded space, or cut the per-object cost of a million records. The Python tools are generators instead of lists, collections.deque(maxlen=n), __slots__ on classes or @dataclass(slots=True), array.array for packed numbers, and sys.getsizeof and tracemalloc to measure rather than guess.
The common mistakes are building an intermediate list "for clarity" in the middle of a pipeline, holding references in a cache that never evicts, and assuming sys.getsizeof counts the objects a container references (it doesn't). Another is reaching for exact answers when the follow-up clearly wants an approximation. A strong answer states the memory bound in big-O before coding, picks a streaming algorithm that meets it, and knows the approximate structures by name. Those are reservoir sampling for a uniform sample, a Bloom filter for membership and HyperLogLog for distinct counts.
2 open problems
- 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.
- 36Delimited Stream Parsermedium
Turn an iterator of arbitrary byte chunks into delimited messages, handling messages and multi-byte delimiters split across chunks.
10 locked
These open in order of demand. Open one and press I want this one to ask for it.
- 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.
- 33Streaming File Checksum Lockedeasy
Compute and verify a SHA-256 checksum of a large file by hashing it in chunks, rejecting malformed expected digests.
- 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.
- 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.
- 37Stream Rechunker Lockedeasy
Regroup a stream of arbitrarily sized byte chunks into fixed-size chunks, lazily and with memory bounded by the chunk size.
- 40Length-Prefixed Message Parser Lockedmedium
Decode a stream of 4-byte big-endian length-prefixed messages from arbitrary chunks, rejecting oversized and truncated frames.
- 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.
- 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.
- 48API Key Usage Tracker Lockedmedium
Enforce a per-API-key sliding-window quota on both request count and tokens, rejecting requests that do not fit, with memory bounded by the window.
- 50Streaming Response Aggregator Lockedmedium
Assemble interleaved token, done and error events into complete responses with a lazy generator that keeps only in-flight requests in memory.