Show Streamed Responses in Request Order
Updated
Problem statement
A web page sends requests 0, 1, ..., n - 1 to a server, in that order. Each answer streams back in small pieces called chunks, and pieces of different answers arrive mixed together. The page must show the answers in request order: all of answer 0, then all of answer 1, and so on.
Answer i has chunkCounts[i] chunks, so n = chunkCounts.length. You get the arrivals in the order they happen: arrival j is the chunk chunks[j] of request ids[j], at time times[j]. The chunks of one request arrive in their own order. A request is complete once all of its chunks have arrived.
The page shows a chunk as soon as both of these are true: the chunk has arrived, and every earlier request is complete. So the earliest request that is not complete shows its chunks the moment they arrive, and chunks of later requests wait. When a request becomes complete, the waiting chunks of the next request appear at once, and if that request is complete too, the same happens for the one after it.
Return what appears on the page as a list of strings "time:request:text". Make one entry for each pair of a time and a request at which some text of that request appears. text joins the chunks of that request that appear at that time, in order. List the entries by time, and entries with the same time by request number.
Function
showInRequestOrder(chunkCounts: int[], times: int[], ids: int[], chunks: String[]) → String[]Examples
Example 1
chunkCounts = [2,1,2]times = [1,2,4,5,6]ids = [1,0,2,0,2]chunks = ["ok","he","wo","y","rld"]return = ["2:0:he","5:0:y","5:1:ok","5:2:wo","6:2:rld"]At time 1, request 1's answer arrives, but request 0 is not complete, so request 1 must wait and the screen stays empty. At time 2, request 0's first chunk he appears right away.
Request 2's first chunk arrives at time 4 and must wait. At time 5, request 0 gets its last chunk y and is complete, so request 1 (ok, already complete) and then request 2's waiting chunk wo appear at the same time. Request 2's last chunk appears as soon as it arrives at time 6.
Example 2
chunkCounts = [3,1]times = [3,3,3,8]ids = [0,0,1,0]chunks = ["a","bc","zz","d"]return = ["3:0:abc","8:0:d","8:1:zz"]At time 3, two chunks of request 0 arrive and appear, so they form one entry, "3:0:abc". Request 1's chunk also arrives at time 3, but request 0 is not complete yet, so it waits.
At time 8, request 0's third chunk d completes it, and request 1's waiting chunk appears at the same time.
Example 3
chunkCounts = [1,1,1]times = [2,4,9]ids = [2,1,0]chunks = ["c","b","a"]return = ["9:0:a","9:1:b","9:2:c"]The answers arrive in reverse order. Nothing can be shown until request 0 is complete at time 9. Then all three answers appear at time 9, one entry per request, in request order.
Constraints
1 <= chunkCounts.length <= 10001 <= chunkCounts[i] <= 10times,idsandchunkshave the same length, equal to the sum of allchunkCounts[i], which is at most2000.0 <= times[j] <= 10^9, andtimesnever decreases.0 <= ids[j] < chunkCounts.length, and each requestiappears inidsexactlychunkCounts[i]times.- Each
chunks[j]has1to3lowercase English letters.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- High-Card Pot GameAffirm · PHONE SCREENAffirm interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
- Simulate a Queued Multi-Rule Rate LimiterSnowflake · PHONE SCREENSnowflake interview questions
- Bus Station Seat AllotmentInMobi · PHONE SCREENInMobi interview questions
- FIFO FizzBuzz Market MatchingGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions
- Chunked Round-Robin Row TraversalMotive · PHONE SCREENMotive interview questions