Google interview question

2 medium leetcode questions, not hard