Lesson overview · Free interview practice

Heaps

Discover heap structures and their use in priority queues.

Topics in the full lesson
  • Concept and Use Cases
  • Types of Heaps
  • When to Use
  • Time and Space Complexity
  • Heap Operations and Methods
  • Practical Tips and Tricks
  • Common Gotchas
  • Advanced Topics
  • Heap Algorithms
  • Interview Tips and Tricks
  • Common Mistakes

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Question

Given an array of points where points[i] = [xi, yi] (2D Euclidean), and an integer k, return the k points closest to the origin (0, 0) (any order). Solve with a max-heap of size k — the canonical heap-based interview answer.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given a string s, sort it in decreasing order based on the frequency of the characters (most frequent first). The frequency of a character is the number of times it appears in the string. Return the sorted string. If two characters have the same frequency, any valid ordering between them is accepted.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given an integer array nums and an integer k, return the k-th largest element in the array. Note that it is the k-th largest in sorted order, not the k-th distinct element. Solve it using a min-heap of size k — the canonical heap-based interview answer.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given an array of products and a string searchWord, design a system that, after each character of searchWord is typed, returns up to three product names that start with the prefix typed so far. When more than three products share the prefix, return the three lexicographically smallest. Return a list of lists — one suggestion list per typed character.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Heaps material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium