Motadata Interview Question

give me optimal solution for perticuler code