Problem
Design a set that holds positive integers. At the start, the set holds every positive integer: 1, 2, 3, and so on.
Write the class SmallestInfiniteSet with these operations:
SmallestInfiniteSet() creates the set with all positive integers in it. popSmallest() removes the smallest integer from the set and returns it. addBack(num) adds num to the set again. If num is already in the set, addBack does nothing.
Example 1
- Input:
- ["SmallestInfiniteSet","popSmallest","popSmallest","addBack","popSmallest","popSmallest","popSmallest"] [[],[],[],[1],[],[],[]]
- Output:
- [null,1,2,null,1,3,4]
- Explanation:
- The first two calls remove 1 and 2. Then
addBack(1)puts 1 back, so the next call returns 1 again.
Example 2
- Input:
- ["SmallestInfiniteSet","addBack","popSmallest","popSmallest","addBack","addBack","popSmallest"] [[],[3],[],[],[1],[1],[]]
- Output:
- [null,null,1,2,null,null,1]
- Explanation:
- The first
addBack(3)does nothing, because 3 is still in the set. The secondaddBack(1)also does nothing.
Constraints
- 1 <= num <= 1000
- At most 1000 calls to popSmallest and addBack in total
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 2336. Smallest Number in Infinite Set
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source