Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: What is the average-case and worst-case time complexity of the quickselect algorithm for finding the k-th smallest element?
- A. O(log n) average and worst case
- B. O(n) always, with no worst case
- C. O(n log n) average and worst case
- D. O(n) average case, O(n^2) worst case
Correct answer: D. O(n) average case, O(n^2) worst case
Like quicksort, quickselect partitions around a pivot but only recurses into the side containing the target rank, giving O(n) expected time; an adversarial pivot choice (e.g. always picking the smallest/largest) degrades it to O(n^2).
More Coding Interview Problems questions
- Problem: Which of these sorting algorithms is guaranteed O(n log n) in the worst case AND is stable (preserves the relative order of equal…
- Problem: Counting sort can sort n integers in the range [0, k] in O(n + k) time. What is the key limitation that makes it unsuitable for…
- Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?
- Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time…