Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
A disjoint-set forest uses path compression but NO union by rank/size. What is its amortised cost per operation over m operations on n elements (m ≥ n)?
- A. O(α(n)), the same as with union by rank
- B. O(1) worst case per operation
- C. O(n) amortised, because trees can become chains
- D. O(log n) amortised; reaching O(α(n)) needs union by rank as well
Correct answer: D. O(log n) amortised; reaching O(α(n)) needs union by rank as well
Path compression alone gives Θ(m log_(1+m/n) n) total (Tarjan–van Leeuwen), i.e. O(log n) per operation when m ≈ n. The inverse-Ackermann bound α(n) needs both heuristics; a single find can still be O(n) once, but compression makes chains short afterwards.
More Coding Interview Problems questions
- What is the running time of Dijkstra's algorithm with a Fibonacci heap on a graph with V vertices and E edges?
- For an array of n = 10⁶ integers, report the number of DISTINCT values in every window of k = 10⁵ consecutive elements. Which approach is…
- n = 10⁵ points are given, followed by q = 10⁵ OFFLINE queries 'how many points lie in the axis-aligned rectangle [x1, x2] × [y1, y2]?'…
- Edge case: integer division and modulo with negative operands in Python. python print(-7 // 2, -7 % 2, int(-7 / 2)) What is printed?
- Problem: Replace at most k characters so that the longest possible substring consists of one repeated letter; return its length. python…
- Problem: Smallest length of a contiguous subarray of POSITIVE integers with sum ≥ target (0 if none). python def minlen(a, t): l, s, best…