Whitepages interview question

Typical DP problem