← J.P. Morgan Interview Insights

J.P. Morgan·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Went through a coding round for a Software Engineer position at J.P. Morgan. One algorithmic problem, interval merging, which sounds straightforward until you actually have to handle the edge cases cleanly under pressure.

Questions Asked (1)

Q1

Given an array of intervals (each with a start and end), merge all overlapping intervals, including ones that only touch at a single point, and return the resulting list sorted by start value.

Algorithms & Data Structures
Author's notes

I knew to sort first, which helped, but I fumbled on the touching-endpoint case for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that intervals touching at a single point should be merged (e.g., [1,2] and [2,3] become [1,3]). Then sort intervals by start time and iterate through them, merging when the current interval's start is less than or equal to the last merged interval's end. Finally, return the merged list, which is already sorted by start.

Pro tip: Mention that sorting is the key to achieving O(n log n) time, and that in-place merging can save space if the input can be modified. Also, discuss edge cases like empty input or single interval to show thoroughness.

1. Clarify requirements and edge cases

Confirm that intervals touching at a single point should be merged, and ask about input format, output format, and constraints. Consider edge cases: empty array, single interval, all overlapping, none overlapping.

2. Sort intervals by start time

Sort the intervals based on their start values. This ensures that any overlapping intervals will be adjacent, simplifying the merging process.

3. Iterate and merge

Initialize a result list with the first interval. For each subsequent interval, if its start is less than or equal to the end of the last interval in the result, merge them by updating the end to the maximum of the two ends. Otherwise, add the interval to the result.

4. Return the merged list

After processing all intervals, return the result list. Since we sorted by start and merged in order, the result is already sorted by start.

5. Analyze complexity and discuss optimizations

State that time complexity is O(n log n) due to sorting, and space complexity is O(n) for the output (or O(1) extra if merging in-place). Mention that in-place merging is possible if the input can be modified.

Key Points to Mention

  • Sorting by start time is crucial for O(n log n) efficiency.
  • Merging condition: current.start <= lastMerged.end (to include touching intervals).
  • Updating the end to max(lastMerged.end, current.end) when merging.
  • Handling edge cases: empty input, single interval, intervals with same start.
  • Time complexity: O(n log n) due to sorting; space complexity: O(n) for output.
  • In-place merging to achieve O(1) extra space if input modification is allowed.

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