InstaVision interview question

Medium level DSA question from Leetcode