Teya interview question

Leetcode style coding problem of medium difficulty.