The part 1 warmup (sorted arrivals) felt manageable since you just check the last three items each time.
Maintain a sorted data structure of the items seen so far, and after each insertion check the window of three consecutive items around the new item to see if any triple satisfies the condition. Since any valid triple must be three consecutive items in sorted order, this check is sufficient and can be done in O(log n) time per insertion using a balanced BST or a skip list.
Pro tip: Emphasize that the condition is equivalent to finding three items within a range of T, so sorting is key; also note that if the stream is bounded or known, a bucket-based approach could give O(1) amortized time, but a balanced BST is more general and robust.
Restate the problem: we need to detect any triple (a,b,c) from the arrival history such that max(a,b,c) - min(a,b,c) < T. Recognize that this is equivalent to finding three items that lie within an interval of length T.
Prove that if such a triple exists, then in the sorted order of all items seen so far, there must be three consecutive items that satisfy the condition. This reduces the problem to checking only local windows around each newly inserted item.
Select a balanced binary search tree (e.g., Red-Black Tree) or a skip list to maintain the sorted order of items, supporting insertion and neighbor queries in O(log n) time. Alternatively, if the value range is small, consider a bucket-based approach for O(1) amortized operations.
Upon each new item, insert it into the data structure, then retrieve its immediate predecessor and successor (and possibly the next successor) to form candidate triples. Check if any of these triples satisfy the condition; if so, return it immediately.
State that each insertion and neighbor lookup takes O(log n) time, so the overall time per item is O(log n). Discuss space O(n) for storing all items. Mention that early termination is possible as soon as a valid triple is found.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.