Data Structures & AlgorithmsHardProduct-company MCQ pattern
Two sorted linked lists [1 → 10 → 11 → 19 → 28] and [12 → 14 → 18 → 27 → 35] 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. 35 → 28 → 27 → 19 → 18 → 14 → 12 → 11 → 10 → 1
- B. 1 → 10 → 11 → 12 → 14 → 18 → 19 → 27 → 28 → 35
- C. 1 → 10 → 11 → 19 → 28 → 12 → 14 → 18 → 27 → 35
- D. 1 → 12 → 10 → 14 → 11 → 18 → 19 → 27 → 28 → 35
Correct answer: B. 1 → 10 → 11 → 12 → 14 → 18 → 19 → 27 → 28 → 35
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 [1 → 10 → 11 → 12 → 14 → 18 → 19 → 27 → 28 → 35] in one linear pass. Simply concatenating the two lists, [1 → 10 → 11 → 19 → 28 → 12 → 14 → 18 → 27 → 35], 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
- Two sorted linked lists [3 → 14 → 35 → 39] and [9 → 20 → 21 → 27 → 34] are merged with the standard O(n+m) two-pointer merge (always take…
- Floyd's cycle detection runs on a linked list 1 → 2 → ... → 7 whose last node's next pointer points back to node 5 (0-based position 4)…
- Heights [1, 6, 2, 6, 2, 5, 2, 2, 7] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Elevation map [4, 5, 4, 4, 0, 1, 2, 1, 0, 2] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(3), enqueue(6), enqueue(4), enqueue(7), dequeue()…