0% found this document useful (0 votes)
36 views6 pages

Minor 1 Solution PDF

This document provides instructions and questions for a closed book exam. It consists of 5 questions worth a total of 35 marks. The questions assess understanding of time complexity analysis, data structures like stacks, queues, linked lists and hash tables, and algorithms like merge sort. Solutions must be written in the spaces provided below each question.

Uploaded by

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

Minor 1 Solution PDF

This document provides instructions and questions for a closed book exam. It consists of 5 questions worth a total of 35 marks. The questions assess understanding of time complexity analysis, data structures like stacks, queues, linked lists and hash tables, and algorithms like merge sort. Solutions must be written in the spaces provided below each question.

Uploaded by

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COL 106: Minor-I Exam

Maximum marks: 35
Time: 1 PM - 2PM

Important Guidelines:

1. Your answer to each question must be formal.


2. For each question, you must write your solution only in the space provided below it.
3. This is a closed book exam. You cannot look at your lecture notes.

1 Write your name and entry number [1 marks]

Name:

Entry number:

2 Short Questions [3 × 5 = 15 marks]


(a) Analyse the time complexity of the following function, and also obtain a tight bound on
the number of iterations of while loop.
void compute(int n)
{
int i=1;
while(i*i <= n)
{
for(int j=n; j>0; j=j/2)
{i = i+1;}
}
}

Part 1: The time complexity of algorithm is Θ( n) because in each iteration of “for” loop
(counted across all iterations of outer while loop)√the value of i increases by 1. √
Since the outer loop breaks when i goes beyond n, the total time complexity is Θ( n).


Part 2: The number of iterations of outer while loop is Θ( n/ log n) because in any two
√ difference in value of i is exactly ⌊log2 n⌋, and
consecutive iterations of while loop the
the loop breaks when i goes beyond n.


Alternate proof of√Part 1: The time complexity of the algorithm is Θ( n) as the outer
while loop runs Θ( n/ log n) times and the inner loop runs Θ(log n) times.

1
(b) You have a queue containing integers and an empty stack, and the only allowed operations
are: push to stack() and pop stack().
public class MyClass
{
private Stack S;
private Queue Q;
public void push_to_stack()
{
int value = Q.dequeue();
S.push(value);
}
public void pop_stack()
{
System.Out.print(S.pop() + ",");
}
}

Assume that the queue contains 1,2,3,4,5 in that order initially. Write down a sequence of
calls to push to stack() and pop stack() such that the output is “2,1,3,5,4,”.

Sequence to generate “2,1,”:


push to stack(), push to stack(), pop stack(), pop stack().

Sequence to generate “3,”:


push to stack(), pop stack().

Sequence to generate “5,4,”:


push to stack(), push to stack(), pop stack(), pop stack().

(c) Prove or disprove the following: Let m be the size of a hash table and n be the number
of entries, if n > km there must exist a chain of size > k under any hash function H.

The average chain length is n/m which is is strictly greater than k (because n > km).

Since the average chain length is strictly greater than k, there must exist a chain of size
larger than k under any hash function H.

2
(d) A circular linked list is a linked list where the tail points to its head instead of a null
pointer. The diagram below shows a circular linked list with 4 at its head and 3 at its tail.

4 2 3

Implement the method remove for a circular linked list, that takes an integer value x as
an argument and deletes all entries in the list that are equal to x.
You may assume that class Node has two attributes: data and next.
public class CircularLinkedList
{
private Node head;
public void remove(int x)
{

Node tail=head;
while (tail.next!=head) \\find tail
{tail=tail.next;}

while(head.data==x && head!=tail) \\update head


{head=head.next;}
tail.next=head;

if(head.data==x)
head=Null;
else
for(Node n=head; n!=head; n=n.next;)
while (n.next.data==x)
{n.next=n.next.next;}

}
}

(e) Consider the hash function H(x) = ((2x ∗ (6 + x mod 3)) mod 5). Draw the hash
table you will obtain after insertion of elements 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 assuming that
you use chaining as collision resolution technique.

• T [0] : 5 → 10 → N ull

• T [1] : 3 → 4 → N ull

• T [2] : 2 → 6 → N ull

• T [3] : 7 → 8 → 9 → N ull

• T [4] : 1 → N ull

