Módulo 14 · Data Structures & Problem-Solving — Lección 2 de 4 · ~11 min
Stacks, Queues & Two Pointers
A slice is already a stack and a queue
You don't import a stack in Go — a slice is one. A stack is LIFO (last in, first out), like an undo history: the last thing you did is the first you undo.
- Push:
stack = append(stack, x)— O(1) amortized.
- Peek:
stack[len(stack)-1]— the top.
- Pop:
stack = stack[:len(stack)-1]— reslice to drop the last element, O(1).
A queue is FIFO (first in, first out), like a job queue. Enqueue with append; dequeue from the front with queue = queue[1:]. (Reslicing the front is fine for interview-sized inputs; a production hot loop would use a ring buffer or container/list to avoid the underlying array growing forever.)