Data Structures & AlgorithmsHardGATE CS · Product placement
The median-of-medians (groups of 5) selection algorithm is used to choose a pivot. What does it guarantee?
- A. Worst-case O(n) time for finding the k-th smallest element
- B. A perfectly balanced 50/50 split every time
- C. Worst-case O(log n) time
- D. Expected O(n log n) time
Correct answer: A. Worst-case O(n) time for finding the k-th smallest element
The pivot is guaranteed to have at least about 3n/10 elements on each side, giving T(n) ≤ T(n/5) + T(7n/10) + O(n) = O(n) in the worst case.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Over all left rotations of [7, 7, 1, 1, 3, 6], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 7, "pop", 3, "pop"…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed? python a, k = [2, 11, 6, 2, 5, 9…