Tesco interview question

Coding round looking for complete working solution