Lite / Intelligent Robotic Systems / Week 4
Stacks and queues: who goes next?
A robot cell gets jobs faster than it can do them. Do you serve the newest job first, or the oldest? The answer changes everything.
Why should I care?
Many systems are really about order: undo histories, job queues, sensor event buffers, and the search frontier of a path planner. A stack serves the newest item first (LIFO); a queue serves the oldest (FIFO). Picking the wrong one is a fairness bug, or a planner that wanders.
- Use a list as a stack (append/pop) and collections.deque as a queue (append/popleft).
- Trace the contents of a stack or queue through a sequence of operations.
- Choose last-in-first-out or first-in-first-out for an engineering task and justify it.
Predict
A queue receives jobs 101, 102, 103, then serves one job, then receives 104. Which job is served next?
- 101
- 102
- 103
- 104
Guess first, then reveal
102. A queue is first in, first out: 101 was served first, so 102 is now the oldest waiting job. A stack would have served 103 first and 104 next.
Discover
In Stack mode, append three values and pop twice: which came out? Do the same sequence in Queue mode. Then pop from an empty structure and read the error. Which one would you use for an undo button, and which for print jobs?
What the interactive shows (text version)
The stack and queue modes show items in a row, oldest on the left. In a stack, append adds to the right and pop removes from the right (newest). In a queue, append adds to the right and popleft removes from the left (oldest). Popping from an empty structure shows IndexError.
Watch (optional)
Watch for: Watch for the two operations each structure allows and which end of the line they use.
Contrasts last-in-first-out with first-in-first-out using clear diagrams, the two orders behind undo and job queues.
“Data Structures: Stacks and Queues” by HackerRank (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Stack: last in, first out
A Python list is a stack: s.append(x) pushes onto the top, s.pop() removes and returns the top. Both are fast (O(1)). Use it for undo, for backtracking, and for depth-first search.
Queue: first in, first out
from collections import deque gives a double-ended queue. q.append(x) joins the back; q.popleft() serves the front, both in O(1). Don't use list.pop(0) for a queue: it shifts every remaining item, which is O(n).
Empty means stop
Popping from an empty list or deque raises IndexError. Loops usually run while q: (an empty container is falsy), so they end naturally when the work is done.
Explain it another way
Intuitive
A stack is a pile of plates: you take the top one. A queue is a line at a bus stop: the person who arrived first boards first.
Technical
deque is implemented as a linked list of blocks, so both ends are O(1). A list is a dynamic array: the end is O(1) amortised, but inserting or removing at the front moves every element.
Applied
Robot task schedulers use FIFO queues for fairness; motion planners use stacks to backtrack out of dead ends; breadth-first search (next lesson) is simply 'keep the frontier in a queue'.
Worked example 1
Process jobs in arrival order and keep an undo history.
from collections import deque
jobs = deque()
for job in ["drill A", "drill B", "inspect A"]:
jobs.append(job)
print(jobs.popleft()) # drill A (oldest first)
jobs.append("drill C")
print(list(jobs)) # ['drill B', 'inspect A', 'drill C']
undo = []
undo.append("move wp3")
undo.append("delete wp5")
print(undo.pop()) # delete wp5 (newest first)
while jobs: # stops when the queue is empty
print("doing", jobs.popleft())Common mix-ups
A list works fine as a queue.
✓ It works, but list.pop(0) is O(n). deque gives O(1) at both ends.Stacks and queues are different kinds of storage.
✓ They are the same items with different rules about which one leaves next.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Simulate a robot cell: jobs arrive at known times and each takes a known number of minutes. Use a deque to serve them in order and record each job's waiting time. Then try a stack instead and compare the longest wait. Which is fairer, and when might the stack be acceptable?
Reflect
Name one system you use that is a queue and one that is a stack.
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- Python docs: collections.deque
All deque operations and their costs, including maxlen for fixed-size buffers.