GATE Computer ScienceModerateGATE CS (PYQ pattern)
In a min-heap stored 1-indexed in an array with children of i at 2i and 2i+1, a node at index 77 is replaced by a smaller key and sifted up to the root. How many comparisons does the sift-up use at most, and which indices does the key pass through?
- A. 7 comparisons: indices 77, 38, 19, 9, 4, 2, 1
- B. 6 comparisons: indices 77, 39, 19, 9, 4, 2, 1
- C. 6 comparisons: indices 77, 38, 19, 9, 4, 2, 1
- D. 5 comparisons: indices 77, 38, 19, 9, 4, 2
Correct answer: C. 6 comparisons: indices 77, 38, 19, 9, 4, 2, 1
Parents are found by integer division by 2: 77, 38, 19, 9, 4, 2, 1. That is 6 moves up, hence at most 6 comparisons with parents.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int a[] = {5, 3, 8, 4, 2, 9, 1, 7, 6, 10}; int st[10], top = -1, nge[10]; for (int i = 0; i < 10; i++) nge[i] = -1; for (int i = 0; i <…
- A min-stack keeps an auxiliary stack: a value is pushed on it when it is less than or equal to the current auxiliary top (or it is empty)…
- for (i = 1; i < 1000; i = 2) for (j = 0; j < i; j++) cnt++; What is the final value of cnt (starting from 0)?
- A table of size 11 uses linear probing with h(k) = k mod 11 and tombstones: a deleted slot is marked DELETED; search continues past…
- The maximum of every window of 3 consecutive elements of [4, 2, 12, 11, -5, 7, 9, 3, 1, 6] is found with a deque of indices: before…
- A singly linked list has 25 nodes. A routine places pointer p at the head and pointer q at the 6th node from the head, then advances both…