Citadel Interview Question

Solve problem in python please