Data Structures & AlgorithmsHard
Two sorted lists 1 4 6 9 and 2 3 7 8 10 are merged with the standard two-pointer merge (take the smaller head; on a tie take from the first list). Count the element comparisons made until one list is exhausted; the rest of the other list is then copied without comparisons.
- A. 9
- B. 8
- C. 4
- D. 20
Correct answer: B. 8
Outputs 1, 2, 3, 4, 6, 7, 8, 9 each need one comparison; then the first list is empty and 10 is copied free: 8. 9 = n + m counts the copy too, 4 is the best case min(n, m), and 20 compares every pair.
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
- In the sorted array 1 2 2 2 3 5 5 8, what is the 0-based index of the FIRST occurrence of the value 5 (the leftmost position where 5…
- How many index pairs i < j in 4 9 2 7 5 3 8 have A[i] + A[j] strictly greater than the MEDIAN of the whole array (the median is the middle…
- Why is the standard binary-search probe count for an unsuccessful search about log₂(n), not n?
- When sorting records by key with counting sort, why must the placement pass proceed in a specific direction for stability?
- Why does the cycle-based 'place each value at its index' method find missing numbers in linear time?
- Why does Floyd's method for finding a duplicate in an array of n+1 values in 1..n view the array as a linked structure?