3
3 Stack with Maxima [6 marks]
You need to maintain an integer stack that along with operations push(), pop(), top() has an
additional operation called ReportMaximum(). This operation returns the maximum among all
elements currently present in the stack.
Describe an implementation of the modified stack that achieves O(1) time for all four op-
erations using a single linked-list data-structure. (You may assume all integers inserted are
non-negative).

Part 1: We first provide an implementation using two standard stacks S1 and S2. Let D be
the new data structure, then the four operations on D can be performed as below.

• D.push(int i):
First perform S1.push(i). Next check if i >= S2.top(). If yes, then perform
S2.push(i).
Note: An element is pushed into S2 as long as the sequence in S2 remains non-decreasing.
This will guarantee that S2.top() returns the maximum element in D so far.

• D.pop():
First assign i=S1.pop(). This removes the top element from S1 and stores it in i.
Next check if i==S2.top(), if yes, then perform S2.pop(). Finally return i.

• D.top():
Simply return S1.top().

• D.max():
Simply return S2.top().

Part 2: Next observe that two standard stacks S1 and S2 storing non-negative integers can
be implemented using a single linked list, say L.

• We will initialize L to be linked-list storing a single element “−1”.

• The push(), pop(), top() operations on S1 can resp. be performed by inset-at-head(),


remove-at-head(), print-element-at-head() operations on L.

• The push(), pop(), top() operations on S2 can resp. be performed by inset-at-tail(),


remove-at-tail(), print-element-at-tail() operations on L.

• We can appropriately modify the functions to ensure that element −1 is never modi-
fied/returned.

4
4 Skip Lists [8 marks]
Consider a 2-level skip list data structure having lists L0 , L1 . The list L0 contains all√elements
in sorted order. Each element of L0 is present in the list L1 with probability p = 1/ n. (You
may assume that the number of elements in skip list remains Θ(n)).
Provide an implementation of search function, and analyse its expected time complexity.

Steps to search x in skip list: [1 marks]


• Search x in L1 in brute force manner, and let p be the largest element less than equal to
x in L1 .
• We next perform search from element p in list L0 . Note that if p is undefined (or −∞),
then we perform search from left most element in L0 .
• Return True if x is found in L0 , else return False.

We now analyse the time complexity to search an element x in Skip List.


Search time in L1 : The time to search x √in L1 is O(|L1 |). Since E[|L1 |] = p|L0 | = O( n),
the expected time to search x in L1 is O( n). [2 marks]

Search time in L0 : Suppose L0 = (ar , . . . , a2 , a1 , b1 , . . . , bs ), where x satisfies a1 ⩽ x < b1 .


Let “t” be the smallest index such that at lies in L1 (we set t to r + 1 if none of at ’s lie in L1 ).
Then, the time taken to search x in L0 is O(t) as we will perform our scan from the tth index
in L0 .

P rob(t = 1) = p
P rob(t = 2) = p · (1 − p)
P rob(t = 3) = p · (1 − p)2
..
.
P rob(t = r) = p · (1 − p)r−1
X r ∞
X
P rob(t > r) = 1 − P rob(t = i) = p · p · (1 − p)i .
i=1 i=r
So,
r
X
E[Time to Search x in L0 ] = E[t] = i · P rob(t = i) + r · P rob(t > r)
i=1

X
⩽ i · p · (1 − p)i−1
i=1

= O(p−1 ) = O( n).
[5 marks]

5
5 Sorting [5 marks]
Consider a variant of Merge Sort where an input list of size n is divided into 3 sub-lists of size
respectively n/2, n/4, and n/4.
(i) Write down the recurrence relation for time complexity analysis.
(ii) Solve the recurrence relation to obtain a bound on the the time complexity.

Recurrence Relation: T (n) ⩽ T (n/2) + T (n/4) + T (n/4) + cn.

Analysis:
The value T (n) is bounded above by expressions in the nodes of the following tree structures.

cn

T (n/2) T (n/4) T (n/4)

Opening expressions in layer 1 gives us:

cn

cn/2 cn/4 cn/4

T (n/4) T (n/8) T (n/8) T (n/8) T (n/16) T (n/16) T (n/8) T (n/16) T (n/16)

..
.

We have:

• The sum in each layer is at most cn, and

• After expanding the number of layers is at most O(log2 n).

Thus the time complexity is O(n log n).

You might also like