Anthropic software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
Background
Requirements
end events for the old suffix, innermost first, followed by start events for the new suffix, outermost first. All events use the new sample's timestamp. A frame's depth is its zero-based position in its stack; repeated function names represent distinct recursive frames. Matching prefix frames are assumed to remain active: do not infer unobserved calls between samples.finish(thread_id, timestamp) closes the thread by ending every remaining frame, innermost first. Its timestamp must be at least the thread's latest sample timestamp. Finishing an already finished thread returns false without emitting events or checking the timestamp. Finishing an unknown thread raises ValueError; adding a sample to a finished thread also raises ValueError.drain_events() returns and removes all buffered events. Each event is an object with exactly thread_id, timestamp, kind, function, and depth. Preserve method-call order, not global timestamp order; within a call, preserve the nesting order defined above. A second drain without intervening events returns an empty list. Copy input stacks so later caller mutations cannot change stored state.Class API
StackTraceReconstructor() — create an empty reconstructor.add_sample(thread_id: str, timestamp: int, stack: list[str]) -> None — register or update a thread and append inferred events; raise ValueError for a finished thread or a non-increasing timestamp.finish(thread_id: str, timestamp: int) -> bool — return true when an active thread is closed, or false if already closed; raise ValueError for an unknown thread or a timestamp earlier than its latest sample.drain_events() -> list[dict[str, str | int]] — atomically return all pending events and clear the buffer.Example 1
[
{"method":"add_sample","args":["worker-1",10,["serve","decode"]]},
{"method":"add_sample","args":["worker-1",14,["serve","sample"]]},
{"method":"finish","args":["worker-1",20]},
{"method":"drain_events","args":[]}
][
null,
null,
true,
[
{"thread_id":"worker-1","timestamp":10,"kind":"start","function":"serve","depth":0},
{"thread_id":"worker-1","timestamp":10,"kind":"start","function":"decode","depth":1},
{"thread_id":"worker-1","timestamp":14,"kind":"end","function":"decode","depth":1},
{"thread_id":"worker-1","timestamp":14,"kind":"start","function":"sample","depth":1},
{"thread_id":"worker-1","timestamp":20,"kind":"end","function":"sample","depth":1},
{"thread_id":"worker-1","timestamp":20,"kind":"end","function":"serve","depth":0}
]
]["serve"], so only decode ends and sample starts. Finishing ends the two remaining frames in reverse depth order.Example 2
[
{"method":"add_sample","args":["A",5,["walk","walk"]]},
{"method":"add_sample","args":["B",2,[]]},
{"method":"add_sample","args":["A",6,["walk"]]},
{"method":"add_sample","args":["B",3,["serve"]]},
{"method":"finish","args":["A",6]},
{"method":"finish","args":["A",0]},
{"method":"drain_events","args":[]},
{"method":"drain_events","args":[]}
][
null,
null,
null,
null,
true,
false,
[
{"thread_id":"A","timestamp":5,"kind":"start","function":"walk","depth":0},
{"thread_id":"A","timestamp":5,"kind":"start","function":"walk","depth":1},
{"thread_id":"A","timestamp":6,"kind":"end","function":"walk","depth":1},
{"thread_id":"B","timestamp":3,"kind":"start","function":"serve","depth":0},
{"thread_id":"A","timestamp":6,"kind":"end","function":"walk","depth":0}
],
[]
]finish is a no-op even with an earlier timestamp.Constraints
[0, 10^12]; thread IDs and function names are nonempty strings of at most 128 characters. Types and these bounds are guaranteed by callers.set(file: str, data: bytes) -> None to store a file.get(file: str) -> Optional[bytes] to retrieve file data by name.filter(criteria: Callable[[str], bool]) -> List[str] to return a list of files matching certain conditions.backup(destination: str) -> None to backup all files to a specified location.restore(source: str) -> None to restore files from a backup location.Class API:set(file: str, data: bytes) -> None: Stores the file file with the content data in bytes.get(file: str) -> Optional[bytes]: Returns the content of the file if it exists, otherwise returns None.filter(criteria: Callable[[str], bool]) -> List[str]: Returns a list of file names that match the given criteria.backup(destination: str) -> None: Backs up the current files to the specified destination.restore(source: str) -> None: Restores files from a backup source.set('document.txt', b'This is a document.') set('image.png', b'Image data here.') get('document.txt') returns b'This is a document.' filter(lambda x: x.endswith('.png')) returns ['image.png'] set is used to store files and get retrieves the content of a specific file. The filter method uses a lambda function to find files with the .png extension.Example 2: backup('/backup/location') restore('/backup/location') Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free