Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
Assertion (A): Quick sort with the last element as pivot takes Θ(n²) time on an array that is already sorted. Reason (R): On sorted input every partition splits the array into parts of sizes n − 1 and 0.
- A. Both A and R are true, but R is not the correct explanation of A
- B. Both A and R are true, and R is the correct explanation of A
- C. A is true, but R is false
- D. A is false, but R is true
Correct answer: B. Both A and R are true, and R is the correct explanation of A
With the maximum as pivot each call peels off only one element, giving n + (n − 1) + … = Θ(n²) comparisons; R is exactly why.
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 Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[2][2] = 5…
- Rearrange a linked list so that nodes at odd POSITIONS (1st, 3rd, …) come first, followed by the even-position nodes, keeping relative…
- What is the length of the longest contiguous substring of ((()((()(()) that is a valid (balanced) parentheses string?
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[90, 11, 62, 33], [40, 71, 82, 13], [10, 81…