Design an Ordered Stream
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A stream delivers n (idKey, value) pairs in an arbitrary order, where the ids are exactly 1 through n, each appearing once. You must release the values **in id order** as soon as possible.
- OrderedStream(n) creates a stream expecting ids 1..n.
- insert(idKey, value) stores the pair and returns a list containing the **largest possible run** of values that can now be released: starting from the smallest id not yet released, take consecutive ids as long as their values have arrived. The list is ordered by id and may be empty.
Every value is released exactly once, and concatenating all returned lists gives the values sorted by id.
Examples
Constraints
- `1 <= n <= 1000`
- `1 <= idKey <= n`, and each `idKey` is inserted exactly once.
- `value.length == 5`, and `value` consists of lowercase English letters.
- Exactly `n` calls to `insert`.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.