Microsoft interview question

It was classic leetcode assignment, medium difficulty