Pregunta de entrevista de Endex (NY)

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