0% found this document useful (0 votes)
15 views

Col106 Iitd

All collected questions for COL106
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)
15 views

Col106 Iitd

All collected questions for COL106
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/ 90

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Dashboard ► My courses ► 2101-COL106 ► 15 September - 21 September ► Minor Exam: Part 1

Started on Sunday, 19 September 2021, 9:15 AM


State Finished
Completed on Sunday, 19 September 2021, 9:46 AM
Time taken 30 mins 44 secs
Marks 5.00/5.00
Grade 10.00 out of 10.00 (100%)
Question 1 Given and , define as follows.
Correct

Mark 1.00 out of


1.00

Calculate the time complexity of in big-theta notation. It is given that the time
taken by is units and that by is units. Assume that: assignments,
statements, statements, mathematical operations and comparisons take unit
of time.

Select one or more:


a.

b.

c.

d.

e.

f.

g.

h.

i.
j.

k.

l.

m.

The correct answers are:

Question 2 What will be the final state of a min-heap (implemented using an array) if the following 7
Correct elements are inserted in order: 7, 0, 8, 5, 13, 17, 14. Assume that the heap was empty
before inserting these elements.
Mark 1.00 out of
1.00
Select one:
a. [0, 8, 5, 17, 14, 7, 13]
b. [0, 7, 5, 17, 8, 14, 13]
c. [0, 8, 5, 13, 14, 17, 7]

d. [0, 5, 8, 17, 7, 14, 13]

e. [0, 5, 8, 7, 13, 17, 14]


f. [0, 8, 5, 14, 13, 7, 17]

g. [0, 5, 13, 8, 7, 17, 14]


h. [0, 7, 5, 17, 13, 14, 8]

i. [0, 5, 7, 8, 14, 13, 17]

j. [0, 7, 5, 17, 14, 8, 13]

The correct answer is: [0, 5, 8, 7, 13, 17, 14]


Question 3 Consider a Merkle tree with 8 integer documents. Assume that the hash function used
Correct to calculate the digest at each node N is f(a,b) = a*3 + b (mod. 17) where a and b are
the digests (or values for the leaves) of the left and right children of N respectively.
Mark 1.00 out of
Calculate the digest at the root node if the documents (leaves) in order are [15, 12, 6,
1.00
18, 7, 9, 13, 17].

Select one:
a. 0

b. 8
c. 14

d. 16

e. 11

f. 12
g. 9

h. 10
i. 2

j. 1
k. 6

l. 4

m. 13

n. 15

o. 3

p. 7

q. 5

The correct answer is: 2


Question 4 For the given binary tree, choose the options corresponding to the correct pre-order, in-
Correct order and post-order traversals. L and R denote the left and right child of a node
respectively.
Mark 1.00 out of
1.00

Select one or more:


a. In-order traversal: 16, 11, 1, 5, 12, 7, 10, 3

b. In-order traversal: 5, 10, 11, 3, 12, 7, 16, 1

c. Post-order traversal: 5, 7, 10, 11, 12, 16, 3, 1

d. Post-order traversal: 16, 12, 3, 11, 1, 7, 5, 10

e. In-order traversal: 16, 12, 10, 5, 1, 3, 7, 11

f. Pre-order traversal: 1, 11, 16, 10, 12, 5, 7, 3


g. Pre-order traversal: 12, 5, 1, 16, 3, 10, 7, 11

h. Pre-order traversal: 12, 5, 1, 16, 10, 7, 11, 3


i. Pre-order traversal: 1, 11, 5, 3, 16, 10, 7, 12

j. Post-order traversal: 1, 7, 10, 5, 12, 3, 16, 11


k. In-order traversal: 16, 7, 5, 1, 3, 10, 11, 12

l. Post-order traversal: 16, 11, 5, 7, 12, 3, 10, 1

m. Post-order traversal: 16, 7, 1, 3, 11, 12, 5, 10

n. In-order traversal: 16, 12, 3, 10, 11, 1, 5, 7


o. Pre-order traversal: 12, 1, 16, 10, 3, 5, 11, 7

