Data Structures · Visualization

Priority Queue: Peek

Priority Queue

Peeking looks at the current highest-priority item (the root of the underlying heap) without removing it. Time: O(1). Space: O(1).

Big-O: Insert/serve O(log n).

Priority Queue: Peek

Priority Queue

Peeking looks at the current highest-priority item (the root of the underlying heap) without removing it. Because it's always sitting right at the root, this check is instant and doesn't disturb the queue.

How Peek Works

  1. Check that the priority queue is not empty.
  2. Read the value at the root of the underlying heap.
  3. Return it without changing the structure.

Peek Worked Example

Input: Peek at priority queue heap [D(7), C(5), B(1), A(3)]

  1. Look at the root without removing it: D(7)

Output: D(7) (queue unchanged)

Introduction
A complete binary tree stored in an array, where every parent is bigger than its children — so the largest value always sits at the root.
90
0
60
1
70
2
20
3
40
4
55
5
66
6
10
7
Bubbling / sifting
Max-Heap — Introduction
Press ▶ Run to watch it step by step.
Python · Heaps in Code
import heapq

# heapq is a MIN-heap by default
tasks = []
heapq.heappush(tasks, (3, "low"))
heapq.heappush(tasks, (1, "urgent"))
heapq.heappush(tasks, (2, "normal"))

while tasks:
    priority, name = heapq.heappop(tasks)
    print(priority, name)   # highest priority first

# For max-priority: push (-p, item)
# example: priority queue
Input: Peek at priority queue heap [D(7), C(5), B(1), A(3)]

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Peek Time & Space Complexity

TimeO(1)
SpaceO(1)

When to Use Peek

  • Checking which task would run next without actually starting it
  • Deciding whether the next item is worth processing before extracting it
  • Displaying 'next up' information in a scheduling dashboard

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.