Endex (NY) interview question

Standard Hackrank Data structure Question. The real difficulty is the time aspect.