The correct answers are: Pre-order traversal: 1, 11, 16, 10, 12, 5, 7, 3, In-order
traversal: 16, 11, 1, 5, 12, 7, 10, 3, Post-order traversal: 16, 11, 5, 7, 12, 3, 10, 1
Question 5 In this question we look at an open addressing scheme called cuckoo hashing. The set
Correct of keys we are trying to store is a subset of the natural numbers and we have a hash
table T of size n. We are given two hash functions h1 and h2 which map all natural
Mark 1.00 out of
numbers to the set {0, 1, . . . , n−1}. In order to place a newly inserted key x into the
1.00
table we do the following:
1. Compute l1 = h1(x).
2. If T[l1] is free then store x in T[l1] and exit, else
3. Compute l2 = h2(x).
4. If T[l2] is free then store x in T[l2] and exit else if y is currently stored in T[l2]. Remove
y from T[l2] and store x there.
5. Now we need to find an alternate position for y. If l2 was h1(y) then compute l3 =
h2(y) and go to step 4 else if l2 was h2(y) then compute l3 = h1(y) and go to step 4. The
fifth time we have to go to step 4 we assume a cycle has occured and an exception is
thrown declaring the insertion unsuccessful.

Now, consider a hash table with 5 elements already inserted:


[49, 69, 89, __, __, __, __, __, __, 58, 18]
What is the state of the table after you insert keys 55 and 56 if h1(x)=(5x + 8) mod 11,
and h2(x)=(1x + 6) mod 11?
In case you detect a cycle (i.e. you throw an exception and abort the insertion) please
show the state of the hashtable at the 5th time the “go to” statement is executed.

Select one or more:


a. State after inserting 55: [18, 55, 69, __, __, __, __, __, 58, 49, 89]

b. State after inserting 56: [18, 55, 69, __, __, __, 56, __, 58, 49, 89]
c. State after inserting 55: [49, 69, 89, __, __, __, __, 55, __, 58, 18]

d. State after inserting 56: [69, 89, 18, __, __, __, __, 55, 49, 58, 56]

e. State after inserting 55: [49, 69, 89, __, __, __, __, __, 55, 58, 18]

f. State after inserting 56: [49, 69, 89, __, __, __, __, 55, 56, 58, 18]

g. State after inserting 56: [49, 69, 89, 56, __, __, __, __, 55, 58, 18]

h. State after inserting 56: [49, 69, 89, __, __, __, __, 56, 55, 58, 18]

i. State after inserting 55: [49, 69, 89, 55, __, __, __, __, __, 58, 18]

j. State after inserting 55: [49, 69, 89, __, __, __, 55, __, __, 58, 18]

The correct answers are: State after inserting 55: [49, 69, 89, __, __, __, __, __, 55, 58,
18], State after inserting 56: [49, 69, 89, __, __, __, __, 56, 55, 58, 18]

◄ Lab Submission 3 Friday 17.9.2021

Jump to...
Dummy Minor Part 1 ►
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2

Name:

Entry number:

There are 4 questions for a total of 15 points.

1. Answer the following questions:


(a) (1/2 point) State true or false: In any undirected graph, the sum of degrees of odd-degree vertices
is always an even. (Degree of a vertex is the number of neighbouring vertices it has.)

(a) True

(b) (1/2 point) State true or false: In any strongly connected undirected graph with at least two vertices,
there are always two vertices that have the same degree.

(b) True

(c) (1/2 point) State true or false: For any directed graph G with n 2 vertices and any two vertices
s, t in G, the number of distinct paths from s to t is O(2n )

(c) False

(d) (1/2 point) State true or false The graph given below is a bipartite graph.

(d) True

(e) (1/2 point) State true or false Any strongly connected directed graph with at least two vertices con-
tains a cycle.

(e) True
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2

(f) (1/2 point) Consider the binary search tree below. Draw the binary search tree obtained after
performing remove(88).

(g) (1 point) Consider the array A representing a min-heap below (only keys are shown) and answer
the question that follow:

Array index 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Key 3 10 5 13 17 6 11 15 16 21 18 9 8 23

Give the array after performing one removeMin() operation.

Array index 1 2 3 4 5 6 7 8 9 10 11 12 13
Key
5 10 6 13 17 8 11 15 16 21 18 9 23

(h) (1 point) Show the AVL tree that results after each of the integer keys 9, 27, 50, 15, 2, 21, and 36 are
inserted, in that order, into an initially empty AVL tree. (You may want to show steps for partial
grading)

Page 2
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2

2. (4 points) You are given an array A[1...n] representing a min-heap and an integer k. You have to design
an algorithm to output all the keys in the array that are less k.
(For example if k = 6 and the array A is as shown below, then your algorithm should output the keys 3
and 5.)

Array index 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Key 3 10 5 13 17 6 11 15 16 21 18 9 8 23

Give pseudocode for your algorithm and discuss running time. Let m be the number of keys in A that
are smaller than k. The running time of your algorithm should be O(m).

Solution: Here is the pseudocode for the algorithm. The function call should be SmallerThan(A, n, k, 1).

SmallerThan(A, n, k, i)
- if(A[i] k OR i > n)return
- Print(A[i])
- SmallerThan(A, n, k, 2i)
- SmallerThan(A, n, k, 2i + 1)

