Image transform pipeline with a wall-clock budget
Apply a fixed sequence of image transforms (grayscale, flip, scale, blur, rotate) to every image in a directory, then speed the batch up with threads or processes to hit a wall-clock target. Expect questions about what parallelizes and what does not.
Images here are lists of rows of (r, g, b) tuples, so the code runs anywhere.
def grayscale(img): ...
def flip(img): ... # mirror left to right
def scale(img, factor): ... # nearest neighbour
def blur(img): ... # 3x3 box blur, edges clamped
def rotate(img): ... # 90 degrees clockwise
def apply_steps(img, steps=None):
"""Apply [(name, kwargs), ...]; default: grayscale, flip, scale 0.5, blur, rotate."""
def run_batch(images, steps=None, budget_s=None, mode="process", workers=4):
"""{name: image} -> (results, sorted skipped names); mode is serial, thread or process."""
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
