Data Structures & AlgorithmsHardProduct company OA
Quickselect with Lomuto partition (pivot = last element) looks for the 3rd smallest element of [7, 2, 9, 4, 3, 8], recursing only into the side that contains the target. How many partition calls are made?
- A. 1
- B. 2
- C. 4
- D. 3
Correct answer: C. 4
Target index k = 2. Call 1 (whole array, pivot 8) puts 8 at index 4 → array [7, 2, 4, 3, 8, 9] → go left. Call 2 on indices 0–3 (pivot 3) puts 3 at index 1 → [2, 3, 4, 7, …] → go right. Call 3 on indices 2–3 (pivot 7) puts 7 at index 3 → go left. Call 4 on the one-element range 2–2 puts 4 at index 2 = k. Answer 4 with 4 partition calls; stopping the count when the range first contains the answer gives 3.
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
- Binary search finds the index of the minimum of the rotated sorted array [15, 18, 22, 3, 6, 9, 12] using: mid = (lo + hi) / 2; if a[mid] >…
- The sorted array [3, 4, 7, 10, 11] contains some positive integers. What is the 4th positive integer MISSING from it?
- For [2, 3, 0, 4], out[i] is the product of all elements except a[i], computed with prefix and suffix products (no division). What is out?
- The intervals [1, 4], [6, 8], [2, 3], [8, 10], [12, 13] are sorted by start and merged (intervals that touch at an endpoint also merge)…
- A 'leader' is an element strictly greater than every element to its right (the last element is always a leader). Scanning from the right…
- The words stop, pots, opts, post, tops, spot, pot, top are grouped so that anagrams share a group (key = the sorted letters). How many…