Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j, return j) is applied to [28, 7, 13, 18, 27, 11, 44, 47]. What is the array afterwards and the returned index j?
- A. [11, 7, 13, 18, 27, 28, 44, 47]; j = 5
- B. [7, 13, 18, 27, 11, 28, 47, 44]; j = 4
- C. [7, 11, 13, 18, 27, 28, 44, 47]; j = 4
- D. [11, 7, 13, 18, 27, 28, 44, 47]; j = 4
Correct answer: D. [11, 7, 13, 18, 27, 28, 44, 47]; j = 4
Hoare's scheme swaps out-of-place pairs from both ends and does NOT put the pivot in its final position; it only guarantees a[0..j] ≤ pivot ≤ a[j+1..]. Tracing gives [11, 7, 13, 18, 27, 28, 44, 47] with j = 4.
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
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 16 in [18, 39, 40, 44, 45, 56, 60, 62, 70, 84…
- For the sorted array [3, 3, 4, 5, 5, 7, 7, 8, 8, 8, 8, 10] (0-based), what are lowerbound(5) (first index with value ≥ 5) and…
- How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- What does this loop-counting code print? (Python) python n, c = 16, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [0, 9, 2, 3, 4, 2…