Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] 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. 7 → 13 → 23 → 40 → 5 → 6 → 9 → 18 → 25
- B. 5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40
- C. 7 → 5 → 13 → 6 → 23 → 9 → 40 → 18 → 25
- D. 40 → 25 → 23 → 18 → 13 → 9 → 7 → 6 → 5
Correct answer: B. 5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40
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 [5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40] in one linear pass. Simply concatenating the two lists, [7 → 13 → 23 → 40 → 5 → 6 → 9 → 18 → 25], 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 [2, 0, 2, 2, 1, 0, 1, 1] (values 0, 1, 2 only) in…
- Array [85, 11, 19, 30, 40, 43, 70] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-2)…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 6 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- An LRU cache of capacity 3 runs this sequence of operations in order: put(4, 40), put(2, 20), put(5, 50), put(1, 10), put(1, 10), put(2…
- What is the postfix (Reverse Polish) form of the infix expression "(i + j) d - b / h", built with the standard stack-based conversion (^…