swap deprecated Stack with ArrayDeque
use template method for algorithm