← 返回 anthropic 的题目列表Image Processing Pipeline (Grayscale / Scale / Resize) with Performance Optimization
类型:online_judge
Problem: Build an Image Processing Pipeline (Grayscale / Scale / Resize) and Optimize Performance
You are given a set of input images and a list of image-processing operations. Implement a program that applies the operations in order to each image and outputs the processed images.
The pipeline must support:
grayscale: Convert a color image to grayscale.
scale: Uniformly scale the image by a given factor.
resize: Resize the image to a target width and height.
The interview has two stages:
Stage 1: Correctness (small images)
Implement a correct version that:
Reads input images
Applies the operations sequentially
Outputs the processed images
Stage 2: Performance (large images / many images)
Then optimize for larger images or higher throughput. You may use strategies such as:
Multi-process parallelism across images
Reducing unnecessary copies / intermediates
Improving I/O patterns
Your code should reflect the optimization (e.g., using ProcessPoolExecutor).
Input format (sample convention; exact details may be clarified during the interview)
Read from stdin:
First line: integer N, number of images.
Next N lines: each a path (or local identifier) to an image.
Next line: integer M, number of operations.
Next M lines: one operation per line, one of:
grayscale
scale <factor> (e.g., scale 0.5)
resize <width> <height> (e.g., resize 800 600)
Output format
For each image, write the processed image to an output directory (or print the output path / success marker; to be aligned with the interviewer’s expectation).
Scale (discussion points)
Small images: e.g., 100x100 to 512x512
Large images: e.g., 4K/8K, or thousands of images in batch
Sample test cases (validated via output filenames)
Note: Image bytes are not easy to assert via plain text, so these examples validate stdout by printing output file paths. In interviews, real image files are usually used.
Single image + grayscale
input:
1
input/a.jpg
1
grayscale
output:
output/a_grayscale.jpg
Single image + scale
input:
1
input/a.jpg
1
scale 0.5
output:
output/a_scale_0.5.jpg
Single image + resize
input:
1
input/a.jpg
1
resize 800 600
output:
output/a_resize_800x600.jpg
Single image + chained operations
input:
1
input/a.jpg
3
grayscale
scale 0.5
resize 200 200
output:
output/a_grayscale_scale_0.5_resize_200x200.jpg
Multiple images + parallel processing (performance stage)
input:
3
input/a.jpg
input/b.jpg
input/c.jpg
2
grayscale
resize 1024 768
output:
output/a_grayscale_resize_1024x768.jpg
output/b_grayscale_resize_1024x768.jpg
output/c_grayscale_resize_1024x768.jpg
Example
Input
1
input/a.jpg
1
grayscale
Output
output/a_grayscale.jpg