Your question is Merge Overlapping Chat Messages. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Reddit may receive multiple timestamped message fragments for the same chat stream. Given a list of message objects, merge fragments belonging to the same stream whenever their time intervals overlap or touch.
Implement merge_chat_streams(messages), where messages is a list of dictionaries with the following fields:
stream_id: a string identifying the Reddit chat streamstart: an integer start timestampend: an integer end timestamp, with start <= endtext: the message fragment as a stringReturn a list of dictionaries. Each result must contain stream_id, the earliest start, the latest end, and messages, an array of fragment texts ordered by increasing start. Return result streams ordered lexicographically by stream_id, then by start time. A fragment belongs to the same unified stream only when its stream_id matches and its start is less than or equal to the current merged interval's end. Do not mutate the input list.
stream_id and text are non-empty stringsstream_id, start, end, and textdef merge_chat_streams(messages):