Design a set of all positive integers with pop-smallest and add-back operations

Quick Overview

Design a class that holds the set of all positive integers and supports removing the smallest value and adding a removed value back. It tests whether you can model an infinite set with finite state, keep duplicates out, and choose a heap or ordered set with the right cost per operation.

Design a set of all positive integers with pop-smallest and add-back operations

Company: Amazon

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

You have a set that contains all positive integers `[1, 2, 3, 4, 5, ...]`. Implement the `SmallestInfiniteSet` class, which supports removing and returning the smallest integer in the set, and adding a positive integer back into the set if it is not already present. Implement the following methods: - `SmallestInfiniteSet()` initializes the object so it contains all positive integers. - `popSmallest() -> int` removes and returns the smallest integer contained in the infinite set. - `addBack(num: int)` adds the positive integer `num` back into the infinite set, if it is not already in the set. Example 1: ```text Input: ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"] [[], [2], [], [], [], [1], [], [], []] Output: [null, null, 1, 2, 3, null, 1, 4, 5] Explanation: set = SmallestInfiniteSet() # contains all positive integers [1, 2, 3, ...] set.addBack(2) # 2 is already in the set, no change set.popSmallest() # -> 1 (removes 1 from the set) set.popSmallest() # -> 2 (removes 2 from the set) set.popSmallest() # -> 3 (removes 3 from the set) set.addBack(1) # adds 1 back into the set set.popSmallest() # -> 1 (1 was added back and is the smallest) set.popSmallest() # -> 4 (next smallest is 4) set.popSmallest() # -> 5 (next smallest is 5) ``` Example 2, where the same number is added back twice: ```text Input: ["SmallestInfiniteSet", "popSmallest", "popSmallest", "addBack", "addBack", "popSmallest", "popSmallest"] [[], [], [], [1], [1], [], []] Output: [null, 1, 2, null, null, 1, 3] Explanation: set = SmallestInfiniteSet() set.popSmallest() # -> 1 set.popSmallest() # -> 2 set.addBack(1) # 1 was removed, so it goes back in set.addBack(1) # 1 is already back, no change set.popSmallest() # -> 1 set.popSmallest() # -> 3 (2 was removed and never added back) ``` ```hint Where nothing has changed Past some point, no integer has ever been removed. Think about how little you need to store to describe everything above that point. ``` ```hint Watch for duplicates Check what your design does when `addBack` gets a number that is already in the set, either because it was never removed or because it was added back earlier. ``` ### Constraints & Assumptions - `1 <= num <= 1000` - At most `1000` calls in total are made to `popSmallest` and `addBack`. ### What a Strong Answer Covers - A finite representation of the infinite set, with an argument that it matches the real set after any sequence of calls - How `popSmallest` chooses between added-back numbers and the range of numbers that were never removed - How `addBack` ignores a number that is already present, in both ways it can be present - Time and space cost of each operation in terms of how many numbers are stored - A trace through the example calls or the edge cases (adding back a number twice, adding back a number that was never removed) ### Follow-up Questions - How would you add a `remove(num)` method that works for any positive integer, including ones that were never popped? - What changes if several threads call `popSmallest` and `addBack` at the same time? - If `addBack` took a range, `addBackRange(lo, hi)`, that could cover millions of numbers, how would you store the added-back values?

Overview: Design a class that holds the set of all positive integers and supports removing the smallest value and adding a removed value back. It tests whether you can model an infinite set with finite state, keep duplicates out, and choose a heap or ordered set with the right cost per operation.

Amazon logo
Amazon
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

You have a set that contains all positive integers [1, 2, 3, 4, 5, ...].

Implement the SmallestInfiniteSet class, which supports removing and returning the smallest integer in the set, and adding a positive integer back into the set if it is not already present.

Implement the following methods:

  • SmallestInfiniteSet() initializes the object so it contains all positive integers.
  • popSmallest() -> int removes and returns the smallest integer contained in the infinite set.
  • addBack(num: int) adds the positive integer num back into the infinite set, if it is not already in the set.

Example 1:

Input:
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
[[],                    [2],       [],            [],            [],            [1],       [],            [],            []]

Output: [null, null, 1, 2, 3, null, 1, 4, 5]

Explanation:
set = SmallestInfiniteSet()   # contains all positive integers [1, 2, 3, ...]
set.addBack(2)                # 2 is already in the set, no change
set.popSmallest()             # -> 1 (removes 1 from the set)
set.popSmallest()             # -> 2 (removes 2 from the set)
set.popSmallest()             # -> 3 (removes 3 from the set)
set.addBack(1)                # adds 1 back into the set
set.popSmallest()             # -> 1 (1 was added back and is the smallest)
set.popSmallest()             # -> 4 (next smallest is 4)
set.popSmallest()             # -> 5 (next smallest is 5)

Example 2, where the same number is added back twice:

Input:
["SmallestInfiniteSet", "popSmallest", "popSmallest", "addBack", "addBack", "popSmallest", "popSmallest"]
[[],                    [],            [],            [1],       [1],       [],            []]

Output: [null, 1, 2, null, null, 1, 3]

Explanation:
set = SmallestInfiniteSet()
set.popSmallest()             # -> 1
set.popSmallest()             # -> 2
set.addBack(1)                # 1 was removed, so it goes back in
set.addBack(1)                # 1 is already back, no change
set.popSmallest()             # -> 1
set.popSmallest()             # -> 3 (2 was removed and never added back)

Constraints & Assumptions

  • 1 <= num <= 1000
  • At most 1000 calls in total are made to popSmallest and addBack .

What a Strong Answer Covers Guidance

  • A finite representation of the infinite set, with an argument that it matches the real set after any sequence of calls
  • How popSmallest chooses between added-back numbers and the range of numbers that were never removed
  • How addBack ignores a number that is already present, in both ways it can be present
  • Time and space cost of each operation in terms of how many numbers are stored
  • A trace through the example calls or the edge cases (adding back a number twice, adding back a number that was never removed)

Follow-up Questions Guidance

  • How would you add a remove(num) method that works for any positive integer, including ones that were never popped?
  • What changes if several threads call popSmallest and addBack at the same time?
  • If addBack took a range, addBackRange(lo, hi) , that could cover millions of numbers, how would you store the added-back values?