Practice problem · Anthropic · Medium

Image Processing

pictures.py is a small read-only picture library. 2 stages in Python written by you, with an assistant that only answers. Pro.

Stage 1 of 2: A batch of pictures

Background. pictures.py is a small read-only picture library. It reads and writes binary netpbm files (.pnm), gray ("L") or color ("RGB"), and transforms pictures. Its docstring is the reference for every call below.

Implement process_images(paths, out_dir, steps) in batch.py. For each path, load the picture, apply steps to it in order, and save the result in out_dir. Return a list with one entry per path.

| Step | Library call | What it does | |------|--------------|--------------| | ("grayscale",) | picture.gray() | a gray copy | | ("scale", factor) | picture.scaled(factor) | both sides times factor, rounded | | ("resize", width, height) | picture.resized(width, height) | exactly width by height | | ("rotate", degrees) | picture.rotated(degrees) | a quarter, half or three-quarter turn counterclockwise for 90, 180 or 270 |

Load a picture with pictures.load(path) and save one with picture.save(path). Every transform returns a new picture.

  • Each step applies to the previous step's result. With no steps, the picture is saved as loaded.

  • Save each result as os.path.join(out_dir, os.path.basename(path)), replacing any file already there. No two paths in one call share a file name.

  • out_dir may not exist yet: create it, with its parents, also when paths is empty. It may also exist already.

  • Each entry, in the order of paths, is that output path, os.path.join(out_dir, os.path.basename(path)), for a picture that was saved, or the exception that stopped it, as the library raised it:

    • FileNotFoundError for a missing file;
    • pictures.FormatError, a ValueError, for a file that is not a picture;
    • ValueError for a step the picture cannot take: a scale that leaves a side below one pixel, or an angle other than 90, 180 or 270.

    Nothing is saved for a picture that failed, and the pictures after it go on.

Example

process_images(["in/cat.pnm", "in/gone.pnm"], "out", [("grayscale",), ("resize", 32, 24), ("rotate", 90)])
# ["out/cat.pnm", FileNotFoundError(2, 'No such file or directory')]

in/cat.pnm is a 64 by 48 color picture, so out/cat.pnm is gray, 24 wide and 32 high. in/gone.pnm does not exist, so nothing is saved for it.

Notes

  • paths is a list of strings and steps a list of tuples, each in one of the four shapes above, with width and height at least 1. An angle may still be one rotated refuses, and a factor one that leaves a side below one pixel.
  • The pictures in the tests are at most 64 by 64.
  • On the starter, all 10 tests in tests/test_part1.py fail.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: batch.py
  • Read-only: pictures.py, tests/test_helpers.py and all tests/test_*.py files
  • Steps are always well formed, and no two paths in one call share a file name

What comes after stage 1

The next stage opens once the tests for the one before it pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Large pictures, several cores. The pictures are large and one core is too slow; keep several busy at once and still report every result against its input.

The files you get

  • Yours to edit: batch.py.
  • Read-only: pictures.py, tests/test_helpers.py.
  • Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.

The round: no AI, an assistant that only answers

Anthropic runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.

Frequently asked, briefly answered

Is Image Processing free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

Anthropic. It is modelled on Anthropic's coding round, which runs without AI. Based on candidates' reports of Anthropic interviews. Nyrion wrote this version, so the question you get may differ.

Do I bring my own AI tool?

No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.

What does the interviewer ask?

4 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.

Start this problem → Sign in

More like it: every practice problem, Anthropic's interviews, the AI coding interview guide.