TCP exposes an ordered byte stream even though network segments can arrive out of order, overlap, or be duplicated. A receiver can buffer later bytes, but it can advance its acknowledgment only through the contiguous prefix with no gap.
Design a TcpSegmentReassembler class:
TcpSegmentReassembler() creates a stateless reassembler.int nextExpected(int[][] segments) returns the first byte sequence number that has not been received contiguously from byte 0.
Each segment is [seq, length] and covers the half-open interval [seq, seq + length). The input can be out of order and intervals can overlap. Do not mutate segments, and treat each call independently.
Example 1:
Input:
Output:
Explanation: The first segment covers bytes [0,3). The second covers [5,7), leaving a gap at bytes 3 and 4, so the receiver still expects byte 3.
Example 2:
Input:
Output:
Explanation: Arrival order does not matter. Sorting reveals [0,3) followed immediately by [3,5), so all bytes before 5 are present.
Constraints
0 <= segments.length <= 10^5segments[i].length == 20 <= segments[i][0] <= 10^91 <= segments[i][1] <= 10^9segments[i][0] + segments[i][1] <= 2 * 10^9- At most
100 calls are made to nextExpected.