Moloco interview question

devise an algorithm and implement it simply