KatInterview

Scaling a Solved Algorithm When the Data Outgrows Memory

System DesignModel answer
MediumModel answerSystem Designscalabilitystreamingexternal-sort

Last updated

Explain how to adapt a working in-memory solution when the input becomes far too large, moving from constraints to streaming, external sorting, approximation or distribution.

You have just finished a correct, in-memory solution to a typical algorithm problem, such as merging intervals, counting or grouping records, or sorting. The interviewer then asks:

"Suppose the input were enormous. What would you change?"

Write the answer a strong candidate would give. It should be a structured way of reasoning from the constraints to a design, not a list of buzzwords.

Your answer should:

  1. Name the clarifying questions you would ask first, and explain why each one changes the design.
  2. Walk through the main strategies in a sensible order of escalation: tightening the in-memory solution, streaming, chunk-and-merge, external-memory techniques, approximation, and finally distribution across machines.
  3. Say when each strategy applies and what it costs in correctness, memory, I/O, latency and complexity.
  4. Show how the approach works on at least one concrete example, such as interval merging or top-k frequent items.
  5. Avoid jumping to "use a distributed system" without first justifying why a single machine is not enough.

Hints

Hint 1

Start by quantifying: how many bytes is the input, how much memory do you have, and is the data static, streamed or changing? The answer decides which strategy is even needed.

Hint 2

Ask what the output actually needs. If it is a small summary (a count, a max, top-k), compact running state may be enough. If it needs a global ordering, you need sorting or an index.

Hint 3

Order your options by escalation: better in-memory layout, one-pass streaming, chunk and merge, external sort, approximate sketches, and only then multi-machine partitioning. Mention the cost of each step up.