Running time: Let the number of elements smaller than k in the array A be m. The above algorithm
is a recursive algorithm. There are constant number of operations performed in every recursive call
before further recursive calls are made. So, the total running time of the algorithm is proportional
to the total number of recursive calls that are made during the execution of the algorithm. For each
element that are smaller than k, at most two recursive calls will be made. Note that no recursive
calls are made with respect to elements that are at least k. So, the total number of recursive calls
will be bounded by 2m.

Page 3
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2

3. (3 points) Consider the following basic java implementation of Binary Search Tree that we have used in
the lecture. Complete the body of the method PrintKeys below that is supposed to print all the keys
in the tree in a sorted order (increasing order). Your algorithm should run in O(n) time where n is the
number of keys in the binary search tree. (The methods for insert, search, delete are not shown below)

class Node{
public int key;//This denotes the key stored at the node
public String value;//This denotes the value stored in the node.
public Node leftChild;//This is a reference to the left child
public Node rightChild;//This is a reference to the right child
public Node parent;//This is a reference to the parent of the node
public Node(){parent = leftChild = rightChild = null;}
}
public class BinarySearchTree{
public Node root;
public int size;
public BinarySearchTree(){size=0;root=null;}

//This method checks if the given node is a leaf


public boolean isLeaf(Node N){
if(N.leftChild == null && N.rightChild == null)return(true);
else return(false);
}
... other methods

//This method should print the keys in sorted order


public void PrintKeys(){
InOrderTraverse(root);
}

public void InOrderTraverse(Node N){


if(isLeaf(N))return;
InOrderTraverse(N.leftChild);
System.out.println(N.key);
InOrderTraverse(N.rightChild);
}
}

Page 4
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2

4. (3 points) You are given an unsorted array A = A[1...n] containing n distinct integers. Design an
algorithm that outputs the smallest k elements in the array A. The running time of your algorithm
should be O(n + k log n). Give pseudocode and discuss running time.

Solution: Here is the pseudocode for the algorithm.

SmallElements(A, k, n)
- BottomUpHeapConstruction(A, n)
- For i = 1 to k
- Print(A[1])
- A[1] A[n]
-n n 1
- DownHeapBubble(A, n, 1)

DownHeapBubble(A, n, i)
-c i
- if(2i > n and 2i + 1 > n)return;
- elseif(2i  n and 2i + 1 > n){if(A[c] > A[2i]) c 2i}
- else{
if(A[c] > A[2i])c 2i
if(A[c] > A[2i + 1])c 2i + 1
}
- if(c = i)return;
- else{swap(A, i, c); DownHeapBubble(A, n, c)}

BottomUpHeapConstruction(A, n)
-h blog nc
- For i = h 1 to 0
- For j = 2i to 2i+1 1
- DownHeapBubble(A, n, j)

We use the array based bottom-up heap construction discussed in the class to convert array A to
one representing a min-heap. After this we perform the remove minimum operation on this heap to
obtain the k smallest elements.
Running time: Constructing the heap will take time O(n) and performing k remove minimum
operations costs O(k log n) time. So, the total running time is O(n + k log n).

Page 5
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1

Name:

Entry number:

There are 4 questions for a total of 15 points.

1. Answer the following:


(a) (1/2 point) State true or false: Let f (n) = 5n2n + 3n and g(n) = n3n . Then f (n) = O(g(n)).

(a) True

(b) (1/2 point) State true or false: Let f (n) = 5n2n + 3n and g(n) = n3n . Then g(n) = O(f (n)).

(b) False

(c) (1/2 point) Express the running time of the algorithm below in big-O notation.

Alg2(A, n)
- for i = 1 to n
- for j = 2i to n
- A[i] A[j] + 1

(c) O(n2 )

(d) (1 1/2 points) Solve the following recurrence relation and write the exact value of T (n). Show
calculations.
T (n) = 2 · T (n/2) + n2 ; T (1) = 1 (assume n is a power of 2)

(d) T (n) = 2n2 n

Solution:

T (n) = 2 · T (n/2) + n2
= 2 · (2 · T (n/22 ) + (n/2)2 ) + n2
= 22 · T (n/22 ) + n2 · (1 + 1/2)
..
.
= 2i · T (n/2i ) + n2 · (1 + 1/2 + ... + 1/2i 1
)
..
.
= 2log n · T (n/2log n ) + n2 · (1 + 1/2 + ... + 1/2log n 1
)
1 1/n
= n · T (1) + n2 ·
1 1/2
= n + 2n(n 1)
= 2n2 n
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1

