Merge overlapping and touching intervals in a ledger statement

Quick Overview

Merge the time intervals in a ledger statement so that every pair of overlapping or touching ranges becomes one, and return the merged ranges sorted by start time. It tests sorting, a single linear sweep, and careful handling of shared endpoints and nested intervals.

Merge overlapping and touching intervals in a ledger statement

Company: Nubank

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a ledger statement represented as a 2D array `entries`, where each `entries[i] = [start_i, end_i]` denotes a time interval during which a transaction was active in the ledger. Some intervals may overlap or be adjacent. Your task is to merge all overlapping or touching intervals so the ledger contains only non-overlapping time ranges that together cover exactly the same total span as the original entries. Return the resulting list of merged intervals sorted by start time in ascending order. ### Function Signature ```python def merge_ledger(entries: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each interval `[start, end]` is closed, so it includes both endpoints. - Two intervals that share an endpoint, such as `[1, 4]` and `[4, 5]`, count as touching and must be merged. - The entries can arrive in any order. - Return each merged interval as a two-element list `[start, end]`, with the list sorted by `start` ascending. Merged intervals never overlap, so this order is unique. ### Constraints - `1 <= len(entries) <= 10^4` - `len(entries[i]) == 2` - `0 <= entries[i][0] < entries[i][1] <= 10^9` ### Examples ```text Input: entries = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] ``` Intervals `[1,3]` and `[2,6]` overlap and merge into `[1,6]`. `[8,10]` and `[15,18]` each start after the previous range ends, so they remain separate. ```text Input: entries = [[1,4],[4,5]] Output: [[1,5]] ``` Intervals `[1,4]` and `[4,5]` are adjacent (touching at 4) and merge into `[1,5]`. ```text Input: entries = [[8,9],[1,10],[2,3]] Output: [[1,10]] ``` The entries are not sorted. Both `[2,3]` and `[8,9]` lie inside `[1,10]`, so the whole statement covers the single range `[1,10]`.

Overview: Merge the time intervals in a ledger statement so that every pair of overlapping or touching ranges becomes one, and return the merged ranges sorted by start time. It tests sorting, a single linear sweep, and careful handling of shared endpoints and nested intervals.

Nubank logo
Nubank
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a ledger statement represented as a 2D array entries, where each entries[i] = [start_i, end_i] denotes a time interval during which a transaction was active in the ledger.

Some intervals may overlap or be adjacent. Your task is to merge all overlapping or touching intervals so the ledger contains only non-overlapping time ranges that together cover exactly the same total span as the original entries.

Return the resulting list of merged intervals sorted by start time in ascending order.

Function Signature

def merge_ledger(entries: list[list[int]]) -> list[list[int]]:

Rules

  • Each interval [start, end] is closed, so it includes both endpoints.
  • Two intervals that share an endpoint, such as [1, 4] and [4, 5] , count as touching and must be merged.
  • The entries can arrive in any order.
  • Return each merged interval as a two-element list [start, end] , with the list sorted by start ascending. Merged intervals never overlap, so this order is unique.

Constraints

  • 1 <= len(entries) <= 10^4
  • len(entries[i]) == 2
  • 0 <= entries[i][0] < entries[i][1] <= 10^9

Examples

Input: entries = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]

Intervals [1,3] and [2,6] overlap and merge into [1,6]. [8,10] and [15,18] each start after the previous range ends, so they remain separate.

Input: entries = [[1,4],[4,5]]
Output: [[1,5]]

Intervals [1,4] and [4,5] are adjacent (touching at 4) and merge into [1,5].

Input: entries = [[8,9],[1,10],[2,3]]
Output: [[1,10]]

The entries are not sorted. Both [2,3] and [8,9] lie inside [1,10], so the whole statement covers the single range [1,10].