Advanced Topics in Computer Science

Unit 4: Stacks and Queues

Students study abstract data types that control access order: last-in-first-out stacks and first-in-first-out queues.

Unit Focus

What this unit is really teaching

The unit connects ADTs to practical applications like undo history, expression evaluation, simulations, and breadth-first search.

Key Topics

  • +Stack ADT operations: push, pop, peek, and empty checks
  • +Queue ADT operations: enqueue, dequeue, front, and circular buffers
  • +Array-backed and linked implementations
  • +Expression matching, postfix evaluation, and undo/redo workflows
  • +Queue-based simulations and scheduling
  • +Using queues as preparation for breadth-first graph traversal

Practice Work

Implementation and analysis tasks

Student task

Build a balanced-symbol checker and postfix expression evaluator.

Student task

Model a service line simulation using queues and runtime statistics.

DSA Assignment Bank

30 interview-style algorithm questions for this unit

Code Scholars provides structured assignments, implementation support, edge-case review, and runtime analysis coaching so students learn the concept rather than only memorizing a solution.

1

Stack matching

Validate nested symbols

Use a stack to validate parentheses, brackets, and braces while rejecting mismatched or unfinished groups.

2

Stack evaluation

Evaluate postfix expressions

Process a postfix expression with numbers and operators, pushing operands and applying operations in the correct order.

3

Operator precedence stack

Convert infix to postfix

Use a stack to handle precedence and grouping while converting an infix expression to postfix form.

4

Auxiliary stack

Build a min stack

Support push, pop, top, and getMin in constant time by tracking minimum values alongside the main stack.

5

ADT conversion

Implement a queue using two stacks

Use two stacks so enqueue and dequeue behave like a FIFO queue and analyze amortized cost.

6

ADT conversion

Implement a stack using queues

Use one or two queues so push and pop behave like a LIFO stack.

7

Two stacks

Design browser history navigation

Model back and forward navigation with two stacks and explain what happens after visiting a new page.

8

Stack cleanup

Simplify a file path

Process path tokens, handling current directory, parent directory, and repeated separators safely.

9

Monotonic stack

Find next greater values

For each value in an array, find the next value to its right that is greater.

10

Monotonic stack

Compute daily wait times

Given daily measurements, compute how many days until a larger measurement appears.

11

Monotonic stack

Calculate largest rectangle in bars

Use a stack of increasing bar indices to compute the largest rectangle area in a histogram.

12

Deque

Sliding window maximum

Maintain candidate indices in a deque to report the maximum value in each fixed-size window.

13

Queue plus counts

First non-repeating stream item

Process a stream of characters and report the first character that has appeared exactly once after each update.

14

Queue simulation

Round-robin task scheduler

Simulate tasks that receive fixed time slices until complete and report completion order.

15

Queue simulation

Customer service line simulation

Track arrivals, service durations, wait times, and maximum queue length in a service-line model.

16

Queue traversal

Level-order tree traversal

Use a queue to visit tree nodes level by level and group values by depth.

17

BFS queue

Shortest path in an unweighted grid

Use a queue to find the minimum number of moves from a start cell to a target cell.

18

Two stacks

Undo and redo editor actions

Model text editor undo and redo behavior while clearing redo history after a new edit.

19

Stack parsing

Decode repeated strings

Decode a compact repeated-string notation using stacks for counts and partial strings.

20

Simulation

Check stack pop sequence

Given a push order and proposed pop order, simulate stack operations to determine whether the pop order is possible.

21

Array-backed queue

Design a bounded circular queue

Implement enqueue and dequeue with front, size, and wraparound arithmetic.

22

Double-ended queue

Design a bounded circular deque

Support insert and delete at both ends with wraparound and capacity checks.

23

Stack compression

Remove adjacent duplicate groups

Process a string and remove groups of k adjacent equal characters until no such group remains.

24

Monotonic stack

Find stock span values

For each price, compute how many consecutive previous prices are less than or equal to it.

25

Queue discipline

Implement a task priority buffer

Compare FIFO, stack, and priority behavior for tasks with arrival times and urgency values.

26

Queue and stack

Reverse the first k queue items

Reverse only the first k items of a queue while preserving the order of the remaining items.

27

Queue rearrangement

Interleave queue halves

Given an even-length queue, interleave the first half and second half while preserving each half order.

28

Stack reasoning

Detect impossible call-stack trace

Given function entry and exit events, determine whether the trace could come from a valid call stack.

29

ADT selection

Choose stack or queue

Given several scenarios, choose stack, queue, or deque and explain how access order drives the choice.

30

DSA support

Plan a stacks-and-queues assignment

Create an assignment with implementation, simulation, edge cases, and a written comparison of stack and queue behavior.

Need support with Unit 4?

Work through the concepts, code, edge cases, and runtime analysis with 1:1 guidance.