Glassdoor Interview Question

Leet code medium but more time consuming.