CS218-Data Structures Final Exam
CS218-Data Structures Final Exam
Q1: [2+3=5]
Consider a function findTopPlayers which receives a list of N players with in a tournament and returns
top 3 players based on their scores.
void sort(Player** playersList, int N) {
int largest;
for (int j = 0; j < N - 1; j++) {
largest = j;
for (int i = j + 1; i < N; i++) {
if (playersList[i]->getScore() > playersList[largest]->getScore())
largest = i;
}
swap(playersList[j], playersList[largest]);
}
}
sort(playersList, N);
Q2: [6+2+2=10]
Given a string containing only lowercase letters and spaces, remove pairs of matching characters. This
matching starts from the end of the string. For example,
Remember that the matching starts from the end of the string. So, for input string “sadness”, the output
must be “sadne” and not “adnes”.
a) Write a C++ implementation for the function removePairs to solve the above mentioned problem.
You’re allowed to use only two stacks and a constant number of variables. The minimum program
structure is shown below
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string removePairs(string input) {
stack <char> stack1;
stack <char> stack2;
//Write code here
}
int main() {
return 0;
}
Three agents were working on a mission when they came across a secret message. They decided to deliver
this message to their boss but they can’t deliver the message as it is because it’s very sensitive information.
They decided to encrypt it and then send it to their boss. All of the three agents tried to encode it and came
up with the following Huffman codes:
a 15 00 11 01
m 8 10 01 10
space 12 01 10 00
Q4: [4+1+(2+3)=10]
a. It is possible to determine the shortest distance (or the least effort / lowest cost) between a start node
and any other node in a graph. The idea of the algorithm is to continuously calculate the shortest distance
beginning from a starting point, and to exclude longer distances when making an update. FINDPATH
function shown below calculates the shortest path from a given source vertex towards the destination.
Given the pseudocode shown above, and the graph shown in Fig.1, perform the following tasks
b. If we add another edge into a Minimum Spanning Tree, it doesn't remain minimum anymore. But for a
Maximum Spanning Tree, it makes sense because adding one or more edges to it will further increase
its overall weight/cost. So, can we really do that? Justify your answer.
c. Mr. X has just shifted in his brand new house. The floor plan for the ground floor is shown in Fig 2. Mr.
X wants to give a tour of his new home to an old friend. The doors within the rooms are represented by
the alphabets for simplicity. And the integers in the rooms are cost of moving from one door to another
door. This means, in room 2 the cost of moving from door B to door W is 2, from door B to door D is
2, and from door B to door F is also 2. Similarly, in room 3 the cost of moving from door F to door G
is 6, from door G to door K is 6 and from door F to door K is also 6.
There are three tree traversal methods in-order, pre-order and post-order. Given the iterative and recursive
code (Q5-TreeTraversals.cpp) provided answer the following questions
a. Write down specifications of your system i.e. CPU capacity, RAM, Cache (if applicable) and virtual
memory size [ The specification will directly impact the values in part b]
b. With different input sizes mentioned, find the elapse time required for in-order, preorder and post-order
traversal using recursive solution and iterative solution, and fill the following table
Table 1: Comparative analysis
Recursive Solution (Time in nano secs) Iterative Solution (Time in nano secs)
Input Size
In-order Preorder Post-order In-order Preorder Post-order
10
50
500
c. What is the efficiency in terms of Big Oh notation for each algorithm (recursive and iterative)?
d. What trend do you observe in table 1? Which version (recursive/iterative) in general grows faster? How
can you justify difference in computational time? Write crisp and clear statements. Ambiguous
statements won’t earn you any credit.
Q6: [(2+3+2)+2+1=10]
Coalesced hashing is a technique for implementing a hash table. It's an open addressing technique which
means that all keys are stored in the array itself. However, as opposed to other open addressing techniques,
it also uses nodes with next-pointers to form collision chains. Coalesced hashing, also called coalesced
chaining, is a strategy of collision resolution in a hash table that forms a hybrid of separate chaining and
open addressing. For example, Coalesced hash table holding five keys in two collision chains is shown in
the following figure (Keys of the same color hash to the same bucket.)
It uses the concept of Open Addressing(linear probing) to find first empty place for colliding element and
the concept of Separate Chaining to link the colliding elements to each other through pointers. Inside the
Modify the Q6-hashingLinearProbing.cpp to perform the following operations for Coalesced hashing and
the following functions
a. INSERT(key): The insert Operation inserts the key according to the hash value of that key if that hash
value in the table is empty otherwise the key is inserted in first empty place found after applying
QUADRATIC PROBING.
b. SEARCH(key): Returns True if key is present, otherwise return False.
c. What is the worst case complexity analysis for insertion and searching in hash table using coalesced
hashing?
The hash function used is h=(key)% (total number of keys)
For the given input sequence,
Input : {20, 35, 16, 40, 45, 25, 32, 37, 22, 55},
the output of the Coalesced hash table is shown for first 4 inputs
Q7: [3+4=7]