← JP Morgan Interview Insights

JP Morgan·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

JP Morgan software engineer interview with a coding question that looked deceptively simple on the surface. It's basically a string parsing problem but the edge cases pile up fast if you're not careful about state.

Questions Asked (1)

Q1

Given an array of strings representing document lines, build a table of contents: lines starting with '# ' are chapters, lines starting with '## ' are sections under the most recent chapter, everything else is ignored. Number chapters as 1, 2, 3... and sections as 1.1, 1.2... Return the result as an ordered array of strings.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The numbering logic tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the exact formatting rules and edge cases, then propose a single-pass solution that tracks the current chapter number and section counter. Walk through the algorithm with a small example, and discuss time/space complexity and potential trade-offs.

Pro tip: Mention that you would validate the input format and handle edge cases like sections before any chapter or multiple consecutive chapters, showing attention to robustness. Also, note that the output should be an array of strings, not a nested structure, to match the expected return type.

1. Clarify requirements and edge cases

Ask about the exact prefix rules (e.g., '# ' vs '#' without space), whether lines can have leading whitespace, and how to handle sections before any chapter. Confirm the output format and numbering scheme.

2. Outline the algorithm

Propose a single-pass approach: iterate through lines, maintain a chapter counter and a section counter. When a chapter is found, increment chapter counter, reset section counter, and add the chapter title with its number. When a section is found, increment section counter and add the section title with the current chapter number and section number.

3. Walk through an example

Use a small example to demonstrate the algorithm, showing how the counters update and how the output array is built. This helps verify correctness and catch off-by-one errors.

4. Analyze complexity and trade-offs

State that the solution is O(n) time and O(1) extra space (excluding output), and discuss alternative approaches like two-pass or using regex, highlighting why single-pass is optimal.

5. Discuss error handling and extensions

Mention how to handle malformed input (e.g., sections without chapters) and how the solution could be extended to support deeper nesting or different numbering styles.

Key Points to Mention

  • Single-pass iteration with O(n) time complexity
  • Maintaining chapter and section counters, resetting section counter on new chapter
  • Handling edge cases: sections before any chapter, multiple chapters, empty input
  • Output as an array of strings with proper numbering (e.g., '1. Chapter Title', '1.1 Section Title')
  • Trade-offs: single-pass vs. two-pass, regex vs. manual parsing
  • Robustness: input validation and clear assumptions

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.