Interview Questions/Coding/Range Stream Kth Signal

Range Stream Kth Signal

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Range Stream Kth Signal

A monitoring service expands several inclusive integer ranges into one combined stream. If ranges overlap, duplicate emitted values are kept because they came from different ranges. After all range values are emitted, the stream is sorted in non-decreasing order. You must identify the k-th value in this sorted stream, using one-based indexing. Input Format: You are given an array ranges and an integer k. Each range is [left, right]. Output Format: Return the k-th smallest emitted value.

Examples

Example 1
Input:
ranges = [[1, 3], [5, 7]]
k = 4
Output:
5

Explanation: Expanded and sorted, the stream is [1, 2, 3, 5, 6, 7]. The 4th value is 5, so the output is 5.

Approach hint

Binary search the answer value.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp