TD Interview Question

a simple algorithm coding problem