Solving problems using stacks