Google interview question

2 LC medium - hard question