Microsoft interview question

Had a task on strings and trees.