No doubt, the method of teaching the problem is good but how to know which approach to follow and decide whether to use a stack for the problem or a queue. The way these question’s solutions are taught it just feels cramming the solution for this particular problem. Please help me understand this.
How to know whether to use a stack or queue?
Stack is used when you have to follow LIFO . Queue is used when you have to follow FIFO . And it varies from question to question whether to use stack or queue . I don’t think there is any fixed rule to it .You can learn these things only by practice .