Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
Quick sort with the Lomuto partition (pivot = last element) is run on an already sorted array of 10 distinct elements. How many element-to-pivot comparisons does it make?
- A. 10
- B. 45
- C. 34
- D. 90
Correct answer: B. 45
The pivot is always the largest element, so each partition removes just one element: 9 + 8 + … + 1 = 10 × 9/2 = 45 comparisons, the O(n²) worst case. About 34 would be n log₂ n for balanced splits.
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 (one three-way comparison per probe) is done on a sorted array of 1000 elements. What is the maximum number of probes needed?
- This code tries to reverse the singly linked list 1 → 2 → 3 → 4, but its last line has been changed from cur = nxt. What does the list…
- A linked list has 3 nodes before the loop starts and a loop of 5 nodes. Floyd's slow pointer moves 1 step and fast pointer 2 steps per…
- ^ is exponentiation (highest precedence, right-associative). What is the postfix form of A + B C ^ D ^ E − F?
- What is the value of the postfix expression 2 3 2 ^ ^ (^ means power)?
- What is the minimum stack size needed to evaluate the postfix expression a b c d + e f − / + ?