Data Structures & AlgorithmsModerateAmazon · Microsoft OA
Building a binary heap from n given keys: what are the worst-case costs of (i) bottom-up heapify and (ii) n successive inserts into an empty heap?
- A. (i) Θ(n log n), (ii) Θ(n log n)
- B. (i) Θ(n), (ii) Θ(n)
- C. (i) Θ(log n), (ii) Θ(n log n)
- D. (i) Θ(n), (ii) Θ(n log n)
Correct answer: D. (i) Θ(n), (ii) Θ(n log n)
Bottom-up heapify sifts down from the last internal node; about n/2^(h+1) nodes sit at height h and each costs O(h), and Σ h/2^h converges, giving Θ(n). Inserting keys in increasing order into a max-heap makes every insert sift all the way up, so n inserts cost Θ(n log n).
More Coding Interview Problems questions
- In Java 8+, many keys that are Comparable and all land in ONE HashMap bucket. What is the worst-case cost of get() on that bucket once it…
- Problem: For each day, how many days until a strictly warmer day (0 if none)? python def wait(t): res, st = [0] len(t), [] for i, x in…
- Problem: The span of a stock on a day is the number of consecutive days up to and including it with price ≤ that day's price. python def…
- Problem: Asteroids move along a line: positive = right, negative = left, |value| = size. When two meet, the smaller explodes; equal sizes…
- Problem: Decode a string where k[s] means s repeated k times (brackets can nest). python def decode(s): st, cur, k = [], "", 0 for c in s…
- Problem: Evaluate an expression with +, −, parentheses and non-negative integers, using a stack of signs. python def calc(s): res, num…