Google Interview Question

two leetcode mediums, not that hard