Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP
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 [11, 22, 2, 55, 7, 32, 57, 44]. What is the array afterwards and the returned index j?
- A. [7, 2, 22, 55, 11, 32, 57, 44]; j = 2
- B. [2, 7, 11, 22, 32, 57, 44, 55]; j = 1
- C. [7, 2, 22, 55, 11, 32, 57, 44]; j = 1
- D. [2, 7, 11, 22, 32, 44, 55, 57]; j = 1
Correct answer: C. [7, 2, 22, 55, 11, 32, 57, 44]; j = 1
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 [7, 2, 22, 55, 11, 32, 57, 44] with j = 1.
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 inversions (pairs i < j with a[i] > a[j]) does [5, 6, 10, 37, 38, 4, 17, 23] have? (This is also the number of swaps bubble sort…
- Keys 112, 95, 54, 144, 21, 31, 32 are inserted in this order into an empty table of size 11 by double hashing: h1(k) = k mod 11, h2(k) = 7…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A checker counts how many strings are balanced over (), [] and {}. Version A uses a stack; version B only keeps one counter of open minus…
- A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 1 3 7 6; 6 8 8 6; 1 9 7 8…
- How many contiguous subarrays of [3, −2, 5, 0, −2, 1, 4, 5, 0] have sum exactly 5? (Prefix sums with a hash map of prefix-sum frequencies.)