Hexaware On Campus OA 2023 | Smallest Infinite Set Design Problem Using Min Heap and Set
Hexaware · Question · Posted Jun 2026
Problem Statement You have a set that initially contains all positive integers: [1, 2, 3, 4, 5, ...] Implement the SmallestInfiniteSet class: Functions SmallestInfiniteSet() Initializes the object to contain all positive integers. int popSmallest() Removes and returns the smallest integer currently contained in the infinite set. void addBack(int num) Adds a positive integer num back into the infinite set if it is not already present. Example Input ["SmallestInfiniteSet","addBack","popSmallest","popSmallest", "popSmallest","addBack","popSmallest","popSmallest","popSmallest"] [[],[2],[],[],[],[1],[],[],[]] Output [null,null,1,2,3,null,1,4,5] Explanation SmallestInfiniteSet set = new SmallestInfiniteSet(); set.addBack(2); ...
The full answer & interview discussion are available to premium members.
Log in Create a free account