Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [14, 4, 8, 46, 16, 34, 31, 40] and k = 3, what does it return, and how many elements does the first Lomuto partition (pivot = last element) place to the LEFT of the pivot?
- A. 34; 6
- B. 14; 6
- C. 14; 1
- D. 16; 6
Correct answer: B. 14; 6
The k-th smallest of [4, 8, 14, 16, 31, 34, 40, 46] is 14. The first partition around 40 puts the 6 smaller elements to its left; quickselect then recurses into only one side.
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
- How many contiguous subarrays of [6, 4, 2, 5, 6, 2, 3, 3] have a product strictly less than 30? (Positive values: a two-pointer window…
- 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…
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…
- Evaluate the postfix expression 7 4 − 2 + 6 − 8 × 7 1 + + (all divisions are exact). What is its value, and what is the maximum number of…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…