Data Structures & AlgorithmsHardProduct company OA · Infosys pattern
The list 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 is reversed in groups of k = 3 nodes; a final group with fewer than k nodes is left as it is. What is the result?
- A. 3 → 2 → 1 → 6 → 5 → 4 → 8 → 7
- B. 3 → 2 → 1 → 6 → 5 → 4 → 7 → 8
- C. 8 → 7 → 6 → 5 → 4 → 3 → 2 → 1
- D. 1 → 2 → 3 → 6 → 5 → 4 → 7 → 8
Correct answer: B. 3 → 2 → 1 → 6 → 5 → 4 → 7 → 8
Groups: (1 2 3) → 3 2 1, (4 5 6) → 6 5 4, and the leftover (7 8) has only 2 nodes, so it stays 7 → 8. '… 8 → 7' is the variant that also reverses a short final group, which the question rules out.
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
- 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…
- 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…