You are on page 1of 18

Self Assessment Assignments II

Assignment 41:
Problem Statement
Write a program to manipulate linked lists. The input to the program will be a sequence of instructions of the
following three categories:
1. insert name age: create a node with item as the info field and insert into the current list.
2. remove N : remove the Nth item from the list.
3. print N : print the Nth item from the list. The name should be printed first and then the age with a
space in between.
4. stop : end of instructions
Each of the instructions occurs on a different line.
The list should always be maintained in sorted order in increasing order of age. If there is more than one
person of same age, the order should be as in the input. You may assume that the input is valid and correct.
While executing remove or print, if the list does not have enough elements, the command should be ignored.
You may assume that the name is always a single word with no spaces within. The age will be a positive
integer.
Terminate each line output with a newline character.
Sample Input/Output
Input
insert sasi 40
insert srini 30
print 1
insert pradeep 35
remove 3
print 1
stop
Output
srini 30
srini 30
Assignment 42:

Problem Statement
Consider a service counter. People waiting for service form a queue. At the start, the queue is empty and
time is 0. The service time per person is 3 minutes.
The input to the program consists of an integer N followed by a sequence of integers indicating the arrival
time (in minutes) of the customers. These times are given as offset from the start of the simulation, so that an
input of 44 means 44 minutes from the start of simulation. The sequence of input numbers is terminated by
the number -1.
Your program must simulate the queue and print out the following:
1. The number of customers waiting in the queue at time = N minutes from the start of simulation.
2. The arrival times of the customers in the queue at that time, in increasing order.
Each integer must be separated by a space. Terminate your output with a newline character.
For doing the computation, you can assume that if the counter is expected to be vacant at time t, the first
person in the queue will be scheduled for service, before the counting is done for time t.
You must use the queue data structure to solve this problem.
Sample Input/Output
Input
9 0 2 5 6 6 8 10 -1
Output
2 6 8
Assignment 43:

Problem Statement
In this problem you have to construct a decision tree in the manner detailed below and print out the data
which satisfies some given conditions.
Input Specification
The input will consist of a sequence of lines.
1. A series of lines containing two strings
2. END DATA (on a separate line)
3. A positive integer N (on a separate line)
4. N lines containing an integer M followed by M conditions. Each condition will be a string.
e.g, 4 C1 C2 !C3 C5
where ! indicates a negation i.e, NOT C3 in the above example. (No space between ! and condition)
Each of the lines in (1) will contain two strings.
• The first string is a sequence of zero of more L’s and R’s enclosed within a pair of brackets.
• The second string being the condition or data. (A string will be a single word without any spaces)
The Task
The lines in item (3) describe a binary decision tree, where the internal nodes are conditions and the leaf
node denotes decisions. All the conditions are binary - the true branch is to the right and the false branch to
the left.
Each line in item (4) describes a situation using the various decisions constituting the tree. You have to
traverse the tree based in these decisions, taking appropriate branches. The traversal stops when you hit a
leaf or when a decision node whose value is not given is encountered. In the first case the leaf label is
printed out. In the other case, the labels of all leaf nodes in the remaining subtree are printed out in left to
right mode.
Output Specification
For each line of conditions in (4) the program should output on a separate line all the data which satisfy all
the conditions maintaining the left to right order.
Note
The data will be contained only in the leaf nodes. The conditions given will be in the proper order starting
from the root and moving downwards.
Sample Input/Output
Input
() 4-legged
(R) carnivorous
(L) winged
(LL) amphibian
(RL) elephant
(RR) tiger
(LR) crow
(LLL) man
(LLR) frog
END DATA
4
2 !4-legged !winged
2 4-legged carnivorous
2 4-legged !carnivorous
2 !4-legged winged

Output
man frog
tiger
elephant
crow
Assignment 44:

