Practice problem · Anthropic · Medium

Sampling Profiler

A sampling profiler does not watch every call. 3 stages in Python written by you, with an assistant that only answers. Stage 1 free.

Stage 1 of 3: Snapshots to calls

Background. A sampling profiler does not watch every call. Every so often it pauses the program and writes down the call stack at that moment, with the time. From those snapshots alone you can rebuild a trace: when each call started and when it returned, as far as the samples can tell.

Implement calls_from_samples(samples) in profiler.py.

samples is a list of (time, stack) pairs in the order they were taken. stack is a list of function names from the outermost call (index 0) to the one running at that moment; it may be empty. Times never decrease, and two samples in a row may carry the same time.

Return a list of events, each ("start", time, name) or ("end", time, name):

  • Before the first sample the stack is empty.
  • Compare each sample with the one before it from the bottom up. The frames the two stacks share, up to the first position where they differ, are calls still running. Every frame above that point in the earlier stack has ended, and every frame above it in the later stack has started, all at the later sample's time.
  • At each sample, list its ends first, innermost first, then its starts, outermost first. Samples are handled in order.
  • A function that appears twice on one stack (it called itself) is two calls.
  • Calls still on the stack at the last sample have no end event.

Example

time  stack
 0    main
10    main parse
20    main parse read
30    main render
30    main render render
40    (empty)
50    main

returns

("start", 0, "main"), ("start", 10, "parse"), ("start", 20, "read"),
("end", 30, "read"), ("end", 30, "parse"), ("start", 30, "render"),
("start", 30, "render"),
("end", 40, "render"), ("end", 40, "render"), ("end", 40, "main"),
("start", 50, "main")

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: profiler.py
  • Read-only: all tests/test_*.py files
  • A sample is (time, stack); a stack lists function names bottom first, so its last name is the one running

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Calls that last. One snapshot proves little. Report a call only once it has lasted several samples, and end it only once it has really gone.
  2. Stage 3: One sample at a time. The samples never stop coming. Report each call as soon as it is certain, without holding on to the past.

The files you get

  • Yours to edit: profiler.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 Sampling Profiler free?

Stage 1 is free with an account. The other 2 stages need Nyrion Pro.

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 first stage of this problem is free.

Start this problem → Sign in

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