Glassdoor interview question

Leet code medium but more time consuming.