Problem Statement
In this problem you have to write a program to check if two given binary trees are structurally equivalent.
Two trees are structurally equivalent if they are both null or if the left and right children of one are
structurally equivalent to the RESPECTIVE children of the other. In other words, when you draw the trees
on paper, they should LOOK alike (ignoring the values at the nodes).
The input to the program is a number N followed by N lines of input. Each line consists of a sequence of
positive numbers terminated by -1. There will be no duplicate numbers in any of the lines.
Construct a binary search tree with the input in the second line and use this as the basis-tree. For each of the
remaining N-1 lines, construct a binary search tree and compare against the basis tree for equivalence. If the
trees are equivalent, print YES else print NO. Also print the depth difference between the two trees (ie, depth
of the bigger tree minus the depth of the smaller tree). Both these for a given tree pair must be on one line
separated by a space. The answers for the different pairs must be on separate lines.
Sample Input/Output
Input
5
1 3 2 4 -1
4 1 2 3 -1
3 2 1 4 -1
4 3 2 1 -1
1 3 4 2 -1
Output
NO 1
NO 0
NO 1
YES 0
(Note that the depth difference will be zero if the trees are equivalent.)
Assignment 45:

Problem Statement
This problem requires you to monitor a tree for violation of the AVL balance criteria as the tree is being
constructed.
The input to the program consists of a sequence of numbers. As you read in each number, check where the
node is going to be inserted into the current tree. [At the start, the tree is empty.] If that insertion can cause
the balance of any of the nodes in the tree to go beyond what is allowed by the AVL criteria, DO NOT add
the number into the tree. Instead, print out the number into the standard output. Numbers which retain the
AVL property of the tree should be added to the tree at the appropriate place as per the method discussed in
class. Continue with the remaining numbers. Please note that you do not have to do any balancing of the
tree! The input is terminated by –1.
The output from the program consists of the numbers rejected by the program. At the end, you should also
print out the count of such numbers rejected.
Hint: It would help to keep the height of the left and right subtrees of each node along with the node. Also
note that the process of checking for violation and actually inserting are quite similar; in the former case you
do not update anything but do everything else. This observation can be used to write the code.
Sample Input/Output
Input
3 5 1 6 2 4 9 7 -1
Output
7 1
(This means rejected key(s) are: key 7, totally 1 rejected key)
Assignment 46:

Problem Statement
In this problem, you have to implement a variation of Insertion Sort as described below.
Suppose X is an array of N positive integers to be sorted. In this scheme, another array Y is used to store the
sorted integers. This array is to be viewed as a circular array, ie. index (N-1)+1 = index 0 and index 0-1 =
index N-1. The sorted integers get stored in a circular manner in Y, ie, it is possible that the index of the
smallest integer may be greater than the index of the largest integer.
Eg. 6 8 _ _ _ 1 2 4 5 is a view of the array Y sometime into the algorithm. ’_’ indicates unused locations of
the array. Smallest integer 1 is at index 5, largest integer 8 is at index 1. So the sorted array in Y is to be
generated by printing the contents from index 5 to 1, assuming the array wraps around at the end, ie. after
index 8, the next index is 0.
Assume that h holds the index of the smallest integer in Y and t holds the index of the largest integer in Y.
Initially,
1. h = t = 0
2. Y[0] = X[0] ie. the first integer in X[] is copied as the first integer in Y[].
3. All other elements in Y[] are initialised to a dummy value -1.
The rest of the integers in X[] are now inserted one by one into Y[] such that Y[] always contains a sorted
list of integers, with the smallest integer at index h and the largest at index t. This is done in the following
manner:
Let I be the next integer from X[] to be inserted into Y[]. Scan the array Y downwards from index h (with
wrap-around at the end) till index t and find out the place in Y[] where I has to fit in. If I fits in at either end
of the list, then insert it at the appropriate place in Y[]. Modify either t or h as appropriate to indicate the new
array structure; ie. either t is incremented or h is decremented (with wrap-around).
If I fits in somewhere in the middle of the list, then I should be inserted by shifting all the S smaller integers
one place to the left or by shifting all the L larger integers one place to the right, depending on the number of
integers to be shifted. That is, if S < L, the smaller integers should be shifted one place to the left and if S >=
L, the larger integers should be shifted one place to the right. Again either h or t should be modified
appropriately.
Example
Integers to be sorted X[]: 25 57 37 48 12 92 86 33
Contents of Y[] after inserting each integer from X[]:

