Data Structures & AlgorithmsVery hardProduct company OA
Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 1 positions using the reversal algorithm (reverse first d, reverse rest, reverse whole). What is the result?
- A. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- B. 10, 1, 2, 3, 4, 5, 6, 7, 8, 9
- C. 2, 3, 4, 5, 6, 7, 8, 9, 10, 1
- D. 3, 4, 5, 6, 7, 8, 9, 10, 1, 2
Correct answer: C. 2, 3, 4, 5, 6, 7, 8, 9, 10, 1
Left-rotating by d moves the first d elements to the end in order: reversing the first d elements, then the remaining n−d, then the whole array achieves this in O(n) time with O(1) extra space, giving [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]. Rotating right by d instead would give [10, 1, 2, 3, 4, 5, 6, 7, 8, 9].
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 recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(7)?
- Kadane's algorithm runs on the array [-4, 3, 9, 3, 3, -6, 7, 4, 6] to find the maximum sum of a contiguous subarray. What sum does it…
- Binary search for first/last occurrence runs on the sorted array [2, 5, 6, 16, 19, 19, 20, 21] for target 19. What (first index, last…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 3 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- Two sorted linked lists [8 → 33 → 35 → 40] and [2 → 13 → 16] are merged with the standard O(n+m) two-pointer merge (always take the…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(2), enqueue(6), dequeue(), dequeue(), enqueue(1)…