2. (4 points) Solve the following recurrence relation and write the exact value of T (n).
8
>
<T (n 1) if n > 1 and n is odd
T (n) = 2 · T (n/2) if n > 1 and n is even
>
:
1 if n = 1

2. T (n) = 2blog nc
(Hint: Unrolling may be tricky. You may want to guess the bound and then use induction to confirm
your guess.)

Solution: The answer follows from the following claim.


Claim: For all k 0, the following holds: For all 2k  n < 2k+1 , T (n) = 2k .

Proof. We show this by induction on k. Let P (k) denote the given proposition in the claim. We
need to show that 8k, P (k) is true.
Base step: Base case is trivially true since T (1) = 1.
Inductive step: Suppose P (1), P (2), ..., P (i) are true. We will show that P (i + 1) is true. Consider
any 2i+1  n < 2i+2 . We need to consider the case when n is even and n is odd.
If n is odd, then T (n) = T (n 1) = 2 · T ( n 2 1 ). Note that 2i+1  n 1 < 2i+2 . So, we have
2i  (n 1)/2 < 2i+1 . Applying induction hypothesis, we get T (n) = 2i+1 .
If n is even, then T (n) = 2 · T (n/2). Since 2i+1  n < 2i+2 , we have 2i  n/2 < 2i+1 . Applying
induction hypothesis, we get that T (n) = 2i+1 .

Page 2
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1

3. (4 points) In the lecture, we saw an implementation of Dynamic Arrays using regular arrays involving
doubling the size of the array on overflows. What is the amortized running time for insert operations
for the implementation where we increase the size of the array by 42 on overflows. That is, if the size
of the array before overflow was m, then the size of the array after overflow is m + 42. Also, you may
assume that we start with an empty array of size 1.

Solution: Starting from an empty array of size 1, consider n insertions. We can break down the
running time into time for copying when an overflow occurs and time for inserting into the array.
Note that the total time for insertion is going to be n since n elements are being inserted. Let us
now focus on the time for copies.
Every time the current array overflows, we need to copy. The time for this copying is equal to the
size of the array when the overflow occurs. The first time the array overflows, size of the array is
1. The second time the array overflows, size of the array is (1 + 42). The third time the array
overflows, size of the array is (1 + 42 + 42). So, the ith time the array overflows, the size of the array
is (1 + 42 · (i 1)). Suppose k is the number of times the array overflows on n insertions. Then we
have
1 + 42(k 1)  n < 1 + 42k
This gives k = b n421 c. So, the total cost of copying is

k
X k(k 1) n 1 (n 1) · (n 43)
1 + 42(i 1) = k + 42 ·  + = O(n2 ).
i=1
2 42 2 · 42

So, the asymptotic running time for insert operation is O(n).

Page 3
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1

4. (4 points) Consider the following basic java implementation of linked list that we have used in the
lecture. Complete the body of the method SortList below that is supposed to sort the integers in the
linked list in non-decreasing order. It will be sufficient to give an O(n2 )-time algorithm for this problem.
You are NOT allowed to use arrays within this method.

class Node{
public int value;
public Node next;
}
class LinkedList{
public Node head;
public int size;
public LinkedList(){size=0; head = null;}
//other methods
..
.
//Implementation of Bubble Sort
public void SortList(){
Node N;
for(int i=0;i<size-1;++i){
N = head;
for(int j=0;j<size - i - 1;++j){
if(N.value > N.next.value){
int x = N.value;
N.value = N.next.value;
N.next.value = x;
}
N = N.next;
}
}
}
}

Page 4
COL106: Minor Examination
11/11/2020
Maximum Marks: 30

Instructions: ​Please read carefully before you start your examination