25 –1 –1 –1 –1 –1 –1 –1 Initially (t=0, h=0)


25 57 –1 –1 –1 –1 –1 –1 57 fits in at end (t=1)
25 37 57 –1 –1 –1 –1 –1 37 fits in middle, S=1, L=1, so shift 57 right. (t=2)
25 37 48 57 –1 –1 –1 –1 48 fist in middle, S=2, L=1, So shift 57 right. (t=3)
25 37 48 57 –1 –1 –1 12 12 fits in at beginning, circular property, (h=8, t=3)
25 37 48 57 92 –1 –1 12 92 fits in at end (t=4).
25 37 48 57 86 92 –1 12 86 fits in middle, S=5, L=1, so shift 92 right, (t=5).
33 37 48 57 86 92 12 25 33 fits in middle, S=2, L=5, so shift 12, 25 left (h=7, t=5).

Input Specification
The input will consist of a single line containing an integer N followed by the N integers to be sorted. All
integers are positive and are separated by a single space. There will be no duplicates among the N integers.
Output Specification
The output should consist of N lines, each line containing N integers. The N integers are the contents of Y[]
(ie. Y[0] to Y[N-1]) after the insertion of each integer from X[]. All integers on a line should be separated by
a single space. N will be less than 50.

Sample Input/Output
Input
8 25 57 37 48 12 92 86 33
Output
25 -1 -1 -1 -1 -1 -1 -1
25 57 -1 -1 -1 -1 -1 -1
25 37 57 -1 -1 -1 -1 -1
25 37 48 57 -1 -1 -1 -1
25 37 48 57 -1 -1 -1 12
25 37 48 57 92 -1 -1 12
25 37 48 57 86 92 -1 12
33 37 48 57 86 92 12 25
Assignment 47:

Problem Statement
X is an array of N integers(no duplicates) to be sorted using the algorithm described below. The array is
partitioned by finding the location J for the pivot-key. After partitioning, all the elements in the array at
positions less than J are less than the pivot-key and all elements in the array at positions greater than J are
greater than the pivot-key.
The partitioning works as follows and the partitioning procedure gives the position J. Let LB and UB be the
lower and upper bound of the sub-array respectively.
Step I: The element at LB is exchanged with the element at the middle i.e. (LB+UB)/2 and an element
X[LB] is chosen as the pivot-key and LB is chosen as initial pivot location.
Step II: Scan the sub-array from i=LB+1 to i=UB. If X[i] < X[LB], increment the pivot location by one and
then swap X[new pivot location] with X[i]
Step III: The final pivot location is the position J required. Swap X[J] and X[LB]
After performing steps 1 to 3, the two new sub-arrays are LB to J-1 and J+1 to UB.
Input Specification:
The input will be a single line containing an integer N, followed by N integers. Assume N < 50.

Output Specification:
You have to print out the contents of the array X each time partitioning is done. The output should be on a
single line containing the array elements separated by a space.
Sample Input/Output
Input
8 25 57 48 37 12 92 86 33
Output Explanation
33 25 12 37 48 92 86 57 sort(0,7) pivot-key=37 J=3
12 25 33 37 48 92 86 57 sort(0,2) pivot-key=25 J=1
12 25 33 37 57 48 86 92 sort(4,7) pivot-key=92 J=7
12 25 33 37 48 57 86 92 sort(4,6) pivot-key=48 J=4
12 25 33 37 48 57 86 92 sort(5,6) pivot-key=57 J=5
Assignment 48:

