Coding problem
A set with next-value lookups
The task
A booking system keeps the start times that are still free. For a request at time x it offers the earliest free start at or after x, and the free times change all day.
Complete the class CeilingSet, a set of distinct ints. Add(x) stores x and returns true, or returns false when x is already stored. Remove(x) removes x and returns true, or returns false when x is not stored. Ceiling(x) returns the smallest stored value that is at least x, or null when there is none. Each CeilingSet object keeps its own values.
Signature
public bool Add(int x) public bool Remove(int x) public int? Ceiling(int x)
Examples
Add(10), then Ceiling(10) and Ceiling(11)→10, null. 10 itself counts as "at least 10"; nothing is at least 11.Add(10), Add(0), then Ceiling(-5)→0. A stored 0 is a real answer.
Limits: Up to 400,000 stored values and 400,000 lookups; values can be any int.
The tests check: an empty set, a value equal to x, a stored 0, int.MinValue and int.MaxValue, adding a value twice, removing a missing value, adding again after removing, separate sets, and 400,000 values and lookups in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public sealed class CeilingSet
{
// Stores x; returns false when x is already stored.
public bool Add(int x)
{
throw new NotImplementedException();
}
// Removes x; returns false when x is not stored.
public bool Remove(int x)
{
throw new NotImplementedException();
}
// The smallest stored value that is at least x, or null.
public int? Ceiling(int x)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.