Pregunta de entrevista de Tencent

SQL question and one basic algorithm that needs to be fully written in python.