Problem Statement
Deadlock detection is an important consideration in operating systems of computers. In this problem you
will be given a directed graph representing the state of a system. Your program should detect whether there
are any deadlocks in the system. ( A deadlock can be detected by checking for cycles in the graph)
The input will be in the form
Process X holds resource Ri and wants resource Rj (X holds Ri means an edge Ri->X and X wants Rj means
an edge X->Rj). Note that both processes and resources form nodes of the graph.
Use depth first traversal to determine whether there are any cycles in the graph. The graph contains a cycle if
while expanding a node, you encounter a child that is present in the path from the root of the current tree to
the node being expanded. You can assume that the maximum number of nodes in the graph will not exceed
50.
Input specification
An integer N and a sequence of N lines each containing 3 integers.
The first integer represents the process id P, the second represents the resource id Ri that the the process P is
holding and the third represents the resource id Rj that the process P wants. A resource id of -1 should be
ignored.
A process can hold any number of resources and can ask for any number of resources. These are given one
per line. For eg. in sample input below process 101 holds 7 and 8 and wants 9.
The integers denoting the process id and those denoting resource id’s are known to be disjoint.
Output specifications
The program should print YES if it detects a deadlock and NO otherwise. Terminate output with a newline.

Sample Input/Output
Input
5
101 7 8
101 9 -1
102 -1 7
102 8 -1
103 -1 9
Output
YES
Assignment 49:

Problem Statement
Rain strikes and the roads are flooded, Mr X has to get home from work. Your task is to make sure he
returns home in the shortest time.
Consider the roads as a graph with crossings as nodes, and the path between two nodes as an edge. Assume
the graph is undirected and the nodes are numbered, 1 to V (V <= 50).
The input consists of :
1. An integer H on a line, this is the height of Mr X.
2. Two integers N and M on the next line, where, N is the number of edges in the graph. M is the
number of nodes in the graph.
3. A sequence of N lines each having 4 integers : C1 C2 T D where, C1, C2 are the nodes (or
crossings), T is the time it takes to go from C1 to C2. D is the water depth along the edge(or road)
from C1 to C2.
Note: The depth D has to be less than the height of Mr X for him to be able to take the road.
4. Two integers S and E indicating the node at which Mr X starts and where he is expected to go to,
respectively.
As output you have to give the shortest path starting at S, listing each vertex in the order and ending with E
that Mr X can take. Assume that there will be at least one way to reach the destination and that the shortest
path is unique.
Sample Input/Output
Input
5
10 7
1 2 10 4
1 4 6 3
1 5 8 2
2 3 2 1
3 7 1 2
2 6 1 3
4 6 4 4
6 7 2 5
5 4 2 6
5 7 6 1
1 7
Output
1 2 3 7
Assignment 50:

Problem Statement
In this problem you have to construct a family tree and answer some queries on that.
The names of each person in the family and his children are given as the input. The number of children is at
most 2 (due to strict family planning policies!). Only one parent (mother or father) of the child is given in the
input. You have to construct a tree representing the family. The family tree will start from the head of the
family. Each person is a node in the tree with his children as the child nodes of that node. If a person has
only one child then the child will be the left child of that node. After constructing the family tree, given two
persons you have to find whether the first person is an ancestor of the second. X is an ancestor of Y if and
only if Y occurs in the subtree rooted at X.
Input is a number of lines followed by the string END. Each line contains a person’s name followed by the
number of children he has and their names. Assume that each name consists of at most 20 characters. The
order of the input will be in such a way that each person’s (except the root) name will appear, in the children
of his parent, before his children is given. (Eg. In the sample input given below, the children of ’Indira’
comes after Indira appears as the child of ’Jawahar’.) This is followed by a number N and N lines of name-
pairs. For each pair you have to print YES if the first person is an ancestor of the second person, else print
NO. For each pair, the output should be on a separate line.
Hint (Optional): Consider a routine called search which given a tree reference and a name, returns the
subtree of the tree given, starting with the name. Eg. search(root, "Indira") should return a reference to the
node with label "Indira".
Sample Input/Output The family tree for the sample input is as follows:

