TD interview question

a simple algorithm coding problem