Concurrent Sorted-Stream Merger
Design and implement incremental sorted merging through a supplied unsafe blocking client, with bounded overlapping background reads and a consumer that never waits for stream I/O.
Design and implement a merger over K individually sorted streams using a supplied StreamClient. For this exercise, assume request(streamId) returns promptly after starting a background read; next(streamId) consumes the prior request and blocks until its result is available.
The shared client is not thread-safe: its method calls must not overlap, although background reads started by separate requests may overlap. At most n stream reads may be in flight.
Aim to admit eligible reads together rather than waiting for each read before starting another; a blocking next can nevertheless delay further client calls, so the interface does not promise continuous maximum utilization.
For this exercise, assume K and n are positive, each stream contains nondecreasing signed 64-bit values, one consumer incrementally obtains merged values, and each stream has at most one outstanding request. Each next returns one copied value or EOF.
A read reservation lasts conservatively from request admission through next completion. The consumer operation returns Value(value,streamId), Pending when the next globally ordered value is not yet known, or End after all streams reach EOF. Equal values choose the lower stream ID. Pending consumes no value.
The consumer never performs or waits for stream I/O; brief state synchronization is allowed and this is not a lock-free or wait-free requirement. Supplied calls succeed and eventually complete; I/O failure/retry, transport details and permanent stalls are outside the exercise.
One owner keeps the supplied client alive, stops the merger after use, and may wait during cleanup for outstanding reads to finish; early stop discards remaining output and admits no new reads. Unread record storage is bounded by the stream count rather than total stream length.
Do not collect complete streams into arrays. No coordinator state lock may remain held across client I/O.
Define responsibilities, interfaces and interactions, then implement the bounded admission/collection/ordered-poll core in C++ or precise C++-oriented executable pseudocode. OS threading setup and the supplied client implementation may be treated as provided.
Explain how the design works when n<K and show deterministic tests using real background reads and controllable completion. In a final follow-up, remove n and impose a hard unread-record budget B>=K: retain incremental sorted output while avoiding starvation of a stream whose next head is unknown.
Record count excludes fixed coordination metadata and the caller's consumed output.
The source reports C++/gcc stream-merge implementation with request, blocking next, unsafe client, an in-flight cap and an oral memory-bound follow-up; result types, ties, completion, cleanup and consumer-poll semantics are practice assumptions, not an original OOD-round claim.