GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Keys 5, 3, 8, 3, 9, 1, 8, 5 are inserted one by one into an initially empty sorted singly linked list; each insertion scans from the head and stops at the first node whose value is not less than the new key (the node is inserted before it). The comparison of a key with the list's node values counts one per node examined, including the one that stops the scan. How many comparisons are made in total?
- A. 18
- B. 20
- C. 15
- D. 23
Correct answer: A. 18
For each insertion count the nodes with smaller value plus one stopping node (if any exists). Summing over the 8 insertions gives 18.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Two sorted lists, X with 5 keys and Y with 7 keys (all 12 keys distinct), are merged with the standard two-pointer merge that stops…
- A singly linked list 10 -> 11 -> ... -> 19 is rearranged by a routine that places all nodes at odd positions (1st, 3rd, ...) first…
- A circular singly linked list holds nodes 1 to 100 in order. Deletion starts with node 1 itself and then removes every second remaining…
- struct T { int k; struct T l, r; }; int f(struct T n) { return n ? 1 + max(f(n->l), f(n->r)) : 0; } int g(struct T n) { return n ?…
- Keys 30, 10, 40, 5, 20, 35, 50, 15, 25, 45 are inserted in this order into an empty BST, and then the left and right pointers of every…
- A complete binary tree with 20 nodes is stored in A[1..20] with A[i] = i, the children of i at 2i and 2i + 1. The routine v(i) does: if i…