Google interview question

4-5 leet code medium - hard