Data Structures & AlgorithmsVery hardProduct company OA
Problem: union–find runs m = 10⁶ operations on n = 10⁶ elements. Version X uses union by rank AND path compression; version Y uses path compression only (always attaching the first root under the second); version Z uses union by rank only. Which bounds on the total time are correct?
- A. X: O(m·α(n)); Y: O(m log n) amortised in the worst case; Z: O(m log n), each find O(log n) worst case
- B. X, Y and Z are all O(m·α(n)), because path compression alone already gives the inverse-Ackermann bound
- C. X: O(m log* n) only; Y and Z: O(m·n) because trees can degenerate into chains
- D. X: O(m); Y: O(m log n); Z: O(m) because rank keeps every tree of height at most 2
Correct answer: A. X: O(m·α(n)); Y: O(m log n) amortised in the worst case; Z: O(m log n), each find O(log n) worst case
Rank alone bounds tree height by log₂ n, so every find is O(log n). Path compression alone has a matching worst case of Θ(m log_(1 + m/n) n), i.e. O(m log n) when m ≈ n – not α. Together they give Tarjan's O(m·α(n)), which is ≤ 4 for any practical n (log* n is a weaker but also valid bound). Y and Z cannot become O(n) per operation amortised: compression repairs chains, and rank prevents them. Rank does not bound height by 2.
More Coding Interview Problems questions
- Problem: a ring of n stalls holds 1 (occupied) or 0 (free). In one swap you exchange any two stalls. Find the minimum swaps to bring all…
- Problem: a stream of n = 2·10⁵ latencies; after each arrival report the median of the last k = 10⁵ values (sliding window median), time…
- Problem: A scoreboard calls a reading a 'leader' if it is strictly greater than every reading to its right (the last reading is always a…
- Problem: Return the squares of a sorted array (values may be negative) in non-decreasing order in O(n), filling the result from the back…
- Problem: A wallet starts with a positive balance s and then applies the day's changes in order. Find the minimum s such that the running…
- Problem: A warehouse aisle has shelves with weights; find the leftmost 'balance shelf' i such that the total weight strictly to its left…