GATE Computer ScienceHardGATE CS (PYQ pattern)
A routine rev(k) reverses every block of k consecutive nodes of a singly linked list in place, leaving a final block of fewer than k nodes untouched. The list 1 -> 2 -> ... -> 11 is processed by rev(3) and then by rev(4). Which nodes are now at positions 4 and 10 (1-indexed)?
- A. 7 and 10
- B. 3 and 10
- C. 3 and 11
- D. 6 and 10
Correct answer: B. 3 and 10
rev(3) gives 3 2 1 6 5 4 9 8 7 10 11 (the tail 10 11 is shorter than 3, so it stays). rev(4) then reverses positions 1-4 and 5-8 and leaves the 3-node tail: 6 1 2 3 8 9 4 5 7 10 11. Positions 4 and 10 hold 3 and 10. Doing rev(4) first gives a different list, and reversing the short tail too changes position 10.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A queue is built from two stacks: enqueue pushes onto stack IN; dequeue pops from OUT, and if OUT is empty it first moves every element of…
- A BST is built by inserting 50, 30, 70, 20, 40, 60, 80 in some order into an initially empty tree. The final tree is the perfectly…
- Insert 45, 25, 65, 15, 35, 55, 75, 10, 20, 30, 40, 50, 60 into an empty BST. Then delete 25, 45 and 65 in that order, replacing a node…
- A full binary tree (every node has 0 or 2 children) has preorder 1 2 4 5 8 9 3 6 10 11 7 and postorder 4 8 9 5 2 10 11 6 7 3 1. What are…
- Binary trees (unlabelled, left and right children distinguished) with exactly 6 nodes: how many have height exactly 3 (a single node has…
- Height is counted in edges (a single node has height 0). What is the maximum possible height of an AVL tree with 50 nodes, and what is the…