PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW

Quick Overview

This question evaluates algorithmic and systems engineering skills related to batch image processing, parallelization, I/O versus CPU trade-offs, and deterministic output association within the Coding & Algorithms domain.

  • medium
  • Anthropic
  • Coding & Algorithms
  • Software Engineer

Generate outputs for images and pipelines

Company: Anthropic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given **m input images** and **n processing pipelines**. - Each pipeline is an ordered list of **k operations** (e.g., resize, rotate, crop, blur, color transform). - Applying a pipeline to an image produces a new output image. ### Task Write a function/program that produces **m × n output images** by applying **every pipeline** to **every input image**. ### Requirements - Each output should be associated with `(image_id, pipeline_id)`. - The pipeline operations must be applied **in order**. - Assume images are stored as file paths; pipelines are provided as callable operations. - Your implementation should be structured so it can be **parallelized easily** (e.g., avoid unnecessary repeated work such as reading the same image multiple times). ### Input/Output (one reasonable interface) - Input: - `image_paths: List[str]` (size m) - `pipelines: List[List[Callable[[Image], Image]]]` (size n, each of length k) - `output_dir: str` - Output: - Write `m*n` images to `output_dir` with deterministic names like `img{idx}_pipe{j}.png`, or return a list of output file paths. ### Constraints (assume) - `m, n` can be large enough that runtime and I/O efficiency matter. - Operations may be CPU-bound; image decoding/encoding is I/O-bound. ### Follow-up discussion (no need to implement) - How would you scale this for large `m` and `n`? - What parallelization strategy would you choose and why?

Quick Answer: This question evaluates algorithmic and systems engineering skills related to batch image processing, parallelization, I/O versus CPU trade-offs, and deterministic output association within the Coding & Algorithms domain.

In this interview-friendly version of the image/pipeline problem, each image path maps to an integer image value in `image_store` instead of a real image object. Each pipeline is a list of operations that must be applied in order. For every image index `i` and pipeline index `j`, produce one output named `img{i}_pipe{j}.png` inside `output_dir`. Supported operations: - `('ADD', x)`: add `x` - `('SUB', x)`: subtract `x` - `('MUL', x)`: multiply by `x` - `('NEG',)`: negate the value Return a list of `(output_path, result_value)` tuples ordered first by image index, then by pipeline index. A pipeline may be empty, which means it leaves the image value unchanged. If the same image path appears multiple times in `image_paths`, outputs must still be produced for each position. Your approach should avoid unnecessary repeated work such as reloading the same image path or recomputing the same pipeline from scratch for every image.

Constraints

  • 0 <= len(image_paths), len(pipelines)
  • A pipeline may be empty
  • All paths in `image_paths` exist in `image_store`
  • The total number of operations across all pipelines is at most 2 * 10^5
  • The returned list has exactly `len(image_paths) * len(pipelines)` entries, so any solution must spend at least O(m*n) time to build the output

Examples

Input: (['a.png', 'b.png'], {'a.png': 2, 'b.png': 5}, [[('ADD', 3), ('MUL', 2)], [('NEG',), ('SUB', 4)]], 'out')

Expected Output: [('out/img0_pipe0.png', 10), ('out/img0_pipe1.png', -6), ('out/img1_pipe0.png', 16), ('out/img1_pipe1.png', -9)]

Explanation: For `a.png` (value 2), pipeline 0 gives `(2 + 3) * 2 = 10` and pipeline 1 gives `-2 - 4 = -6`. For `b.png` (value 5), the results are 16 and -9.

Input: (['x', 'x', 'y'], {'x': -3, 'y': 0}, [[], [('MUL', -1), ('ADD', 2)], [('SUB', 5), ('NEG',)]], 'res')

Expected Output: [('res/img0_pipe0.png', -3), ('res/img0_pipe1.png', 5), ('res/img0_pipe2.png', 8), ('res/img1_pipe0.png', -3), ('res/img1_pipe1.png', 5), ('res/img1_pipe2.png', 8), ('res/img2_pipe0.png', 0), ('res/img2_pipe1.png', 2), ('res/img2_pipe2.png', 5)]

Explanation: The first pipeline is empty, so it leaves values unchanged. The same path `x` appears twice, so outputs are produced twice with different image indices.

Input: ([], {}, [[('ADD', 1)]], 'tmp')

Expected Output: []

Explanation: There are no images, so no outputs are generated.

Input: (['z'], {'z': 4}, [[('ADD', 1), ('MUL', 3), ('NEG',), ('SUB', 2)]], 'tmp/')

Expected Output: [('tmp/img0_pipe0.png', -17)]

Explanation: The operations are applied in order: `4 -> 5 -> 15 -> -15 -> -17`. A trailing slash in `output_dir` should not change the final file name format.

Solution

def solution(image_paths, image_store, pipelines, output_dir):
    transforms = []
    for pipeline in pipelines:
        a, b = 1, 0
        for op in pipeline:
            name = op[0]
            if name == 'ADD':
                b += op[1]
            elif name == 'SUB':
                b -= op[1]
            elif name == 'MUL':
                factor = op[1]
                a *= factor
                b *= factor
            elif name == 'NEG':
                a = -a
                b = -b
            else:
                raise ValueError(f'Unsupported operation: {name}')
        transforms.append((a, b))

    loaded = {}
    base = output_dir.rstrip('/')
    result = []

    for i, path in enumerate(image_paths):
        if path not in loaded:
            loaded[path] = image_store[path]
        value = loaded[path]

        for j, (a, b) in enumerate(transforms):
            out_path = f'{base}/img{i}_pipe{j}.png' if base else f'img{i}_pipe{j}.png'
            result.append((out_path, a * value + b))

    return result

Time complexity: O(u + P + m*n), where `u` is the number of distinct image paths and `P` is the total number of operations across all pipelines. Space complexity: O(u + n) auxiliary space, excluding the returned output list.

Hints

  1. Each supported operation keeps the form `a*x + b`. Try compressing each whole pipeline into one pair `(a, b)` before processing the images.
  2. If the same path appears more than once in `image_paths`, cache its loaded value so you do not conceptually decode it multiple times.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Convert Samples into Event Intervals - Anthropic (medium)
  • Convert State Stream to Events - Anthropic (medium)
  • Build a concurrent web crawler - Anthropic (medium)
  • Implement a Parallel Image Processor - Anthropic (medium)
  • Implement a Batch Image Processor - Anthropic (medium)