Problem 2 done using a queue