Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
Two sorted linked lists A = [1, 4, 6, 9] and B = [2, 3, 7, 10, 12] are merged with the standard two-pointer merge, which compares the current heads once per output node until one list is empty. How many comparisons are performed?
- A. 8
- B. 4
- C. 20
- D. 7
Correct answer: D. 7
Each comparison outputs one node; 1,2,3,4,6,7,9 are output by comparisons, then A is empty, so 7. The worst-case formula n + m − 1 gives 8, min(n, m) = 4 is the best case, and 20 = n·m 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
- How many distinct non-empty palindromic subsequences does the string abcba have (distinct as strings, not as index sets)?
- What is the sum of gcd(i, 12) over i = 1 to 12?
- Five customers arrive together at a bank with 2 tellers. Service times in arrival order are 3, 5, 2, 4 and 6 minutes. Each customer goes…
- A stack starts empty. Operations in order: push 1, push 2, pop, push 3, push 4, pop, pop. Write each popped value in order and read the…
- Buildings of heights 3, 7, 8, 3, 6, 1 stand in a row. A building is visible from the left if no building to its left is at least as tall…
- What is the length of the longest contiguous subarray of [4, 2, 4, 5, 6, 2, 4, 1] that contains no repeated value?