Given a list of intervals where each interval is a list of two integers [start, end], merge all overlapping intervals and return the merged list sorted by start value. Two intervals overlap when the start of one is less than or equal to the end of the other (touching intervals such as [1,4] and [4,5] are merged). Return an empty list for a null or empty input.
Examples:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: [1,3] and [2,6] overlap and merge into [1,6]
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Touching intervals are merged
Input: intervals = [[1,4],[2,3]]
Output: [[1,4]]
Explanation: [2,3] is fully contained in [1,4]
Input: intervals = [[1,2],[3,4]]
Output: [[1,2],[3,4]]
Explanation: The intervals do not overlap
Input: intervals = null
Output: []
Explanation: Null input returns an empty list
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay October 2026 (Oct 16–31)Sponsored by FLXBL
Contest runs October 16 - 31. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 16 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.