Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
How many of these statements are correct? (i) Merge sort is stable. (ii) Heap sort runs in O(n log n) even in the worst case. (iii) Quick sort with a randomly chosen pivot runs in O(n log n) in the worst case. (iv) Insertion sort runs in O(n) on an already sorted array.
- A. Only one
- B. Only two
- C. Only three
- D. All four
Correct answer: C. Only three
(i), (ii) and (iv) are true. (iii) is false: random pivots give O(n log n) expected time, but a run of bad pivots can still cost O(n²), so the worst case is unchanged.
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
- What is the time complexity of this code fragment? c int count = 0; for (int i = 1; i < n; i = 2) for (int j = 0; j < i; j++) count++;
- What is the time complexity of this code fragment? c for (int i = 1; i <= n; i++) for (int j = i; j <= n; j += i) work(); // O(1)
- What value of count does this code print for n = 16? c int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count++…
- What does this code print? c int c = 0, n = 10; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) for (int k = j + 1; k < n…
- Keys 12, 22, 32 are inserted into a size-10 table with h(k) = k mod 10 and linear probing. Then 22 is deleted by simply setting its slot…
- Keys 18, 41, 22, 44, 59, 32, 31, 96 are inserted in that order into a table of size 13 by double hashing: h₁(k) = k mod 13, h₂(k) = 7 − (k…