Data Structures & AlgorithmsHardProduct company OA
A singly linked list holds 10 → 21 → 32 → 43 → 54 → 65. It is rearranged so that all nodes at ODD positions (1st, 3rd, …) come first, followed by the nodes at even positions, each group keeping its relative order. What is the result?
- A. 21 → 43 → 65 → 10 → 32 → 54
- B. 10 → 32 → 54 → 65 → 43 → 21
- C. 10 → 32 → 54 → 21 → 43 → 65
- D. 10 → 21 → 32 → 43 → 54 → 65
Correct answer: C. 10 → 32 → 54 → 21 → 43 → 65
Positions, not values, decide: positions 1, 3, 5 hold 10, 32, 54 and positions 2, 4, 6 hold 21, 43, 65. Splitting by ODD VALUES (21, 43, 65 first) is the trap; the result is built by relinking two chains in one pass with O(1) extra space.
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 juggling algorithm rotates an array of n = 12 elements left by k = 8 positions by following cycles of the form i → i + k (mod n). How…
- Interpolation search (pos = lo + (key − a[lo]) · (hi − lo) / (a[hi] − a[lo]), integer division) looks for 32 in [1, 2, 4, 8, 16, 32, 64…
- What is the sum of min(subarray) over all contiguous subarrays of [2, 5, 1, 4, 3]? (A monotonic stack counts, for each element, how many…
- What is the length of the longest substring of ')()())(()())' that is a valid (balanced) parentheses string?
- The stock span of day i is the number of consecutive days ending at day i whose price is ≤ the price on day i. For prices [31, 27, 14, 21…
- What is the minimum number of swaps (of any two elements) needed to sort the permutation [4, 3, 1, 2, 6, 5] into ascending order?