Input
Motilal 2 Jawahar Kamala Motilal
Jawahar 1 Indira
Indira 2 Sanjay Rajiv
Rajiv 2 Priyanka Rahul
END Jawahar Kamala
3
Motilal Rahul
Kamala Priyanka Indira
Indira Jawahar
Output
Sanjay Rajiv
YES
NO
NO Priyanka Rahul
Assignment 51:
Problem Statement
In this problem, you have to implement a sorting algorithm called Tournament sort, which uses a complete
binary tree.
The depth D of the tree will be given as input. Also, N positive integers will be given, where N is the Dth
power of 2 (ie, 2 ** D).
Construct a complete binary tree of depth D and fill in the leaf nodes with the N given integers in the order
given ie. the first integer should be at the leftmost leaf and the last integer should be at the rightmost leaf.
Next, update all the non-leaf nodes in the tree such that each nonleaf node contains the maximum of the
values in its children. Thus, the root will contain the maximum of all the N integers.
Implement a procedure ’maxdelete’ which essentially removes the maximum value from the tree and updates
the tree as follows:

1. traverse the tree from the root


2. find the leaf with the maximum value ie. the value at the root. (note that in this tree, if the value at a
non-leaf node is T, either its left child or its right child will have value T).
3. replace the value at that leaf node with -1 and
4. update the rest of the non-leaf nodes such that each non-leaf node contains the maximum of the
values in its children.

Perform the ’maxdelete’ operation 3 times on the tree and print out the INORDER traversal of the tree after
each operation.
Example
Given depth = 2 Given integers: 25 57 37 48
Constructed and updated tree: After first ’maxdelete’:

57 48

57 48 25 48

25 57 37 48 25 -1 37 48

After second ’maxdelete’: After third ’maxdelete’:


37 25

25 37 25 -1

25 -1 37 -1 25 -1 -1 -1

Input Specification
The input will consist of a positive integer D followed by a positive integer N (where N is the Dth power of
2) followed by N positive integers. All the integers are separated by a single space and will be on a single
line. There will be no duplicates among the N integers. D will be greater than 1 and less than 9.
Output Specification
The output should consist of 3 lines. Each line should contain the INORDER traversal of the tree after a
’maxdelete’ operation. All the integers on a line should be separated by a single space.
Sample Input/Output
Input
2 4 25 57 37 48
Output
25 25 -1 48 37 48 48
25 25 -1 37 37 37 -1
25 25 -1 25 -1 -1 -1
Assignment 52:

Problem Statement
The problem involves inserting into and maintaining a heap.
To start with, you will be given a set of numbers that you insert, as they come in, into an array. This will be
the initial heap (the numbers will be ordered in such a manner that they ensure the "heapness" of the array).
Now, you will be given another set of numbers that you have to insert into the heap. After EACH insertion,
you have to ensure the "heapness" of the array, as per the algorithm discussed in the class.
Input Specification
The first line will have a number N, followed by N integers (on the same line, separated by a space). These
N integers form the initial heap (they will be already in an order that ensures heapness). The next line will
have an integer M, followed by M integers, that have to be entered into the heap, ensuring its heapness after
EACH insertion. (M + N <= 50).
Output Specification
On the next line, there will be a number P. You have to print the element at that index, its left child, the left
child’s left child, and so on, till you reach the leaf node and print that out too, all on a single line, separated
by a space each.
Sample Input/Output
Input
6 10 8 5 4 3 2 NOT PART OF INPUT!!!
4 15 11 6 13 after inserting 13, the array should look like: 15 13 10 8 11 2 5 4 6 3
1

Output
13 8 4
Assignment 53:

Problem Statement
An educational institute has ordered it’s courses according to some criteria. The ordering tells a student
which all courses are the pre-requisites for a particular course. An ordering is said to be consistent only if
there are no cyclic dependencies in it.
You are supposed to write a program that will take an ordering as input and find out whether or not it is
consistent.
Assume that the course names will be less than 10 characters in length and that the maximum number of
courses will be 50.
Input Specification
The first line will contain a number N (the number of courses may be more than N). This line will be
followed by N lines, each having a course name, followed by a number M, giving the number of
prerequisites that course has, followed by the names of those pre-requisite courses, one space apart.
Note that the input lines or the list of pre-requisites are not in any specific order.
Output Specification
Print "YES" if the ordering is consistent, and "NO", otherwise.
Sample Input/Output
Input
5
abc 3 asd sdf dfg
sdf 2 lkj kjh
lkj 2 poi kjh
kjh 1 xyz
xyz 1 sdf
Output (for the Input shown):
NO

Explanation
In this example, sdf-kjh-xyz-sdf is a cycle, so that sdf becomes a prerequisite for itself!
Assignment 54:

Problem Statement
Consider a hash table of size N, numbered 0 to N-1. You have to insert integers into this table using the
hashing technique given below:
Let i be the integer to be inserted. Compute the index j of the location where the insertion is to be made as j
= i mod N. If this location is empty then put the element at this position else recompute the next location as
follows:
Remove the right most digit of i. Using the new value of i, recompute j = i mod N.
If the digit removed was odd, then move j locations forward from the current location else move j locations
backward from the current location (assume 0 as even). Note that this move will wrap around both the edges
of the table.
Keep doing this till you either find a free location or all the digits of i have been removed. When i comes to
only one digit, and its rightmost digit is removed, the number remaining is zero - therefore, this will lead to a
zero-step move.
If all digits of i have been removed and yet unable to find a free location, from the last location tried, start
moving in the direction corresponding to the last digit removed. Keep moving till you detect a free location.
Assume that the number of integers inserted is not more than the table size.
Input Specification
The first line will contain just one integer. This will give the table size, N. On the next line will be the list of
positive integers that need to be inserted into the table. The integers will be separated by a space each, and
the last integer will be -1 indicating end of input. (-1 is not to be inserted into the table).
Output Specification
The output should contain, for each integer, the locations that were checked while inserting that integer
(including the location in which the integer was finally inserted). The locations checked for each of the
integers should be output on a line by itself, separated by one space each, each line being terminated by a
new line.
Sample Input/Output
Input
7
38 52 145 16 179 4 -1
Output
3
3 5
5 5 4
2
4 0
4 4 3 2 1
Assignment 55:

Problem Statement
Consider a university having a very big campus spread in acres of land. The university is undergoing
computerization. All the departments (at-most 50) are to be connected to form the intranet of the university.
You have to write a program, implementing Prims algorithm, which will suggest the network topology and
also minimise the total length of cable for connecting all the departments. Input to the program will be
names of all the departments and straight line distances between the departments (Only those pairs of
departments between which cable can be laid will be given). Output of the program should be the minimum
length of the cable required.
Input specifications
The first line will contain 2 natural numbers, N and M, separated by a blank space. N indicated the number
of departments in the university and M indicates the number of pairs of departments where the cables can be
laid. The following M lines will specify the distances between M pairs of departments as

dept1 dept2 distance

Where dept1 and dept2 are names of the departments (maximum 20 characters) and distance is a positive
integer (>0). Assume that the given distances between each pairs of departments will be unique and these M
lines will contain atleast one pair for each department.
Output specifications
The first line of the output will be names of the departments as they are included in the solution separated by
blank space. If two or more departments are included at a time then their names should be printed in the
alphabetic order. The next line will be the minimum length of cable required to form the intranet, terminated
with a new line character.
Sample Input/Output
input
7 10
physics chemistry 8
biology physics 9
biology office 15
chemistry office 4
chemistry sanskrit 5
sanskrit office 7
english office 16
english sanskrit 19
english cs 12
sanskrit cs 6
output
chemistry office sanskrit cs physics biology english
44

You might also like