Design a class that infers whether a container is a stack, queue or priority queue
Company: Duolingo
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `DataStreamGuesser` class that observes a sequence of `add` and `remove` operations on an unknown underlying container and, at any point, reports which container best matches the observed behavior.
There are three candidate container types: a Stack (removes the most recently added value, LIFO), a Queue (removes the earliest added value, FIFO), and a PriorityQueue (always removes the current minimum value). Initially, all three candidates are considered possible. Each time `remove` is observed with a specific removed value, candidates whose expected removal does not match that value are eliminated.
At any moment, `guess` returns the single remaining candidate. If multiple candidates are still consistent with the observed history, return the one with the highest priority in this order: `"Stack"`, `"Queue"`, `"PriorityQueue"`. If no candidate is consistent, return `"Unknown"`. This can happen: Example 1 ends with a removal that none of the three containers would make.
Implement the following methods:
- `add(value: int) -> None`: Record that `value` was added to the underlying container.
- `remove(value: int) -> None`: Record that `value` was removed from the underlying container. Eliminates any candidate type whose next removal would not equal `value`.
- `guess() -> str`: Return the best-fit candidate consistent with the observed history. Ties go to `"Stack"` first, then `"Queue"`, then `"PriorityQueue"`. Returns `"Unknown"` if no candidate remains.
Example 1:
```text
Input:
["DataStreamGuesser", "add", "add", "add", "remove", "guess", "remove", "guess"]
[[], [3], [1], [2], [2], [], [3], []]
Output: [null, null, null, null, null, "Stack", null, "Unknown"]
Explanation:
ds = DataStreamGuesser() # all candidates valid: Stack, Queue, PriorityQueue
ds.add(3) # stack=[3], queue=[3], heap=[3]
ds.add(1) # stack=[3,1], queue=[3,1], heap=[1,3]
ds.add(2) # stack=[3,1,2], queue=[3,1,2], heap=[1,3,2]
ds.remove(2) # Stack pops 2 ✓; Queue front=3 ✗ invalid; PQ min=1 ✗ invalid
ds.guess() # → "Stack" (only Stack remains valid)
ds.remove(3) # Stack top is 1, not 3 ✗ invalid
ds.guess() # → "Unknown" (no candidate left)
```
Example 2:
```text
Input:
["DataStreamGuesser", "add", "add", "add", "remove", "guess", "remove", "remove", "guess"]
[[], [5], [2], [8], [2], [], [5], [8], []]
Output: [null, null, null, null, null, "PriorityQueue", null, null, "PriorityQueue"]
Explanation:
ds = DataStreamGuesser() # all candidates valid
ds.add(5) # stack=[5], queue=[5], heap=[5]
ds.add(2) # stack=[5,2], queue=[5,2], heap=[2,5]
ds.add(8) # stack=[5,2,8], queue=[5,2,8], heap=[2,5,8]
ds.remove(2) # Stack top=8 ✗; Queue front=5 ✗; PQ min=2 ✓
ds.guess() # → "PriorityQueue" (only PQ remains)
ds.remove(5) # PQ min=5 ✓
ds.remove(8) # PQ min=8 ✓
ds.guess() # → "PriorityQueue"
```
```hint Next removal
For each candidate, ask which value it would remove next given everything added and removed so far. What state lets you answer that in one step?
```
```hint Elimination is permanent
Once a removal contradicts a candidate, nothing later can make it consistent again. Think about what work you still need to do for that candidate, and what a removal means when its container would be empty.
```
### Constraints & Assumptions
- `1 <= number of operations <= 10⁴`
- `-10⁹ <= value <= 10⁹`
- `remove` is only called when at least one value has been added.
- The returned guess is one of `"Stack"`, `"Queue"`, `"PriorityQueue"` or `"Unknown"`.
### Clarifying Questions to Ask
- Can the same value be added more than once, and does it matter which copy of a repeated minimum the priority queue removes?
- Once a candidate is eliminated, can a later operation bring it back?
- What should happen when `remove` is observed while a candidate's own container would be empty?
- What should `guess` return before any `remove` has been observed?
### What a Strong Answer Covers
- What state is kept for each candidate so that its next removal is known at every step.
- How a mismatch, including a removal from an empty simulated container, eliminates a candidate for good.
- The tie-break order in `guess` and when `"Unknown"` is returned.
- Time and space cost of each operation, including the priority queue.
- Behavior with repeated values, negative values and the edge cases allowed by the constraints.
### Follow-up Questions
- How would you add a max-priority queue as a fourth candidate?
- Suppose `guess` must say when more than one candidate is still consistent. What would it return, and what changes in the class?
- The stream interleaves operations from many containers, each tagged with an id. How would you extend the design?
- Over a very long stream, how much memory does the class need once only one candidate is left, and can it be reduced?
Overview: Design a class that watches add and remove operations on an unknown container and reports whether it behaves like a stack, a queue or a min-priority queue. It tests choosing what state to track for each candidate, permanent elimination, tie-breaking, edge cases and the cost of each operation.