FastPrepMerge Intervals
Problems/Merge Intervals

Merge Intervals

Morgan Stanley logoMorgan Stanley● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in Morgan Stanley online assessments for interns. Both reports name interval merging. We chose closed endpoints, sorted output, what happens for empty input, and the number limits.

Our version is about 90% the same as the reported questions.

Each row of intervals is a closed interval [start, end]. Merge every overlapping group into one interval and return the merged intervals sorted by increasing start.

For this exercise, assume endpoints are inclusive: intervals that touch at an endpoint overlap. Intervals cover real points between their integer endpoints; disjoint intervals are not merged merely because their integer endpoints differ by one. The result must cover exactly the same points as the input, with no two output intervals overlapping. An empty input returns an empty array.

Function

mergeIntervals(intervals: int[][]) → int[][]

Examples

Example 1

intervals = [[8,9],[1,4],[4,5]]return = [[1,5],[8,9]]

Closed intervals [1,4] and [4,5] share the point 4, so they merge into [1,5]. The interval [8,9] is separate. Return the intervals in start order.

Example 2

intervals = [[3,3],[1,3],[2,2]]return = [[1,3]]

The point intervals [3,3] and [2,2] are already contained in [1,3], so the union is the single interval [1,3].

Constraints

  • For this exercise, assume 0 <= intervals.length <= 2000.
  • Each interval has exactly two endpoints.
  • For this exercise, assume -1000000 <= endpoints <= 1000000.
  • Every interval satisfies start <= end.

More Morgan Stanley Online Assessment questions

  • Three-Resource 0/1 KnapsackOA · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
  • Count Shipment SchedulesOA · Seen Apr 2026
  • Delete and EarnOA · Seen Apr 2026
  • Interleave Array HalvesOA · Seen Apr 2026
  • Get Patch SequenceOA · Seen Feb 2025
  • String Subsequences 🐳OA · Seen Mar 2024
  • Binary Tree Level OrderOA · Seen Feb 2024
  • Format Chemical SymbolsOA · Seen Feb 2023

Similar questions at other companies

  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
  • Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
  • Minimum Meeting RoomsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN
  • Macaron Order PriceOpenAI · OAOpenAI interview questions

Interview reports that mention this question

  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)

Morgan Stanley interview experiences

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int[][] mergeIntervals(int[][] intervals) {
        // Write your code here.
    }

}
Checking account…