KatInterview

Lazy Pipelines, Memoization, Indexes, and Safe Payment Retries

System DesignModel answer
MediumModel answerSystem Designpythongeneratorsmemoization

Last updated

A multi-part fundamentals question covering lazy vs eager iteration, a multi-argument cache decorator, B+ tree index trade-offs, process vs thread, and testing idempotent retries.

Answer each of the following as you would in a technical conversation. Be concrete, give numbers where they help, and name the trade-offs.

Part 1: Python

  1. You must aggregate a field from a text file with 10 million rows. Compare building a list with a list comprehension against using a generator expression. What changes in memory use, time to first result, and reusability?
  2. Write a decorator that caches the return value of a function that takes several positional and keyword arguments. Explain how you build the cache key and what happens when the same call is repeated, for example compute(1, 2) invoked many times. Mention the edge cases you would handle.

Part 2: Databases

A Users table holds 100 million rows, and a query filters with WHERE email = '...'.

  1. Explain why a B+ tree index on email is far faster than a full table scan. Estimate the number of page reads in each case.
  2. Suppose five secondary indexes are added to the table. Discuss the effect on write latency and on disk and memory usage, and how you would decide which indexes to keep.

Part 3: Systems and testing

  1. Contrast a process with a thread in terms of memory isolation and context-switch cost.
  2. You own a payment module that retries calls after transient network failures. How would you test the retry logic, and how would you guarantee the operation is idempotent so a customer is never charged twice?

Hint on scope: separate what you verify with fast unit tests (logic with fakes) from what needs integration tests (real network behavior and faults).

Hints

Hint 1

For the file, ask what must be alive in memory at the same instant, and whether you need a second pass over the data.

Hint 2

A cache key must be hashable and must treat keyword-argument order as irrelevant. Think about what to do for unhashable inputs and unbounded growth.

Hint 3

Estimate index depth as log base fanout of the row count, and compare it with rows divided by rows per page for a scan.

Hint 4

The nastiest payment failure is when the request succeeds on the server but the response is lost. Design the idempotency key and a unique constraint around that case.