KatInterview

Batch-Apply Transform Chains to a Folder of Photos

System DesignModel answer
MediumModel answerSystem Designparallelismbatch-processingimage-processing

Last updated

Apply every transform chain to every photo, in order, with deterministic output names, no repeated decoding, and a design that parallelizes cleanly.

You have a list of m photos (given as file paths) and a list of n transform chains. Each chain is an ordered list of k callables, and each callable takes an image and returns a new image (for example resize, rotate, crop, blur, or a colour adjustment).

Task

Write a function that produces m × n result images by running every chain on every photo.

Requirements

  • Every result must be identifiable by the pair (image_id, pipeline_id).
  • The operations inside a chain must run strictly in the listed order.
  • Photos are read from disk; chains are plain callables.
  • Structure the code so it can be parallelized easily. In particular, avoid redundant work such as decoding the same file more than once.

Suggested interface

  • Inputs:
    • image_paths: List[str] (length m)
    • pipelines: List[List[Callable[[Image], Image]]] (length n, each of length k)
    • output_dir: str
  • Output: write m*n files into output_dir using deterministic names such as img{i}_pipe{j}.png, and/or return the list of written paths.

Assumptions

  • m and n may be large enough that runtime and I/O efficiency matter.
  • Operations can be CPU-heavy; decoding and encoding files is I/O-heavy.

Discussion (no implementation needed)

  • How would you scale this to very large m and n?
  • Which parallelization strategy would you choose, and why?

Give your approach, its complexity, and code, and mention the edge cases you would handle.

Hints

Hint 1

The result set is a Cartesian product. Think about which loop should be outer so that expensive per-photo work (decoding) happens once per photo rather than once per (photo, chain) pair.

Hint 2

Operations might modify their input in place. What must you do before running each chain so that chains do not contaminate each other?

Hint 3

Name outputs from indices, not from completion order or file contents. Also think about how to make writes safe if a task crashes and is retried, and how to avoid re-sending the chains to workers with every task.