Data Structures & AlgorithmsVery hardHCLTech · Wipro pattern
Array [1, 2, 3, 4, 5, 6, 7, 8] is left-rotated by d = 7 positions using the reversal algorithm (reverse first d, reverse rest, reverse whole). What is the result?
- A. 1, 2, 3, 4, 5, 6, 7, 8
- B. 8, 7, 6, 5, 4, 3, 2, 1
- C. 8, 1, 2, 3, 4, 5, 6, 7
- D. 2, 3, 4, 5, 6, 7, 8, 1
Correct answer: C. 8, 1, 2, 3, 4, 5, 6, 7
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 [8, 1, 2, 3, 4, 5, 6, 7]. Rotating right by d instead would give [2, 3, 4, 5, 6, 7, 8, 1].
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
- What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BABAACBCCA" are anagrams of "AB"?
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(7), enqueue(2), dequeue(), enqueue(5), dequeue()…
- What is the postfix (Reverse Polish) form of the infix expression "f + d h -(i + e)", built with the standard stack-based conversion (^…
- Keys 88, 61, 14, 52, 18 are inserted via linear probing (h(k) = k mod 7, probe by +1 on collision) into an initially empty table of size 7…
- What is the postfix (Reverse Polish) form of the infix expression "j + g h -(i + c)", built with the standard stack-based conversion (^…