Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
Two sorted linked lists [9 → 14 → 35 → 36 → 37] and [27 → 29 → 31 → 32] are merged with the standard O(n+m) two-pointer merge (always take the smaller head, splicing nodes, no new array). What is the merged list?
- A. 9 → 27 → 14 → 29 → 35 → 31 → 36 → 32 → 37
- B. 9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37
- C. 9 → 14 → 35 → 36 → 37 → 27 → 29 → 31 → 32
- D. 37 → 36 → 35 → 32 → 31 → 29 → 27 → 14 → 9
Correct answer: B. 9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37
At each step the merge compares the current heads of both lists and splices the smaller one onto the result, advancing only that list's pointer; this produces the fully sorted list [9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37] in one linear pass. Simply concatenating the two lists, [9 → 14 → 35 → 36 → 37 → 27 → 29 → 31 → 32], is not sorted.
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
- Kadane's algorithm runs on the array [-9, 3, 3, -2, 6, 4, -4, 6, 9] to find the maximum sum of a contiguous subarray. What sum does it…
- Elevation map [4, 6, 0, 2, 6, 6, 6, 2] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 2, 0, 0, 2, 1, 0, 2, 0] (values 0, 1, 2 only)…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [3, 4, 4, 4, 4, 1, 3, 4, 4]. What candidate and…