LESSON 41 / 75·2 min readIntermediate

Heaps and priority queue in Java

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Important class

java
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

Problems to practice

  • Kth largest element
  • Top K frequent elements
  • Merge K sorted lists
  • Find median in stream

Example: min heap

java
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(3);
pq.offer(9);
System.out.println(pq.peek()); // 3

Time and space complexity

  • Insert: O(log n)
  • Peek: O(1)
  • Remove min: O(log n)

PAUSE & RECALL

Can you explain it without the notes?

Heaps and priority queue in Java

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Important class

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Graphs in Java

Adapted from 15_DSA_JAVA.md · “8. Heaps and priority queue in Java”. Examples are study fragments; framework behavior can depend on your version.