Parse and Reconstruct Stack Trace
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates parsing and string-processing skills, hierarchical data structure design for call-tree reconstruction, exception trace analysis, and algorithmic complexity reasoning for robust log handling.
Part 1: Extract Frames from the Outermost Stack Trace Section
Constraints
- 0 <= len(trace) <= 200000
- The trace contains at most 10000 lines.
- A valid frame line must start with 'at ' after trimming and must end with '(file:line)' where line is a non-negative integer.
- Only the first exception section should be parsed; later 'Caused by:' sections are ignored.
Examples
Input: 'RuntimeError: boom\nat App::run(app.py:10)\nat Main::main(main.py:20)'
Expected Output: ['App::run(app.py:10)', 'Main::main(main.py:20)']
Explanation: Both frame lines are valid and belong to the outermost section.
Input: 'ValueError\nrandom text\n at Parser::parse(parser.py:7)\nat BadLine(nope)\nCaused by: IOError\nat IO::read(io.py:3)'
Expected Output: ['Parser::parse(parser.py:7)']
Explanation: The malformed line is skipped, and parsing stops before the cause section.
Input: ''
Expected Output: []
Explanation: Empty input has no frames.