← J.P. Morgan Interview Insights

J.P. Morgan·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jun 2026

Summary

J.P. Morgan software engineer coding round, two problems back to back. Nothing brutal but the second one had more edge cases than it looked like at first glance.

Questions Asked (2)

Q1

Given a binary string of 0s and 1s, count how many substrings have equal numbers of 0s and 1s where all the 0s are grouped together and all the 1s are grouped together (like '0011', '10', '1100', etc.).

Algorithms & Data Structures
Author's notes

Pretty clean problem once you see the pattern.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the substrings must be of the form 0^a 1^a or 1^a 0^a, meaning they consist of a single block of 0s followed by a single block of 1s (or vice versa) with equal counts. Then, scan the string to identify maximal runs of identical characters and count valid substrings within each pair of adjacent runs by taking the minimum of their lengths. Alternatively, use a two-pointer sliding window to count all such substrings in O(n) time.

Pro tip: Demonstrate awareness of edge cases (e.g., empty string, all same characters) and discuss time/space complexity trade-offs. Mention that the problem reduces to counting valid substrings within adjacent runs, which can be done in a single pass.

1. Clarify the problem

Confirm that substrings must have all 0s grouped and all 1s grouped, and that the groups must be contiguous. Ask if overlapping substrings are counted.

2. Identify pattern

Recognize that valid substrings are exactly those that lie entirely within a concatenation of two adjacent runs of different characters, with equal length from each run.

3. Design algorithm

Choose an approach: either scan runs and sum min(len(run_i), len(run_{i+1})) for each adjacent pair, or use a two-pointer sliding window to count valid substrings directly.

4. Handle edge cases

Consider empty string, strings with only one character, and very long runs. Ensure the algorithm handles these efficiently.

5. Analyze complexity

State that the solution runs in O(n) time and O(1) extra space, which is optimal for this problem.

Key Points to Mention

  • Definition of valid substring: 0^a 1^a or 1^a 0^a
  • Reduction to counting within adjacent runs of different characters
  • Two-pointer sliding window technique for O(n) time
  • Time complexity: O(n), Space complexity: O(1)
  • Edge cases: empty string, all same characters, single character
  • Avoid brute-force O(n^3) approach; optimize to O(n)

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

Q2

Given an array of document lines where lines starting with '#' are chapters and lines starting with '##' are sections, generate a formatted table of contents with proper numbering like '1. Chapter Title' and '1.2. Section Title'.

Algorithms & Data Structures
Author's notes

Looked easy, and mostly was, but I kept second-guessing the section reset logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the input format and output requirements, then design a single-pass algorithm that tracks chapter and section counters. Use a stack or counters to manage hierarchical numbering, and handle edge cases like missing chapters or multiple sections per chapter.

Pro tip: Mention that you would validate the input and handle edge cases such as sections appearing before any chapter, or lines with extra spaces after the markers. This shows attention to detail and robustness, which is crucial in financial systems.

1. Clarify requirements and edge cases

Ask about input format (e.g., are there other markers? what about empty lines?), output format (e.g., indentation, numbering style), and edge cases (e.g., sections without chapters, multiple chapters).

2. Design the algorithm

Propose a single-pass solution using two counters: one for chapters and one for sections. Reset the section counter when a new chapter is encountered.

3. Implement the solution

Iterate through each line, check if it starts with '##' or '#', extract the title, increment the appropriate counter, and format the output string.

4. Handle edge cases

Decide how to handle sections before any chapter (e.g., ignore, assign to chapter 0, or throw error) and lines with extra spaces after markers.

5. Test and optimize

Walk through examples, test edge cases, and discuss time/space complexity (O(n) time, O(1) extra space).

Key Points to Mention

  • Single-pass O(n) time complexity with O(1) extra space
  • Use of counters for chapter and section numbering
  • Resetting section counter when a new chapter starts
  • Handling edge cases like sections before chapters or malformed lines
  • String manipulation techniques (e.g., startsWith, substring)
  • Output formatting with proper indentation and numbering

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