ByteDance interview question

What is the best data structure to solve this problem?