Juniper Square interview question

one LeetCode medium problem, solved live through CodeSignal