Tesla interview question

medium to hard LC question