chapter

    Heaps, Priority Queues and Data Structure Operations Pyqs for Gate Cs

    Solve 8+ Heaps, Priority Queues and Data Structure Operations previous year questions for GATE CS with answers and detailed solutions. Free sample questions b

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Let be an odd number greater than 100. Consider a binary minheap with elements stored in an array whose index starts from 1.

    Which of the following indices of do/does NOT correspond to any leaf node of the minheap?
    Question 2
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data structures:

    P: Unsorted doubly linked list with pointers to the head node and tail node of the list.

    Q: Min-heap implemented using an array.

    R: Binary Search Tree.

    Which ONE of the following options gives the worst-case time complexities for meld operation on instances of size of these data structures?
    Question 3
    2025 Slot Set1 PYQ
    Level 3: Exam Standard
    The height of any rooted tree is defined as the maximum number of edges in the path from the root node to any leaf node.

    Suppose a Min-Heap stores 32 keys. The height of is _____________. (Answer in integer)
    Question 4
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    Consider a binary min-heap containing 105 distinct elements. Let be the index (in the underlying array) of the maximum element stored in the heap. The number of possible values of is

    Question 5
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    An array [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified. Which one of the following options represents the first three elements in the heapified array?

    Question 6
    2023 PYQ
    Level 3: Exam Standard

    Which one of the following sequences when stored in an array at locations forms a max-heap?

    Question 7
    2023 PYQ
    Level 3: Exam Standard
    Let A be a priority queue for maintaining a set of elements. Suppose A is implemented using a max-heap data structure. The operation Extract-Max(A) extracts and deletes the maximum element from A. The operation Insert(A,key) inserts a new element key in A. The properties of a max-heap are preserved at the end of each of these operations.

    When A contains n elements, which one of the following statements about the worst case running time of these two operations is TRUE?
    Question 8
    2021 Slot Set2 PYQ
    Level 3: Exam Standard

    Let be a binary min-heap consisting of elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in ?

    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Heaps, Priority Queues and Data Structure Operations PYQs for GATE CS

    Solve 8+ Heaps, Priority Queues and Data Structure Operations previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Heaps, Priority Queues and Data Structure Operations

    Programming and Data Structures

    Heaps, Priority Queues & DS Operations

    From tree geometry to priority queue mastery.

    1

    Heap Structure, Height & Leaf Positions

    Complete binary tree geometry, array indexing, height formula, identifying leaves. Highest weightage in this chapter.

    2

    Heap Construction & Array Validation

    Build-heap procedure, heapify, checking if a given array forms a valid heap.

    3

    Priority Queue Operations & Heap Extrema

    Insert, extract-min or extract-max, finding extreme elements, complexity analysis.

    4

    Meld Operations & Data Structure Complexity

    Merging two heaps, comparing meld cost across data structures.

    Heap Structure, Height and Leaf Positions

    Heaps, Priority Queues & DS Operations › Topic 1

    Heap Structure, Height & Leaf Positions

    The shape of the heap determines everything you can do with it.

    What you will learn here

    • Why a heap is a complete binary tree and what that shape guarantees
    • The exact array-to-tree index mapping (parent, left child, right child)
    • How to compute heap height from in one formula
    • Which indices are internal nodes vs leaves
    • Where extreme elements can hide in a heap

    Heaps, Priority Queues and Data Structure Operations: Solved Questions with Step-by-Step Explanations (8 Problems)

    Question 1 · Programming and Data Structures · 2026_Set1 MSQ
    Let be an odd number greater than 100. Consider a binary minheap with elements stored in an array whose index starts from 1.

    Which of the following indices of do/does NOT correspond to any leaf node of the minheap?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    ["B","C"]

    Step-by-Step Solution

    Insight: In a complete binary tree with nodes, the leaf nodes are exactly those at indices to . The internal nodes are $1$ to . We just need to check which options fall in the internal node range.

    Exam route:

    1. Identify the range of leaf indices for a heap of size : from to .
    2. Since is odd, let . The internal nodes are $1$ to , and leaves are to .
    3. Evaluate each option:
    • (Leaf)
    • (Internal)
    • (Internal)
    • (Leaf)
    1. The question asks for indices that do NOT correspond to leaf nodes, which are the internal nodes.
    2. These are and .

    Learning route:

    1. A binary heap is a complete binary tree.
    2. The array representation maps the tree level by level.
    3. For an array of size , the parent of node is .
    4. A node is a leaf if it has no children, i.e., .
    5. Thus, internal nodes are .
    6. For odd , .
    7. So internal nodes are .
    8. Option A is , which is , so it's a leaf.
    9. Option B is , which is , so it's an internal node.
    10. Option C is , which is , so it's an internal node.
    11. Option D is , which is the last element, always a leaf.
    12. Therefore, B and C do not correspond to leaf nodes.
    Question 2 · Programming and Data Structures · 2025_Set2 MCQ
    A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data structures:

    P: Unsorted doubly linked list with pointers to the head node and tail node of the list.

    Q: Min-heap implemented using an array.

    R: Binary Search Tree.

    Which ONE of the following options gives the worst-case time complexities for meld operation on instances of size of these data structures?
    1. A.

      P: , Q: , R:

    2. B.

      P: , Q: , R:

    3. C.

      P: , Q: , R:

    4. D.

      P: , Q: , R:

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: Meld complexity depends on the physical implementation. DLL with tail pointer is O(1), array heap requires O(n) build-heap, and BST can be melded optimally in O(n) by flattening and rebuilding.

    Exam route:

    P: Unsorted DLL with head and tail. Link tail of first to head of second. .

    Q: Array min-heap. Concatenate arrays () and run Build-Heap (). Total .

    R: BST. Optimal meld: inorder traversal of both (), merge sorted arrays (), build balanced BST (). Total .

    Matches Option A.

    Learning route:

    1. Analyze P: Unsorted DLL with tail pointer allows direct concatenation in time.
    2. Analyze Q: Array-based heap requires physical concatenation () and restoring the heap property using Build-Heap, which takes time, not .
    3. Analyze R: While naive insertion into a BST takes worst-case, the optimal algorithmic approach (flatten, merge, rebuild) takes time.
    4. Combine the results: P is , Q is , R is .
    Question 3 · Programming and Data Structures · 2025_Set1 NAT
    The height of any rooted tree is defined as the maximum number of edges in the path from the root node to any leaf node.

    Suppose a Min-Heap stores 32 keys. The height of is _____________. (Answer in integer)
    Correct Answer:

    5.00

    Step-by-Step Solution

    Insight: This is a direct application of the heap height formula. The height of a complete binary tree with nodes is simply .

    Exam route: The problem states the min-heap stores 32 keys. Using the height formula , we substitute . Since , . The floor of 5 is 5. The height is 5.

    Learning route:

    1. Understand the definition: The height of a rooted tree is the maximum number of edges on any path from the root to a leaf.
    2. Recall the structure of a heap: A heap is a complete binary tree. This means all levels except possibly the last are completely full, and the last level is filled from left to right.
    3. Apply the formula: For a complete binary tree with nodes, the height is given by .
    4. Calculate: Here, . We know that , so .
    5. Conclusion: The height of the min-heap is 5.
    Question 4 · Programming and Data Structures · 2024_Set1 MCQ

    Consider a binary min-heap containing 105 distinct elements. Let be the index (in the underlying array) of the maximum element stored in the heap. The number of possible values of is

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: In a min-heap, the maximum element must be located at one of the leaf nodes. The problem reduces to finding the total number of leaf nodes in a complete binary tree with 105 nodes.

    Exam route:

    1. A min-heap is a complete binary tree. The maximum element can never be an internal node because an internal node must be smaller than or equal to its children. Thus, the maximum must be a leaf.
    2. For a complete binary tree with nodes, the number of leaf nodes is .
    3. Substitute . The number of leaves is .
    4. Since all elements are distinct, the maximum element could be any of these 53 leaves. Thus, there are 53 possible values for the index .

    Learning route:

    1. Understand the min-heap property: Every parent is less than or equal to its children.
    2. Deduce the location of the maximum: If the maximum element were at an internal node, it would have to be less than or equal to its children. But it is the maximum, so it cannot be strictly less than any element. This implies it cannot have any children. Therefore, it must be a leaf.
    3. Count the leaves: In a complete binary tree represented as an array of size , the leaves occupy the indices from to .
    4. Calculate the count: The number of leaves is .
    5. Apply to : .
    Question 5 · Programming and Data Structures · 2024_Set1 MCQ

    An array [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified. Which one of the following options represents the first three elements in the heapified array?

    1. A.

      82, 90, 101

    2. B.

      82, 11, 93

    3. C.

      131, 11, 93

    4. D.

      131, 111, 90

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: Build-Max-Heap starts from the last internal node and sifts down. We must trace the swaps carefully to find the final root and its children.

    Exam route:

    Array: (1-based indexing).

    Internal nodes: 1 to 5. Start from down to 1.

    (111): child . , no swap.

    (11): children . Max child is 131. Swap 11 and 131. Array becomes: .

    (90): children . , no swap.

    (101): children . Max child is 131. Swap 101 and 131. Array: . Sift 101 down: children . Max child is 44. Swap 101 and 44. Array: .

    (82): children . Max child is 131. Swap 82 and 131. Array: . Sift 82 down: children . Max child is 111. Swap 82 and 111. Array: .

    First three elements: 131, 111, 90.

    Learning route:

    1. The algorithm for Build-Max-Heap processes internal nodes from down to 1.
    2. For , we start at index 5 and go down to 1.
    3. Trace each step carefully, ensuring that after a swap, the sifted-down element is compared with its new children.
    4. The final root will be the maximum element (131), and its children will be determined by the final sift-down steps.
    Question 6 · Programming and Data Structures · 2023 MCQ

    Which one of the following sequences when stored in an array at locations forms a max-heap?

    1. A.

      23, 17, 10, 6, 13, 14, 1, 5, 7, 12

    2. B.

      23, 17, 14, 7, 13, 10, 1, 5, 6, 12

    3. C.

      23, 17, 14, 6, 13, 10, 1, 5, 7, 15

    4. D.

      23, 14, 17, 1, 10, 13, 16, 12, 7, 5

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: A max-heap requires every parent to be greater than or equal to its children. We can quickly eliminate options by finding a single violation.

    Exam route:

    Check Option A: At , . Its children are and . Since , this violates the max-heap property. Eliminate A.

    Check Option C: At , . Its child is . Since , this violates the property. Eliminate C.

    Check Option D: At , . Its children are and . Since , this violates the property. Eliminate D.

    Check Option B: Every internal node is greater than or equal to its children. Specifically, ; ; ; ; and . Option B satisfies all conditions.

    Learning route:

    1. The max-heap property is and .
    2. We only need to check internal nodes, i.e., indices 1 to .
    3. Finding a single violation is enough to reject an option.
    4. Option B satisfies the property for all internal nodes.
    Question 7 · Programming and Data Structures · 2023 MCQ
    Let A be a priority queue for maintaining a set of elements. Suppose A is implemented using a max-heap data structure. The operation Extract-Max(A) extracts and deletes the maximum element from A. The operation Insert(A,key) inserts a new element key in A. The properties of a max-heap are preserved at the end of each of these operations.

    When A contains n elements, which one of the following statements about the worst case running time of these two operations is TRUE?
    1. A.

      Both Extract-Max(A) and Insert(A,key) run in .

    2. B.

      Both Extract-Max(A) and Insert(A,key) run in .

    3. C.

      Extract-Max(A) runs in whereas Insert(A,key) runs in .

    4. D.

      Extract-Max(A) runs in whereas Insert(A,key) runs in .

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: Extract-Max and Insert both require maintaining the heap property, which involves traversing the height of the tree.

    Exam route: Extract-Max replaces the root with the last element and percolates down. Insert appends to the end and percolates up. Both percolations take time proportional to the height of the heap, which is .

    Learning route:

    1. Identify the operations: Extract-Max and Insert on a max-heap.
    2. Extract-Max: The maximum element is at the root. To extract it, we swap it with the last element, remove the last element, and then percolate the new root down to its correct position. This percolation takes time in the worst case.
    3. Insert: We add the new element at the end of the array and percolate it up to its correct position. This also takes time in the worst case.
    4. Conclusion: Both operations have a worst-case time complexity of .
    Question 8 · Programming and Data Structures · 2021_Set2 MCQ

    Let be a binary min-heap consisting of elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in ?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: In a min-heap, the maximum element must be a leaf. Since there are leaves, finding the maximum requires checking all of them in the worst case.

    Exam route:

    1. The min-heap property ensures parents are smaller than children. Thus, the maximum element cannot be an internal node; it must be a leaf.
    2. The number of leaves in a binary heap of size is .
    3. There is no ordering among the leaves. To find the maximum, we must inspect all leaves.
    4. Inspecting elements takes time.

    Learning route:

    1. Understand the min-heap property: and .
    2. This implies any internal node is smaller than its descendants. Therefore, the global maximum must be at the leaf level.
    3. The leaves occupy indices from to .
    4. Since there is no horizontal ordering, the worst-case optimal algorithm must scan all leaves, taking time.

    More previous year questions (pyqs) in this unit