Skip to main content
← Data Structures & Algorithms Study Guide

Unit 3 · Moderate

Stacks, Queues, and Linked Structures

Structures defined by a restriction. The restriction is what makes each one fit a specific class of problem, so recognising the fit is the skill rather than the implementation.

What a strong answer looks like

A strong Unit 3 answer names the access discipline and explains why the problem needs exactly that discipline.

Topics in this unit

1

Stacks

Know

Last in, first out. The discipline matches nesting, backtracking, undo, and expression evaluation because the most recent unfinished thing is always the one to resolve.

Apply

Reach for a stack when the problem involves matching pairs or reversing recent work.

Watch out

Indexing into a stack, which discards the guarantee that made it appropriate.

Study move

Use a stack to validate nested brackets and state both failure conditions.

2

Queues and Deques

Know

First in, first out, which matches fairness and level-by-level exploration. A deque relaxes the restriction at both ends.

Apply

Use a queue for breadth-first work and a deque when a sliding-window maximum is needed.

Watch out

Choosing a stack where a queue was needed, which silently changes a breadth-first traversal into a depth-first one.

Study move

Describe how a graph traversal changes when a stack replaces the queue.

3

Linked Lists

Know

No indexing: position k costs k steps. Insertion and deletion are constant once positioned, which is the whole trade.

Apply

Draw the nodes and order the reassignments so no needed reference is dropped.

Watch out

Detaching before attaching, which loses the remainder of the list.

Study move

Reverse a linked list iteratively and name the three references you must hold.

4

Pointer Techniques

Know

Fast and slow pointers detect cycles and find midpoints in one pass without extra memory.

Apply

Advance one pointer twice as fast as the other, and reason about when they meet.

Watch out

Failing to check the fast pointer and its successor before advancing, which dereferences null.

Study move

Detect a cycle with two pointers and explain why they must meet if one exists.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Choosing the discipline before the implementation
  • Ordering pointer reassignments safely
  • Using two pointers to avoid extra memory

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Circular buffers. Covered in systems-leaning tracks.
  • Skip lists. An occasional extension.

Mastery checklist

  • Choose stack or queue from a problem description.
  • Reverse a linked list without losing the tail.
  • Detect a cycle with fast and slow pointers.
  • State the cost of reaching position k.

Check yourself

  • Why does indexing into a stack defeat its purpose?
  • What changes in a traversal when a stack replaces a queue?
  • Why must fast and slow pointers meet inside a cycle?

Modeling drill

Find the middle node of a singly linked list in one pass without counting its length first, and justify the approach.

LIFOFIFODequeNodeReferenceFast and slow pointersCycle