Heap – Interview Questions and Practice Problems

A heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B, then the key (the value) of a node A is ordered with respect to the key of node B with the same ordering applying across the heap. A heap can be classified further as either a “max-heap” or a “min-heap”. In a max-heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min-heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node.

The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact, priority queues are often referred to as “heaps”, regardless of how they may be implemented. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. The heap data structure, specifically the binary heap, was introduced as a data structure for the heapsort sorting algorithm. Heaps are also crucial in several efficient graph algorithms such as Dijkstra’s algorithm. In a heap, the highest (or lowest) priority element is always stored at the root. A heap is not a sorted structure and can be regarded as partially ordered. There is no particular relationship among nodes on any given level, even among the siblings. When a heap is a complete binary tree, it has the smallest possible height – a heap with N nodes always has log N height. A heap is a useful data structure when you need to remove the object with the highest (or lowest) priority.

 
In this post, we have listed out commonly asked interview questions that use heap data structure:

  1. Introduction to Priority Queues using Binary HeapsBeginner
  2. Min Heap and Max Heap Implementation – C++, JavaBeginner
  3. Check if an array represents a min-heap or notMedium
  4. Convert max heap to min heap in linear timeEasy
  5. Find k’th largest element in an arrayMedium
  6. Sort a k-sorted arrayMedium
  7. Merge M sorted lists of variable lengthHard
  8. Find k’th smallest element in an arrayMedium
  9. Find the smallest range with at least one element from each of the given listsHard
  10. Merge M sorted lists each containing N elementsHard
  11. Find first k non-repeating characters in a string in a single traversalMedium
  12. Connect n ropes with minimal costEasy
  13. Return k’th largest element in a streamMedium
  14. Huffman Coding Compression AlgorithmHard
  15. Replace each array element by its corresponding rankEasy
  16. Single-Source Shortest Paths – Dijkstra’s AlgorithmMedium
  17. Construct a Cartesian tree from an inorder traversalMedium
  18. Treap Data StructureBeginner
  19. Implementation of Treap Data Structure (Insert, Search, and Delete)Hard
  20. Heap Sort AlgorithmMedium
  21. Introsort Algorithm – Overview and C++ ImplementationHard
  22. External Merge Sort AlgorithmHard
  23. Efficiently merge k sorted linked listsHard
  24. Check if a binary tree is a min-heap or notMedium
  25. Convert a Binary Search Tree into a Min HeapHard
  26. Find first k maximum occurring words in a given set of stringsMedium

Rate this post

Average rating 4.87/5. Vote count: 77

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?

Thanks for reading.

To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and support our growth. Happy coding :)


guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!