CS 18F DS Mid1+Solution
CS 18F DS Mid1+Solution
a. 1^2*3-4+5/6/7+8
b. 1^23-4+5/6/(7+8)
c. 1^2*3-4+5/6/(7+8)
d. ((((1^2)*3)-4)+((5/6)/7+8))
a. a b c d - * + e - f g h / + /
b. a b c d - * e f g h / + / – +
c. a b c d - * + e f g h / + / –
d. None of above
Page 2 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
3) Recall algorithm to convert infix expression (with parenthesis) into postfix. Given the following
infix expression: A + B * ( C / D / E ). What is the state of the opstk (i.e., operators on
the stack) before reading the token/symbol E from the infix expression? [6 marks]
a. Opstk: + * (/ /
b. Opstk: + * ( /
c. Opstk: + * /
d. Opstk: + * / /
Page 3 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
4) Recall algorithm to convert infix expression into prefix (Hint: Using reversal of infix expression).
Which of the following infix expressions will result in the violation of associativity property. [2
marks]
a. a – (b + c) * d
b. a – b + (c * d)
c. (a – b ) + c * d
d. None of the above
e. All of the above
5) The array-based Queue throws an exception when the array’s capacity has been reached. Consider
the following alternative: Create a larger array, using the resize method. The cost of a resize that
makes the array larger is proportional to the new size. Suppose we expand the array’s capacity by
one element. What will be the running time (Big-Oh notation) in the worst-case for a sequence of
N insertions? [ 1 mark]
a. O(1)
b. O(N)
c. O(N2)
d. O (log2 N)
e. None of above
6) The array-based Queue throws an exception when the array’s capacity has been reached. Consider
the following alternative: Create a larger array, using the resize method. The cost of a resize that
makes the array larger is proportional to the new size. Suppose we double the array’s capacity
(assume the capacity is not zero). What will be the running time (Big-Oh notation) in the worst-
case for a sequence of N insertions? [1 mark]
a. O(1)
b. O(N)
Page 4 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
c. O(N2)
d. O (log2 N)
e. None of above
7) A stack bStack contains the following items: 7, 8, -3, 14, 5 (item 7 is at the top of the
stack). What is the output of the following program? [3 marks]
int x;
while (!bStack.isEmpty()){
bStack.pop(x);
if (x>0 && !bStack.isEmpty())
bStack.pop();
cout << x << endl;
}
a. 7, 8, -3, 14
b. 7, -3, 14, 5
c. 7, 14, 5
d. 7, -3, 14
Page 5 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
8) Given a linked list with the following items: 1 5 6 7 NULL. What will be the
output of the following function? [8 marks]
first = *headRef;
rest = first->next;
first->next->next = first;
first->next = NULL; // (tricky step -- make a drawing)
*headRef = rest;
}
void main (){
. . .
Function1 (&head);
. . .
}
Page 6 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
12: }
13: void main (){
14: . . .
15: Function2 (&head);
16: . . .
17: }
Page 8 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
10) Complete the following function that remove duplicates from a sorted list (e.g., in linked list
12235 duplicate 2 is removed and the resultant list will be 1235). [8 marks]
07:
08:
09: }
10: else {
11: current = current->next;
12: }
13: }
14: }
15:
16: void main (){
17: . . .
18: RemoveDuplicates(head);
19: . . .
20: }
Page 9 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Space for rough work:
11) Which of the following operations is performed more efficiently by doubly linked list than by
singly linked list? [1 mark]
Page 10 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
12) Suppose in a linked list address of first node is 1020 and node size is 4, what will be the address of
node at 5th position? [1 mark]
a. 1022
b. 1025
c. 1040
d. 1024
e. None of the above
13) Suppose a circular queue of capacity (n-1) elements is implemented with an array of n elements.
Assume that the insertion and deletion operations are carried out using REAR and FRONT as array
index variables, respectively. Initially REAR=FRONT=0. The conditions to detect queue full and
queue is empty are? [1 mark]
14) How many minimum queues are needed to implement a stack? Consider the situation where no
other data structure like arrays, linked list is available to you. [1 mark]
a. 1
b. 2
c. 3
d. 4
e. Not Possible
15) How many minimum stacks are needed to implement a queue. Consider the situation where no
other data structure like arrays, linked list is available to you. [1 mark]
a. 1
b. 2
c. 3
d. 4
e. Not Possible
Page 11 of 13
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
16) Complete the following code snippet to implement enqueue and dequeue operations of Queue
using a user stack and function call stack. [12 marks]
05:
07: }
14: s.pop(x);
15: if (_______________________________)
16: return x;
17: int item = deQueue();
18:
Page 13 of 13