← Questions
What if the input is too big for memory
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked May 2025. It tests scalability and external memory.
You have just solved an in-memory coding problem (for example interval merging, grouping, counting or sorting), and the interviewer asks: "What would you do if the input were extremely large?" Give a structured answer that goes from the constraints to the design, instead of jumping to "use a distributed system".
What to cover
- Does it still fit? Estimate the size. If it fits with care, cut memory first: compact types, in-place algorithms, not materialising intermediate lists, generators.
- Streaming: when the answer needs only compact state (a count, a max, a running aggregate), read once and keep that state.
- Chunk and merge: when the problem decomposes, process pieces that fit and combine the partial results (associative merges such as sum, max, top-k with heaps).
- External memory: when global order or grouping is needed, use external sort (sorted runs, then a k-way merge) or hash partitioning to disk so each partition fits; disk-backed indexes for repeated lookups.
- Approximation: when an exact answer is too costly and allowed: Count-Min sketch, HyperLogLog, Bloom filters, reservoir sampling, quantile sketches.
- Distribution: only when one machine's disk, I/O or time is not enough; partition by key, and handle skew.
Clarifying questions
- Does the whole input fit in memory? On disk on one machine?
- Is the input static, streamed, or continuously updated?
- Is an exact answer required, or is an approximation acceptable?
- Does the problem need global ordering, random access, or only a summary?
- What is the binding limit: memory, disk I/O, network, latency or throughput?
What a strong answer covers
- Asking the questions above first, then picking the cheapest technique that works, with its cost in passes over the data and memory.
- Applying it to the problem just solved, concretely: for merging intervals, external sort by start then one streaming merge; for counting, hash partition then count per partition.
- I/O awareness: sequential reads are cheap and random reads are not; the number of passes matters more than CPU.
- Skew: one hot key that doesn't fit its partition, and how to split it.
- How to test chunked processing: compare against the in-memory version on small inputs with tiny chunk sizes, so boundaries are exercised.
Follow-ups
- The problem needs sorting. Walk through external merge sort and its cost.
- One key has half of all records. What breaks?
- Which approximate structures would help for "how many distinct users" and "top 10 queries"?
Where it's asked
- Google · Onsite coding · Last asked May 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
scalability · external memory
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
