GATE 2004
199 questions
MCQ 1991-mark 199
Questions 101–105 of 199
Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-case time complexity of the best-known algorithm to delete the node x from the list ?
Answer: (A)
To delete the node x that Q points to, the node before x must have its next pointer changed to the node after x. In a singly linked list there is no pointer back, so we have to find that node by walking from the head.
Steps:
Note: we can imitate a deletion by copying the data of x's next node into x and then removing that next node in O(1). This does not delete the node x itself (the node x stays in the list and a different node is freed), so it is not counted here.
Answer: (A)
Steps:
- Find the node whose next points to ; call it . This takes O(n).
- Find the node that points to; call it . This takes O(1).
- free(Q)
Note: we can imitate a deletion by copying the data of x's next node into x and then removing that next node in O(1). This does not delete the node x itself (the node x stays in the list and a different node is freed), so it is not counted here.
Answer: (A)
Suppose each set is represented as a linked list with elements in arbitray order. Which of the operations among union, intersection, membership, cardinality will be the slowest?
Answer: (D)
Let the two sets be lists of sizes and , with elements in any order.
- Membership: scan one list until the element is found, so for that list.
- Cardinality: count the nodes of one list, so for that list.
- Union: no duplicates may remain, so each element of one set must be searched for in the other: .
- Intersection: each element of set 1 must be searched for in set 2: .
Union and intersection take product time, while membership and cardinality take linear time. So union and intersection are the slowest.
Answer: (D)
- Membership: scan one list until the element is found, so for that list.
- Cardinality: count the nodes of one list, so for that list.
- Union: no duplicates may remain, so each element of one set must be searched for in the other: .
- Intersection: each element of set 1 must be searched for in set 2: .
Union and intersection take product time, while membership and cardinality take linear time. So union and intersection are the slowest.
Answer: (D)
A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time?
Answer: (A)
p should point to the rear node. In a circular list, is the front node, so one pointer reaches both ends in O(1).
enQueue: put the new node after the rear and make it the new rear.
If p pointed to the front node, both operations would fail to be O(1). enQueue must change the rear node's next pointer, and deQueue must change the rear node's next pointer to the new front. The rear can only be reached by walking the whole list, which is O(n). Option (D), the node next to front, has the same problem: the rear is not reachable in O(1).
Answer: (A)
enQueue: put the new node after the rear and make it the new rear.
newNode->next = p->next; // new node points to the front p->next = newNode; // old rear points to the new node p = newNode; // new node is the reardeQueue: remove the front node, so the second node becomes the front.
struct node *temp = p->next; // the front node p->next = temp->next; // rear now points to the second node free(temp);Both take a constant number of steps. (Two special cases: enQueue on an empty queue sets and ; deQueue on a one-node queue frees that node and sets p to NULL.) The extra pointers newNode and temp are only temporary; the queue itself is accessed through p alone.
If p pointed to the front node, both operations would fail to be O(1). enQueue must change the rear node's next pointer, and deQueue must change the rear node's next pointer to the new front. The rear can only be reached by walking the whole list, which is O(n). Option (D), the node next to front, has the same problem: the rear is not reachable in O(1).
Answer: (A)
Which one of the following binary trees has its inorder and preorder traversals as BCAD and ABCD, respectively?
Answer: (D)
Preorder is node, left, right. Inorder is left, node, right.
The first letter of the preorder ABCD is the root, so the root is A.
In the inorder BCAD, the letters before A are in the left subtree {B, C}, and the letters after A are in the right subtree {D}.
Preorder of the left subtree is B, C, so B is its root. Inorder of the left subtree is B, C, so C comes after B and is B's right child.
So the tree is: A with left child B (B has right child C) and right child D. This is figure (D).
Check the others: (A) has inorder B A D C. (B) has preorder A C B D. (C) has A as root with right child B, and its inorder is A C B D. None of these match.
Answer: (D)
The first letter of the preorder ABCD is the root, so the root is A.
In the inorder BCAD, the letters before A are in the left subtree {B, C}, and the letters after A are in the right subtree {D}.
Preorder of the left subtree is B, C, so B is its root. Inorder of the left subtree is B, C, so C comes after B and is B's right child.
So the tree is: A with left child B (B has right child C) and right child D. This is figure (D).
Check the others: (A) has inorder B A D C. (B) has preorder A C B D. (C) has A as root with right child B, and its inorder is A C B D. None of these match.
Answer: (D)
Consider the following C program segment
struct CellNode
{
struct CellNode *leftChild;
int element;
struct CellNode *rightChild;
}
int Dosomething(struct CellNode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if (ptr->leftChild != NULL)
value = 1 + DoSomething(ptr->leftChild);
if (ptr->rightChild != NULL)
value = max(value, 1 + DoSomething(ptr->rightChild));
}
return (value);
} The value returned by the function DoSomething when a pointer to the root of a non-empty tree is passed as argument isAnswer: (D)
For a node, the function starts with .
If the node has a left child, it sets . If it has a right child, it sets .
So a leaf returns 0, and any other node returns 1 plus the larger of its children's results. This is the length of the longest path from the node down to a leaf, counted in edges, which is the height of the tree.
Check with a complete tree of 7 nodes: it returns 2. It has 4 leaves, 3 internal nodes and 7 nodes, so the result matches none of options (A), (B) and (C).
(The paper names the function Dosomething in its definition and DoSomething in the calls; both mean the same function. The struct declaration is also printed without its closing semicolon.)
Answer: (D)
If the node has a left child, it sets . If it has a right child, it sets .
So a leaf returns 0, and any other node returns 1 plus the larger of its children's results. This is the length of the longest path from the node down to a leaf, counted in edges, which is the height of the tree.
Check with a complete tree of 7 nodes: it returns 2. It has 4 leaves, 3 internal nodes and 7 nodes, so the result matches none of options (A), (B) and (C).
(The paper names the function Dosomething in its definition and DoSomething in the calls; both mean the same function. The struct declaration is also printed without its closing semicolon.)
Answer: (D)