Data Structures & AlgorithmsVery hardProduct company OA
Two sorted linked lists [12 → 17 → 24 → 26 → 32] and [8 → 10 → 12 → 38] 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. 12 → 17 → 24 → 26 → 32 → 8 → 10 → 12 → 38
- B. 38 → 32 → 26 → 24 → 17 → 12 → 12 → 10 → 8
- C. 8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38
- D. 12 → 8 → 17 → 10 → 24 → 12 → 26 → 38 → 32
Correct answer: C. 8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38
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 [8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38] in one linear pass. Simply concatenating the two lists, [12 → 17 → 24 → 26 → 32 → 8 → 10 → 12 → 38], 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
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 1, 2, 2, 1, 1, 1, 1, 2, 0] (values 0, 1, 2…
- Heights [8, 6, 3, 2, 6, 8, 8, 4, 7, 1] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Kadane's algorithm runs on the array [5, 4, 4, -3, -5, -1, 8] to find the maximum sum of a contiguous subarray. What sum does it return?
- What is the sum of the minimum element over every contiguous subarray of [9, 4, 5, 3, 1, 9, 8, 1]?
- QuickSort (last element as pivot, Lomuto-style partition) sorts [31, 34, 38, 59, 23, 16, 54, 42, 46]. Counting one comparison per element…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 2 positions using the reversal algorithm (reverse first d, reverse rest…