Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

UNIT-1 GATE BITS

1) Consider the following C function in which size is the number of elements in the array E:
The value returned by the function MyX is the
int MyX(int *E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;

for (i = 0; i < size; i++)


Y = Y + E[i];

for (i = 0; i < size; i++)


for (j = i; j < size; j++)
{
Z = 0;
for (k = i; k <= j; k++)
Z = Z + E[k];
if (Z > Y)
Y = Z;
}
return Y;
}
(A) maximum possible sum of elements in any sub-array of array E.
(B) maximum element in any sub-array of array E.
(C) sum of the maximum elements in all possible sub-arrays of array E
(D) the sum of all the elements in the array E.

Answer: (A)

Explanation: The function does following


Y is used to store maximum sum seen so far and Z is used to store current sum
1) Initialize Y as sum of all elements
2) For every element, calculate sum of all subarrays starting with arr[i]. Store the current sum in Z. If Z is greater
than Y, then update Y.

2) What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first
parameter is passed by reference, whereas the second parameter is passed by value.

int f(int &x, int c) {


c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}

(A) 3024
(B) 6561
(C) 55440
(D) 161051
Answer (B)

Explanation: Since c is passed by value and x is passed by reference, all functions will have same copy of x, but
different copies of c.
f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4
Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes
9^4 which is 6561.
3) Consider the following function

int unknown(int n) {
int i, j, k = 0;
for (i = n/2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n/2;
return k;
}

What is the returned value of the above function?


(A) Θ(n^2)
(B) Θ(n^2Logn)
(C) Θ(n^3)
(D) Θ(n^3Logn)

Answer (B)

Explanation: The outer loop runs n/2 or Θ(n) times. The inner loop runs Θ(Logn) times (Note that j is divide by 2
in every iteration). So the statement "k = k + n/2;" runs Θ(nLogn) times. The statement increases value of k by n/2.
So the value of k becomes n/2*Θ(nLogn) which is Θ(n^2Logn)

4) Which one of the following correctly determines the solution of the recurrence relation with T(1) = 1?

T(n) = 2T(n/2) + Logn

(A) Θ(n)
(B) Θ(nLogn)
(C) Θ(n*n)
(D) Θ(log n)
Answer: (A)
Explanation: This can be solved using Master Method. It falls in case 1.

5. In a circular linked list organization, insertion of a record involves modification of

A. One pointer

B. Two pointers
C. Three pointers
D. No pointer
Answer: B
Suppose we want to insert node A to which we have pointer p , after pointer q then we will Have following pointer
operations
1.p->next=q->next;
2.q->next = p;
So we have to do two pointer modifications

6. Consider a singly linked list having n nodes. The data items d1, d2, …., dn are stored in the n nodes. Let Y
be a pointer to the jth node (1 ≤ j ≤ n) in which dj is stored. A new data item d stored in a node with address
Y is to be inserted. Give an algorithm to insert d into the list to obtain a list having items d1, d2, ……, dj-1,
d, dj, …..dn in that order without using the header.

Answer:
Algorithm 1 insert_mid( )
1: create newnode
2: newnode->next=y->next
3: newnode->data=y->data /*which is dj*/
4: y->next=newnode
5: y->data=d
Explanation:
Since we didn’t have the address of node which is previous to Y.
So insert a new node after Y.
And copy the data of Y to new node and modify data field of Y to d.
Hence we get required sequence of data as d1,d2,…,dj-1,d,dj,…dn

7. Linked lists are not suitable for data structures for which one of the following problems?

(A) Insertion sort

(B) Binary search


(C) Radix sort
(D) Polynomial manipulation

Answer: B
Explanation: For binary search, if we are using array, then we can go to middle of array by just dividing index
of array by 2. Since array is stored in contiguous memory. But that is not true in case of linked list. If you want
to access middle of list then each time you have to traverse from its head. Hence use of linked list is not good
idea for binary search.

8.The concatenation of two lists is to be performed in O(1) time. Which of the following implementations of
a list

should be used?

(A) singly linked list (B) doubly linked list

(C) circular doubly linked list (D) array implementation of list


Answer:[C]
Explanation: For merging of list you have to point next pointer of last node of first list to first node of 2 nd list. To
do this in O(1) time circular double list is useful. You can go to last node 1 st list by head1->next->previous. And
modify this field pointing to head2->next. And also modify head2->next->previous to head1->next.

9.Let p be a pointer as shown in the figure in a singly linked list.


What do the following assignment statements achieve?
q: = p→ next
p → next: = q→ next
q → next: = (q → next) → next
(p → next) →next: = q
Answer:
q: = p→ next cell i->cell (i+1) ->cell(i+2)->cell(i+3) p->cell i,q->cell(i+1)
p → next: = q→ next cell i->cell(i+2)->cell(i+3) & cell(i+1) ->cell(i+2)->cell(i+3)
q → next: = (q → next) → next cell i->cell(i+2)->cell(i+3) & cell(i+1)->cell(i+3)
(p → next) →next: = q cell->i->cell(i+2)->cell(i+1)->cell(i+3)

Write a constant time algorithm to insert a node with data D just before the node with address p of a singly
linked list.
Constant time algorithm is
Insert before(p)
{
n =newnode(); n->next
= p->next; p->next =n
n->data=p->data; p-
>data = D;
}
As we can’t actually insert before any node to with we have a pointer in singly linked list.
So idea is to insert a node after p , copy the data of p in new node and copy new data in node p.

10.In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element
is

A. log2n
B. n/2
C. log2n – 1
D. n
Answer: D
Explanation: In worst case the element is in last node. So we require n comparisons in worst case.

11.In a circular linked list organization, insertion of a record involves modification of


