First Value Appearing Exactly Once in an Append-Only Queue
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Implement the native FirstUnique class. The constructor initializes the queue with nums. add appends one value. showFirstUnique returns the earliest queue value whose total occurrence count is exactly one,or -1 if none exists. Appending never removes an existing queue occurrence.
```cpp
class FirstUnique {
public:
FirstUnique(vector<int>& nums);
int showFirstUnique();
void add(int value);
};
```
### Examples
**Example 1:**
```text
Calls: FirstUnique([4,6,4,9]); showFirstUnique(); add(6); showFirstUnique(); add(9); showFirstUnique()
Returned query values: [6,9,-1]
Explanation: The initially unique values are6 then9; duplicating each removes its eligibility.
```
**Example 2:**
```text
Calls: FirstUnique([8,8]); showFirstUnique(); add(5); showFirstUnique(); add(8); showFirstUnique()
Returned query values: [-1,5,5]
Explanation: The already repeated8 never becomes unique again, while5 remains unique.
```
### Constraints
- nums has length 1 to 100000.
- Each nums value and added value lies in `[1,100000000]`.
- At most 50000 calls in total are made to showFirstUnique and add.
### Contract considerations
Use the constructor, `add`, and `showFirstUnique` methods shown above; successive calls share the queue's accumulated history.
```hint Read the boundary condition
Check successive queries as values are appended, including repeated initial values and a history with no unique value. Earlier occurrences remain in the history.
```
### Boundary reminders
Use the constructor, `add`, and `showFirstUnique` methods shown above; successive calls share the queue's accumulated history.
Overview: First Value Appearing Exactly Once in an Append-Only Queue. Implement the native FirstUnique class. The constructor initializes the queue with nums.
First Value Appearing Exactly Once in an Append-Only Queue
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Implement the native FirstUnique class. The constructor initializes the queue with nums. add appends one value. showFirstUnique returns the earliest queue value whose total occurrence count is exactly one,or -1 if none exists. Appending never removes an existing queue occurrence.
class FirstUnique {
public:
FirstUnique(vector<int>& nums);
int showFirstUnique();
void add(int value);
};
Examples
Example 1:
Calls: FirstUnique([4,6,4,9]); showFirstUnique(); add(6); showFirstUnique(); add(9); showFirstUnique()
Returned query values: [6,9,-1]
Explanation: The initially unique values are6 then9; duplicating each removes its eligibility.
Example 2:
Calls: FirstUnique([8,8]); showFirstUnique(); add(5); showFirstUnique(); add(8); showFirstUnique()
Returned query values: [-1,5,5]
Explanation: The already repeated8 never becomes unique again, while5 remains unique.
Constraints
nums has length 1 to 100000.
Each nums value and added value lies in
[1,100000000]
.
At most 50000 calls in total are made to showFirstUnique and add.
Contract considerations
Use the constructor, add, and showFirstUnique methods shown above; successive calls share the queue's accumulated history.
Boundary reminders
Use the constructor, add, and showFirstUnique methods shown above; successive calls share the queue's accumulated history.