Smallest Number in Infinite Set
Heap + set (design)
Asked at 5 companies
- Amazon
- Meta
- Microsoft
- Bloomberg
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Merge sort: split, sort, merge · Partitioning around a pivot
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Task schedulers and leaderboards: A heap always knows the most urgent job or the top score, so schedulers and “top 10” lists never re-sort everything.
About this problem
Design a structure where most of the set is implicit and only the exceptions are stored.
Read the full statement and examples on LeetCode, try it for 25–35 min, then come back for the walkthrough and the step-by-step visual.
Examples
Full statement on LeetCode ↗Example 1
- Input
- addBack(2), popSmallest() ×3, addBack(1), popSmallest() ×3
- Output
- [1, 2, 3, 1, 4, 5]
Walk through it step by step(9 steps · shows the approach, so try first)
- Everything from NEXT upward is still in the set without storing it. Numbers added back below NEXT go in a . Pop from the heap first, otherwise hand out NEXT.
- addBack(2): 2 ≥ next, so it's already in the set.
- popSmallest() → 1.
- popSmallest() → 2.
- popSmallest() → 3.
- addBack(1): it was taken out earlier, so it goes into the heap.
- popSmallest() → 1.
- popSmallest() → 4.
- popSmallest() → 5.
Code it on LeetCode
This one needs setup the editor here can't do yet (a class you build, linked nodes, or a helper LeetCode provides), so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionEverything from some counter upward is still in the set. Numbers added back below the counter go in a (with a set to avoid duplicates); pop from the heap first, otherwise hand out the counter.
class SmallestInfiniteSet:
def __init__(self):
self.next = 1
self.heap = []
self.added = set()
def popSmallest(self) -> int:
if self.heap:
x = heappop(self.heap)
self.added.discard(x)
return x
self.next += 1
return self.next - 1
def addBack(self, num: int) -> None:
if num < self.next and num not in self.added:
self.added.add(num)
heappush(self.heap, num)Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.