Parse and Reconstruct Stack Trace
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: 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.
Hints
- Process the trace line by line and stop at the first 'Caused by:' line.
- You do not need a full parser; checking the position of '(', ':', and ')' is enough to validate a frame.
Part 2: Reconstruct a Merged Call Hierarchy Tree
Constraints
- 0 <= len(trace) <= 200000
- The trace contains at most 10000 lines.
- Each section contains at most one abbreviation line of the form '... N more'.
- A valid frame line must start with 'at ' after trimming and must end with '(file:line)' where line is a non-negative integer.
Examples
Input: 'Err\nat B::b(b.py:2)\nat A::a(a.py:1)'
Expected Output: {'A::a(a.py:1)': {'B::b(b.py:2)': {}}}
Explanation: A single stack becomes a single root-to-leaf path from oldest to newest.
Input: 'Outer\nat Service::run(s.py:30)\nat Controller::handle(c.py:20)\nat Main::main(m.py:10)\nCaused by: ParseError\nat Parser::parse(p.py:5)\n... 2 more'
Expected Output: {'Main::main(m.py:10)': {'Controller::handle(c.py:20)': {'Service::run(s.py:30)': {}, 'Parser::parse(p.py:5)': {}}}}
Explanation: The cause expands to [Parser::parse, Controller::handle, Main::main], creating a branch under Controller::handle.
Approach
Space complexity: O(T), for storing the reconstructed stacks (each frame string referenced) plus the merged forest, whose total node count is bounded by the number of inserted frames.
Hints
- First reconstruct each section's full frame list; only then build the hierarchy.
- A trie built from oldest to most recent frames naturally merges shared call prefixes.
Part 3: Find the Deepest Frame of the Thrown Exception
Constraints
- 0 <= len(trace) <= 200000
- The trace contains at most 10000 lines.
- Each section contains at most one abbreviation line of the form '... N more'.
- A valid frame line must start with 'at ' after trimming and must end with '(file:line)' where line is a non-negative integer.
Examples
Input: 'Error\nat B::work(b.py:2)\nat A::main(a.py:1)'
Expected Output: 'B::work(b.py:2)'
Explanation: With no nested cause, the thrown exception is the outermost section, and its deepest node is the most recent frame.
Input: 'Outer\nat Service::run(s.py:3)\nat Main::main(m.py:1)\nCaused by: Inner\nat Parser::parse(p.py:2)\n... 1 more'
Expected Output: 'Parser::parse(p.py:2)'
Explanation: The deepest cause expands to [Parser::parse, Main::main], so the deepest node is Parser::parse.
Approach
Time complexity: O(n + T), where n is the number of characters in the trace (one `splitlines` pass plus constant work per line) and T is the total number of frames across all reconstructed sections (each `... N more` can copy up to the parent's full length).
Space complexity: O(T) for the per-section frame lists and the reconstructed `full_sections` (which may duplicate inherited tail frames); bounded by the total reconstructed frame count.
Hints
- The thrown exception is the last section, not the first one.
- To expand '... N more', reuse the suffix of the previous section's reconstructed frame list.
Community answers
Answer by jayceeLOL