Merge overlapping user sessions

Quick Overview

Merge a list of user sessions so that any that overlap, including sessions that share a single timestamp, become one, and return them sorted by start time. It tests sorting, a one-pass sweep, and a precise overlap check with zero-length and nested sessions.

Merge overlapping user sessions

Company: Nubank

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array `sessions` where `sessions[i] = [start_i, end_i]` represents the start and end timestamps of a user session. Two sessions are considered to overlap if each one starts at or before the other one ends. Merge all overlapping sessions and return an array of the merged sessions such that each session in the result is non-overlapping and they collectively cover all the time ranges in the input. Return the merged sessions sorted by their start time in ascending order. ### Function Signature ```python def merge_sessions(sessions: list[list[int]]) -> list[list[int]]: ``` ### Rules - Sessions `[a, b]` and `[c, d]` overlap when `a <= d` and `c <= b`. - Sessions that touch at a single timestamp, such as `[1, 4]` and `[4, 5]`, count as overlapping. - A session can have zero length (`start_i == end_i`). - The input can be in any order. - Return each merged session as a two-element list `[start, end]`, with the list sorted by `start` ascending. Merged sessions never overlap, so this order is unique. ### Constraints - `1 <= len(sessions) <= 10^4` - `len(sessions[i]) == 2` - `0 <= start_i <= end_i <= 10^9` ### Examples ```text Input: sessions = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] ``` Sessions `[1,3]` and `[2,6]` overlap and merge into `[1,6]`. `[8,10]` and `[15,18]` remain separate. ```text Input: sessions = [[1,4],[4,5]] Output: [[1,5]] ``` Sessions `[1,4]` and `[4,5]` touch at time 4 and are considered overlapping, so they merge into `[1,5]`. ```text Input: sessions = [[6,8],[1,2],[5,5],[2,4]] Output: [[1,4],[5,5],[6,8]] ``` After sorting, `[1,2]` and `[2,4]` share time 2 and merge into `[1,4]`. The zero-length session `[5,5]` starts after 4, so it stays on its own, and `[6,8]` starts after 5.

Overview: Merge a list of user sessions so that any that overlap, including sessions that share a single timestamp, become one, and return them sorted by start time. It tests sorting, a one-pass sweep, and a precise overlap check with zero-length and nested sessions.

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

You are given an array sessions where sessions[i] = [start_i, end_i] represents the start and end timestamps of a user session. Two sessions are considered to overlap if each one starts at or before the other one ends.

Merge all overlapping sessions and return an array of the merged sessions such that each session in the result is non-overlapping and they collectively cover all the time ranges in the input.

Return the merged sessions sorted by their start time in ascending order.

Function Signature

def merge_sessions(sessions: list[list[int]]) -> list[list[int]]:

Rules

  • Sessions [a, b] and [c, d] overlap when a <= d and c <= b .
  • Sessions that touch at a single timestamp, such as [1, 4] and [4, 5] , count as overlapping.
  • A session can have zero length ( start_i == end_i ).
  • The input can be in any order.
  • Return each merged session as a two-element list [start, end] , with the list sorted by start ascending. Merged sessions never overlap, so this order is unique.

Constraints

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

Examples

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

Sessions [1,3] and [2,6] overlap and merge into [1,6]. [8,10] and [15,18] remain separate.

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

Sessions [1,4] and [4,5] touch at time 4 and are considered overlapping, so they merge into [1,5].

Input: sessions = [[6,8],[1,2],[5,5],[2,4]]
Output: [[1,4],[5,5],[6,8]]

After sorting, [1,2] and [2,4] share time 2 and merge into [1,4]. The zero-length session [5,5] starts after 4, so it stays on its own, and [6,8] starts after 5.