Identify Stack, Queue, or Max Priority Queue from an Operation Log
Company: Duolingo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
An unknown container holds integers. You are given the log of every operation performed on it, in order. Each log entry is one of:
- `[1, x]`: the value `x` was inserted.
- `[2, x]`: one element was removed, and the removed element was `x`.
The container is one of three candidates:
- **Stack:** a removal returns the most recently inserted element still present.
- **Queue:** a removal returns the earliest inserted element still present.
- **Priority queue:** a removal returns the largest element still present.
Replay the log against each candidate and report which of them could have produced it.
### Function Signature
```python
def guess_container(operations: list[list[int]]) -> str:
...
```
### Rules
- A candidate is consistent with the log only if, replaying the operations in order, every removal returns exactly the logged value.
- A removal from a candidate that holds no elements makes that candidate inconsistent.
- Values may repeat. In the priority queue, equal values are interchangeable, so removing any copy of the current maximum returns that value.
- A log with no removals is consistent with all three candidates.
- Return `"stack"`, `"queue"` or `"priority queue"` when exactly that one candidate is consistent, `"not sure"` when two or three candidates are consistent, and `"impossible"` when none is.
### Constraints
- `1 <= len(operations) <= 2 * 10^5`
- Each entry holds exactly two integers, and the first one is `1` or `2`.
- `0 <= x <= 10^9` for every value in the log.
- The answer is always exactly one of the five lowercase strings listed in the rules.
### Examples
#### Example 1
```text
Input: operations = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]]
Output: "queue"
```
Values leave in the order they arrived. A stack or a priority queue would return `3` on the first removal.
#### Example 2
```text
Input: operations = [[1, 1], [1, 2], [1, 3], [2, 3], [2, 2], [2, 1]]
Output: "not sure"
```
A stack and a priority queue both return `3`, `2`, `1`. A queue would return `1` first.
#### Example 3
```text
Input: operations = [[1, 5], [2, 5], [2, 5]]
Output: "impossible"
```
Every candidate is empty after the first removal, so the second removal fails for all three.
Overview: Given a log of insertions and removals with the value each removal returned, decide whether the container behaved like a stack, a queue or a max priority queue, or whether the log is ambiguous or impossible. It tests faithful simulation of standard containers, repeated values and removals from an empty container.
Read the full Duolingo Software Engineer interview experience this question came from
An unknown container holds integers. You are given `operations`, the log of every operation performed on it, in order. Each log entry is a pair `[op, x]`:
- `[1, x]`: the value `x` was inserted.
- `[2, x]`: one element was removed, and the removed element was `x`.
The container is one of three candidates:
- **Stack:** a removal returns the most recently inserted element still present.
- **Queue:** a removal returns the earliest inserted element still present.
- **Priority queue:** a removal returns the largest element still present.
Replay the log against each candidate and report which of them could have produced it.
### Rules
- A candidate is consistent with the log only if, replaying the operations in order, every removal returns exactly the logged value.
- A removal from a candidate that holds no elements makes that candidate inconsistent.
- Values may repeat. In the priority queue, equal values are interchangeable, so removing any copy of the current maximum returns that value.
- A log with no removals is consistent with all three candidates.
- Return `"stack"`, `"queue"` or `"priority queue"` when exactly that one candidate is consistent, `"not sure"` when two or three candidates are consistent, and `"impossible"` when none is.
### Input and output
Implement `guess_container(operations)`. The log arrives as a list of two-element integer lists: `int[][]` in Java and `const std::vector<std::vector<int>>&` in C++. Every value fits in a signed 32-bit integer. Return one of the five strings above exactly as written, in lowercase.
### Constraints
- `1 <= len(operations) <= 2 * 10^5`
- Each entry holds exactly two integers, and the first one is `1` or `2`.
- `0 <= x <= 10^9` for every value in the log.
### Examples
#### Example 1
```text
Input: operations = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]]
Output: "queue"
```
Values leave in the order they arrived. A stack or a priority queue would return `3` on the first removal.
#### Example 2
```text
Input: operations = [[1, 1], [1, 2], [1, 3], [2, 3], [2, 2], [2, 1]]
Output: "not sure"
```
A stack and a priority queue both return `3`, `2`, `1`. A queue would return `1` first, so two candidates are consistent.
Constraints
- 1 <= len(operations) <= 2 * 10^5
- Each entry holds exactly two integers [op, x], and op is 1 or 2.
- 0 <= x <= 10^9 for every value in the log, so every value fits in a signed 32-bit integer.
- The answer is always exactly one of the five lowercase strings "stack", "queue", "priority queue", "not sure" and "impossible".
Examples
Input: ([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]],)
Expected Output: 'queue'
Explanation: Example 1: values leave in arrival order, so only the queue is consistent.
Input: ([[1, 1], [1, 2], [1, 3], [2, 3], [2, 2], [2, 1]],)
Expected Output: 'not sure'
Explanation: Example 2: the stack and the priority queue both return 3, 2, 1, and the queue returns 1 first.
Hints
- The element each candidate removes is fully determined by what it holds, so the log can be checked against each candidate on its own, one operation at a time.
- Decide what happens when a removal arrives while the candidate you are replaying holds no elements.
- Two consistent candidates and three consistent candidates lead to the same answer, so track which candidates survive the whole log before you choose the string.