GATE Computer ScienceHardGATE CS (PYQ pattern)
rot(a, n, k) does: reverse(a, 0, k-1); reverse(a, k, n-1); reverse(a, 0, n-1), where reverse(a, i, j) reverses a[i..j] in place. The array a = {1, 2, ..., 10} is passed to rot(a, 10, 7) and then to rot(a, 10, 6). What are a[0] and a[9] at the end?
- A. 8 and 7
- B. 4 and 3
- C. 1 and 10
- D. 5 and 4
Correct answer: B. 4 and 3
Reversing the two parts and then the whole array is a left rotation by k: for [1..7 | 8..10], reversing gives 7..1, 10 9 8, and the full reversal gives 8 9 10 1 ... 7. Two calls rotate left by 7 + 6 = 13, i.e. by 3 positions mod 10, giving 4 5 6 7 8 9 10 1 2 3: a[0] = 4, a[9] = 3. Reading it as a right rotation gives 8 and 7.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int x = 10; void f() { static int x = 1; x += 2; { int x = 100; x++; } printf("%d ", x); } int main() { f(); f(); { extern int x…
- unsigned char rev(unsigned char x) { unsigned char r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } return r; }…
- Assume 4-byte int and 8-byte pointers. char s[] = "ab0cd"; char p = "ab0cd"; int a[3][4]; printf("%zu %zu %zu %zu %zu", sizeof(s)…
- For a BST built from distinct keys (deletion of a node with two children uses its inorder successor), consider: I. Inserting a new key and…
- A directed graph has adjacency lists (visited in the order listed): A: B, C; B: D, E; C: E, F; D: G; E: D, H; F: H, B; G: none; H: G, A…
- A DAG on vertices 1..7 has edges 1->2, 1->3, 2->4, 3->4, 3->5, 4->6, 5->6, 7->5. How many distinct topological orderings does it have?