Pergunta de entrevista da empresa ByteDance

What is the best data structure to solve this problem?