● Make sure that you keep your videos on for the duration of the examination
● Answer each question and its subpart on a separate piece of paper
● Write your name, entry number and page number on each page
● At the end of the examination, you have 10 minutes to send images of your answers to
your TAs using WhatApp/Telegram
● Once you have sent the pictures of your answers to your TAs, you need to convert them
into a single PDF file and upload it on Gradescope by 3:00pm.
● Answers not uploaded on gradescope will not be evaluated
● In case you are unable to upload your answers on Gradescope by 3:00pm due to
Internet or power issues, but you have sent the images of your answers to your TA by
the deadline, please send your PDF to your TA as soon as possible.
Question 1 ​[10 Marks]
(a) Consider the two functions f: N→N and g:N→N defined as follows:
f(n) = n when x is not a prime number
3
n​ when x is a prime number; and
g(n) = n​2 for all x
Formally prove that neither f(n) is in O(g(n)), nor g(n) is in O(f(n)). [7]
Let us call such a pair of functions as `mutually non-dominating’.

(b) Consider the functions u:R​+​→R and v:R​+​→R defined as follows:


u(n) = 1+sin(n); and
v(n) = 1
Do u and v also form a mutually non-dominating pair of functions? Why or why not? [3]

Question 2 ​[10 Marks]


A matrix is a two-dimensional data object made of M rows and N columns, therefore having total
M x N values. If most of the elements of the matrix have ​0 value​, it is called a sparse matrix.
E.g. out of 20 possible values, only 4 are non-zero below. It can be represented using linked
lists, with each node containing {​int row, int column, float val, node nextincol, node
nextinrow​}. SparseMatrixClass will contain two arrays of header pointers, one for rows and one
for columns.

Write pseudocode to multiply two sparse matrices A and B, using the above representation.
Note that the two matrices can have 0 values at different indices, though number of columns in
A can be assumed to be the same as the number of rows in B. [10]

Question 3 ​[10 Marks]


A ​balanced binary min-heap​ is a binary tree with the following two properties:
1. The root of any sub-tree has a key value smaller than key values all the other
nodes of the subtree
2. For any node in the tree, the number of nodes in its left and right subtree differ by
at most 1.
(a) Design a suitable data structure for implementing balanced binary min-heap [2]
(b) Write pseudo code to insert a new element in the balanced binary min-heap [4]
(c) Write pseudo code to delete the minimum element from a balanced binary min-heap [4]
Ensure that the above two properties of the balanced binary min-heap remain satisfied
after the insert and delete-min operations. Also ensure that your code is efficient. Provide
suitable proofs of correctness of your algorithm.
Please write your entry number and name on each sheet.

Entry Number: Name:


A
COL 106 MINOR EXAM II
SEMESTER I 2019-2020
1 hour

Please do not allow any bag, phone or other electronic device near you. Keep your ID card next to you on the desk. Maximum
marks available for questions are listed in []. Write answers in the provided space. Justify all answers.
1. (11)
a) [8] Given the Hash function h below, list the table slots touched/probed and show the status of the Hash table after
each listed operation (in order from left to right, starting with an empty hash table). The table has 5 slots. Assume
open addressing with hi as given: (Deletion is by marking as deleted.)
h = h0 = (∑ digits ) % 5
hi = (h0 + 3*i) % 5
Insert:873 Insert:9734 Insert:280 Delete:9734 Search 143 Insert:14 Rough space

Slots probed:
| | | | | |

b) [3] What is the termination condition for a search in such a Hash table?

2. [5] Write code (approximate syntax is OK) to restructure to balance the following configuration. Assume references
left, right, parent and value are stored for each node. Assume p.parent.left == p and n.parent.right == n. (No references
should be assumed to be null.) Restructure(Node n):
Node p = n.parent, g = p.parent; g
p
n
3. (10) Consider a binary search tree that allows keys to be repeated in multiple nodes such that keys equal to any
node's key are always in that node's left subtree. Write the pseudo-code for search and delete for this tree that each take
time O(h) for a tree with height h. The search function must return all instances and the delete function must delete all
instances of the given key.
[6] Delete(root, key): [4] Search(root, key):

4. [5] You need to implement a Red-Black tree that supports multiple threads inserting, searching or deleting in parallel.
(You may assume that each thread uses the standard algorithms to perform these operations.) What synchronization is
required to ensure that the threads do not interfere with each other's operation?

5. [5] Consider a skip-list that supports multiple threads inserting, searching or deleting in parallel. (Assume each
thread uses the standard algorithms to perform these operations.) What synchronization is required to ensure that the
threads do not interfere with each other's operation?
Please write your entry number and name on each sheet.

Entry Number: Name:

6. [4+3+3] Show that the height of a 2-4 tree with n keys is (a) no less than floor(0.5 log2n) and (b) no more than log2n.
(c) Also show that the number of comparisons required to find a key is no more than 2 log 2n. (5 extra marks to show the
bound to be 1.3 log2n)
7. [6] You are given two heaps with 2h keys each, where h is an integer. Provide an O(h) algorithm to merge these two
heap. (Assume all keys in the two heaps are comparable to each other.)

8. [6] In an AVL tree with with 20 nodes having numbers 1 to 20, respectively, as keys, which numbers may not appear
in the root?
Please write your entry number and name on each sheet.

Entry Number: Name:

9. [6] Given the keys of a Red-black tree in the pre-order traversal order, provide an algorithm to re-construct the Red-
black tree. (You do not have to determine the nodes' colors.)

10. [6] Provide non-recursive pseudo-code to compute the depth of a binary tree.
RO
U
G
H
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy