Repaso
Español
Algorithms & DSCoreALG-42

A stack is…

Options

  1. First in, first out (FIFO): the oldest leaves first
  2. Last in, first out (LIFO): the newest leaves first (Correct option)
  3. Kept sorted automatically as items are added
  4. 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).

Key terms

Practice this question in Repaso