Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
The reversal algorithm rotates an array LEFT by k: reverse the first k elements, reverse the rest, then reverse the whole array. What is [1, 2, 3, 4, 5, 6, 7] after this with k = 3?
- A. 3 2 1 7 6 5 4
- B. 7 6 5 4 3 2 1
- C. 4 5 6 7 1 2 3
- D. 5 6 7 1 2 3 4
Correct answer: C. 4 5 6 7 1 2 3
Step 1: 3 2 1 4 5 6 7; step 2: 3 2 1 7 6 5 4; step 3: 4 5 6 7 1 2 3, a left rotation by 3. 5 6 7 1 2 3 4 is a right rotation by 3 (left by 4).
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
- Which statement about a doubly linked list compared with a singly linked list is FALSE?
- How many of these statements are correct? (i) Merge sort is stable. (ii) Heap sort runs in O(n log n) even in the worst case. (iii) Quick…
- What is the time complexity of this code fragment? c int count = 0; for (int i = 1; i < n; i = 2) for (int j = 0; j < i; j++) count++;
- What is the time complexity of this code fragment? c for (int i = 1; i <= n; i++) for (int j = i; j <= n; j += i) work(); // O(1)
- What value of count does this code print for n = 16? c int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count++…
- What does this code print? c int c = 0, n = 10; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) for (int k = j + 1; k < n…