Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
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 whole). What is the result?
- A. 7, 8, 9, 1, 2, 3, 4, 5, 6
- B. 9, 8, 7, 6, 5, 4, 3, 2, 1
- C. 5, 6, 7, 8, 9, 1, 2, 3, 4
- D. 4, 5, 6, 7, 8, 9, 1, 2, 3
Correct answer: D. 4, 5, 6, 7, 8, 9, 1, 2, 3
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 [4, 5, 6, 7, 8, 9, 1, 2, 3]. Rotating right by d instead would give [7, 8, 9, 1, 2, 3, 4, 5, 6].
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
- 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)…
- Keys 13, 28, 91, 42, 33 are inserted via linear probing (h(k) = k mod 7, probe by +1 on collision) into an initially empty table of size 7…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…