Zoho (India) interview question

Coding flow and optimal solution