Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
Quicksort with Lomuto partitioning (last element as pivot) sorts [12, 42, 92, 73, 37, 39, 75, 75, 32]. How many swaps happen in total, across all recursive calls?
- A. 3
- B. 4
- C. 6
- D. 5
Correct answer: B. 4
Each partition call swaps a[i] with a[j] whenever a smaller-or-equal element is found, plus the final pivot swap; summed over all recursive calls this totals 4 swaps.
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
- A singly linked list is 95 -> 83 -> 36 -> 42 -> 99 -> 64 -> 72 -> None. The first 5 nodes are reversed in place (the node that was…
- Using a monotonic (increasing) stack to find, for each element of [20, 17, 18, 14, 44, 28], the nearest element to its left that is…
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [48, 12, 29, 6, 30, 28] (push index i after…
- A singly linked list is 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None. The standard iterative in-place reversal (prev = None; while cur…
- Using the sliding window technique, what is the maximum sum of any 8 consecutive elements in [24, 30, 4, 27, 25, 2, 13, 22, 26, 1]?
- A singly linked list has nodes at positions 0..6 holding 58 -> 20 -> 49 -> 17 -> 78 -> 37 -> 22, but the last node's next pointer points…