Microsoft Interview Question

It was classic leetcode assignment, medium difficulty