Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
From the max-heap [50, 30, 40, 10, 20, 35, 25], delete the maximum once. What is the array?
From the max-heap [50, 30, 40, 10, 20, 35, 25], delete the maximum once. What is the array?
import heapq h = [-x for x in [50, 30, 40, 10, 20, 35, 25]] heapq.heappop(h) print([-x for x in h])
- A. [40, 30, 35, 10, 20, 25]
- B. [40, 30, 25, 10, 20, 35]
- C. [30, 40, 35, 10, 20, 25]
- D. [40, 35, 30, 25, 20, 10]
Correct answer: A. [40, 30, 35, 10, 20, 25]
Move the last element (25) to the root, then sift down by swapping with the larger child: 25 ↔ 40, then 25 ↔ 35 (children of index 2 are 35 only). Result [40, 30, 35, 10, 20, 25]. Each delete is O(log n).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is printed? (count leaves, internal nodes and full nodes of a binary tree) python T = {1: (2, 3), 2: (4, None), 3: (5, 6), 4: (None…
- A binary tree has preorder A B D E C F G and inorder D B E A F C G. What is its postorder? python def post(pre, ino): if not pre: return…
- How many structurally different binary trees have n = 4 nodes, and how many distinct labelled binary trees can be formed from 4 distinct…
- What is printed? (number of nodes in a complete binary tree in O(log² n)) python def count(nnodes): # implicit complete tree with nodes…
- What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?) python def balancecase(order): a, b, c…
- What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)? python def nmin(h): a, b = 1, 2 # N(0), N(1)…