Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Smallest Number in Infinite Set and Minimum Moves to Reach Target Score



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Smallest Number in Unlimited Set

Smallest Number in Unlimited Set

Imagine you have a collection that initially contains every positive integer starting from 1 and going upwards indefinitely, like [1, 2, 3, 4, 5, ...].

Your task is to create a class called MinInfiniteSet that supports the following operations:

  • MinInfiniteSet(): Initializes the object so that it contains all positive integers.
  • int getMin(): Removes and returns the smallest number currently present in the set.
  • void restore(int val): Adds a positive integer val back to the set, but only if it is not already present.

Examples

MinInfiniteSet minSet = new MinInfiniteSet();
minSet.restore(3);          // 3 is already in the set, so no changes made.
minSet.getMin();            // Returns 1 and removes it from the set.
minSet.getMin();            // Returns 2 and removes it.
minSet.getMin();            // Returns 3 and removes it.
minSet.restore(1);          // 1 is added back to the set.
minSet.getMin();            // Returns 1 since it was restored.
minSet.getMin();            // Returns 4 and removes it.
minSet.getMin();            // Returns 5 and removes it.
  

Constraints

  • 1 ≤ val ≤ 2000
  • At most 1200 calls will be made in total to getMin() and restore().