A. One pointer
B. Two pointers
C. Three pointers
D. No pointer
Answer: [B]
Explanation: Suppose we want to insert node A to which we have pointer p , after pointer
q then we will Have following pointer operations
1.p->next=q->next;
2.q->next = p;
So we have to do two pointer modifications

12.Consider a singly linked list having n nodes. The data items d1, d2, …., dn are stored in the n nodes. Let Y be a
pointer to the jth node (1 ≤ j ≤ n) in which dj is stored. A new data item d stored in a node with address Y is to be
inserted. Give an algorithm to insert d into the list to obtain a list having items d 1, d2, ……, dj-1, d, dj, …..dn in that
order without using the header.

Answer:
Algorithm 1 insert_mid( )
1: create newnode
2: newnode->next=y->next
3: newnode->data=y->data /*which is dj*/
4: y->next=newnode
5: y->data=d
Explanation:
Since we didn’t have the address of node which is previous to Y.
So insert a new node after Y.
And copy the data of Y to new node and modify data field of Y to d.
Hence we get required sequence of data as d1,d2,…,dj-1,d,dj,…dn

13. The following C function takes a singly-linked list of integers as a parameter and rearranges the elements
of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What
will be the contents of the list after the function completes execution?
struct node {
int value;
struct node *next;
};
void rearrange (struct node *list) {
struct node *p, *q;
int temp;
if (!list || !list -> next) return; p =
list; q = list -> next; while (q) {
temp = p -> value; p -> value =q -> value; q -
> value = temp; p = q -> next;
q = p ? -> next : 0;
}
}
(A) 1,2,3,4,5,6,7
(B) 2,1,4,3,6,5,7
(C) 1,3,2,5,4,7,6
(D) 2,3,4,5,6,7,1
Answer: B
Explanation: The q pointer always point to next node of p. And here p is modified first. So swapping is done

14. The data blocks of a very large file in the Unix file system are allocated using

(A) contiguous allocation

(B) linked allocation


(C) indexed allocation
(D) an extension of indexed allocation
Answer: D
Explanation: The file’s inode contains pointer to first 10 data blocks.The 11th pointer in inode points at an indirect
block that contain 128 data blocks. And so on…

15.The following C function takes a singly linked list of integers as a parameter and rearranges the elements of
the list. The list is represented as pointer to structure. The function is called with the list containing integers 1,
2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes?
struct node {int value; struct node *next;};
void rearrange(struct node *list) {
struct node *p, *q;
int temp;
if(!list || !list → next) return; p =
list; q = list → next; while(q) {
temp = p → value;
p → value = q → value; q →
value = temp;

p = q → next;
q = p? p → next : 0;
}
}

(B) 2, 1, 4, 3, 6, 5,
(A) 1, 2, 3, 4, 5, 6, 7 7
(D) 2, 3, 4, 5, 6, 7,
(C) 1, 3, 2, 5, 4, 7, 6 1
Answer: B
Explanation: The q pointer always point to next node of p. And here p is modified first. So swapping is done only
once for each

16.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?

(A) O(n)
(B) O(log2 n)
(C) O(log n)
(D) O(1)
Answer: A
Explanation: As Q is pointing to node X.
So the following algorithm will delete the node in O(1)
Algorithm-:
Delete()
Step1. Q->data =: (Q->next)->data
Step2. temp =: Q->next (temp is temporary pointer variable of type list )
Step3. Q->next =: (Q->next)->next
Step4. delete temp

17.Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations
among union, intersection, membership, and cardinality will be the slowest?
(A) Union only
(B) intersection, membership
(C) membership, cardinality
(D) union, intersection
Answer: D
Explanation: For intersection n*n comparisons are required.
For union, just merging of two list will not work. We have to find out common elements which require again
n*n comparisons.
For membership only n comparisons are needed.
For Cardinality n comparisons. (ie. For each node check next !=NULL and increment )

18. 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?
(A) Rear node
(B) Front node
(C) Not possible with a single pointer
(D) Node next to front
Answer: A
Explanation: p points to rear node
For enQueue
1: create newnode
2: newnode->next=p->next /*which is front node*/
3: p->next=newnode
4: /*rear=newnode;*/
5: p=rear
For deQueue
1:temp=p->next /*temp is pointing to front node
2: p->next=p->next->next
3:/* front=p->next*/
4:delete(temp)

19. Consider the function f defined below.


struct item
{
int data;
struct item * next;
};
int f(struct item *p)
{
return (
(p == NULL) ||
(p->next == NULL) ||
(( P->data &lt;= p->next->data) &amp;&amp; f(p->next))
);
}
For a given linked list p, the function f returns 1 if and only if (GATE CS 2003)
a) the list is empty or has exactly one element
b) the elements in the list are sorted in non-decreasing order of data value
c) the elements in the list are sorted in non-increasing order of data value
d) not all elements in the list have the same data value.
Answer (b)
Explanation:
The function f() works as follows
1) If linked list is empty return 1
2) Else If linked list has only one element return 1
3) Else if node->data is smaller than equal to node->next->data and same thing holds for rest of the list then return 1
4) Else return 0
20. 1. Consider the following C program segment
struct CellNode
{
struct CelINode *leftchild;
int element;
struct CelINode *rightChild;
}
int Dosomething(struct CelINode *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 is (GATE CS 2004)
a) The number of leaf nodes in the tree
b) The number of nodes in the tree
c) The number of internal nodes in the tree
d) The height of the tree
Answer: (d)
Explanation: DoSomething() returns max(height of left child + 1, height of left child + 1). So given that pointer to
root of tree is passed to DoSomething(), it will return height of the tree. Note that this implementation follows the
convention where height of a single node is 0.

You might also like