Module 14 Β· Data Structures & Problem-Solving β Lesson 2 of 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.)