Data Structures & AlgorithmsVery hardProduct company OA
Running median with two heaps (a max-heap for the lower half, a min-heap for the upper half, sizes differing by at most 1, the lower half keeping the extra element). After the stream 5, 15, 1, 3, 8, 7, what are the two heap tops and the median?
- A. lower top 5, upper top 8, median 6.5
- B. lower top 3, upper top 7, median 5.0
- C. lower top 5, upper top 7, median 6.0
- D. lower top 7, upper top 8, median 7.5
Correct answer: C. lower top 5, upper top 7, median 6.0
Sorted stream: 1, 3, 5, 7, 8, 15. The lower half {1, 3, 5} has max 5 and the upper half {7, 8, 15} has min 7, so the median is (5 + 7) / 2 = 6.0. Reporting the middle of the arrival order or forgetting to rebalance after 7 arrives gives the other pairs.
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
- Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?
- Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?
- An alien language uses only the letters a, b, c, d. The words bca, bcd, ad, ac, dc are listed in sorted order. Building an edge from each…
- What is the maximum flow from S to T in the network with capacities S→A 10, S→B 5, A→B 15, A→T 6, B→T 10?
- Weighted undirected graph: A–B 1, B–C 2, C–A 2, C–D 1, B–D 2. How many DIFFERENT minimum spanning trees does it have?
- Keys 85, 62, 86, 26, 40, 54, 92, 17, 18, 67 are inserted into an empty AVL tree (giving preorder 62, 40, 18, 17, 26, 54, 86, 85, 67, 92)…