Tavant interview question

Train chase question for the coding round.