Data Structures & AlgorithmsModerateCapgemini · Arrays
The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated left by 3 positions using three reversals: reverse the first 3 elements, reverse the remaining 5, then reverse the whole array. What is the result?
- A. [6, 7, 8, 1, 2, 3, 4, 5]
- B. [3, 2, 1, 8, 7, 6, 5, 4]
- C. [4, 5, 6, 7, 8, 1, 2, 3]
- D. [4, 5, 6, 7, 8, 3, 2, 1]
Correct answer: C. [4, 5, 6, 7, 8, 1, 2, 3]
Reversing the parts gives [3,2,1,8,7,6,5,4]; reversing the whole array then yields [4,5,6,7,8,1,2,3].
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
- Keys 12, 22, 32, 13, 23 are inserted in this order into an empty hash table of size 10 using h(k) = k mod 10 and linear probing. At which…
- A queue is built from two stacks: enqueue pushes on stack IN; dequeue pops from OUT, first moving all of IN onto OUT (one pop and one push…
- What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] in ascending order?
- For each element of [4, 5, 2, 25, 7, 8], take the first strictly greater element to its right (or -1 if there is none). What is the sum of…
- A sliding window of size 3 moves over [1, 3, -1, -3, 5, 3, 6, 7]. What is the sum of the window maximums?
- What is the length of the longest substring of "abcabcbbxyzab" with no repeated character?