A stack is…
Options
- First in, first out (FIFO): the oldest leaves first
- Last in, first out (LIFO): the newest leaves first (Correct option)
- Kept sorted automatically as items are added
- A kind of database used to hold temporary data
Answer
Last in, first out (LIFO): you take from the top what you put last
Why
Think of a pile of plates: you put a plate on top (push) and take the top one (pop). The last plate added is the first removed.
In Python a list works as a stack: append() to push, pop() to pop, both O(1).
Uses: undo history, matching brackets, the function call stack, DFS.
Remember
Stack = LIFO (pile of plates).