Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
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 the smaller head, splicing nodes, no new array). What is the merged list?
- A. 3 → 9 → 14 → 20 → 35 → 21 → 39 → 27 → 34
- B. 39 → 35 → 34 → 27 → 21 → 20 → 14 → 9 → 3
- C. 3 → 14 → 35 → 39 → 9 → 20 → 21 → 27 → 34
- D. 3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39
Correct answer: D. 3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39
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 [3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39] in one linear pass. Simply concatenating the two lists, [3 → 14 → 35 → 39 → 9 → 20 → 21 → 27 → 34], 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
- 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()…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [7, 5, 2, 5, 5, 5, 2, 7]. What candidate and…