Quick 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.

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

  1. 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.
  2. Decide what happens when a removal arrives while the candidate you are replaying holds no elements.
  3. 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.

Guess the Container From Its Operation Log

hardDuolingoSoftware Engineer

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

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

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.

Examples

Example 1
Input
([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]],)
Output
'queue'
Notes
Example 1: values leave in arrival order, so only the queue is consistent.
Example 2
Input
([[1, 1], [1, 2], [1, 3], [2, 3], [2, 2], [2, 1]],)
Output
'not sure'
Notes
Example 2: the stack and the priority queue both return 3, 2, 1, and the queue returns 1 first.

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".

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def guess_container(operations: list[list[int]]) -> str:
Sample call
guess_container([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]])
Language
Case 1
Input
([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]],)
Expected
'queue'
Case 2
Input
([[1, 1], [1, 2], [1, 3], [2, 3], [2, 2], [2, 1]],)
Expected
'not sure'

Browse More Questions

Show the approach

Approach

Replay the log once and keep all three candidates in step. Each candidate has a flag that starts true and its own structure: an array used as a stack, an array with a front index used as a queue, and a max-heap used as the priority queue. On an insert, push x into every candidate whose flag is still true. On a removal, check every candidate whose flag is still true: if its structure is empty, or the element it would remove differs from x, set its flag to false; otherwise remove that element. Invariant: after any prefix of the log, a candidate's flag is true exactly when every removal in that prefix matched, and then its structure holds exactly the elements that candidate would hold. Each candidate's removals are fully determined, since the stack and the queue have one possible element to remove and equal values in the priority queue are interchangeable, so the heap's maximum is the only value it can return. A flag that turns false never turns true again, so its structure can be ignored from then on, and the loop can stop early once all three flags are false. At the end, count the true flags: zero gives "impossible", two or three give "not sure", and exactly one gives that candidate's name. Edge cases: a removal from an empty candidate fails it (a lone [2, x] returns "impossible"), a log with no removals keeps all three flags true ("not sure"), and removing copies of a repeated maximum keeps the priority queue consistent.

Time complexity:
O(n log n)
Space complexity:
O(n)