Caterpillar interview question

Data Structure and algorithm