X interview question

Coding challenge on HackerRank