Microsoft interview question

medium leetcode problems, logical question