0% found this document useful (0 votes)
72 views73 pages

Screenshot 2023-07-06 at 12.06.58 PM

The document contains questions and answers related to data structures and algorithms from past exams. It includes 33 questions from various sources like MIT, Stanford, Cornell covering topics like binary search trees, tree traversals, graph algorithms etc. Each question has a link to the original source document for reference.

Uploaded by

shivam
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)
72 views73 pages

Screenshot 2023-07-06 at 12.06.58 PM

The document contains questions and answers related to data structures and algorithms from past exams. It includes 33 questions from various sources like MIT, Stanford, Cornell covering topics like binary search trees, tree traversals, graph algorithms etc. Each question has a link to the original source document for reference.

Uploaded by

shivam
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/ 73

Question 1 True/False

https://courses.csail.mit.edu/6.006/oldquizzes/solutions/quiz1-s2011-sol.pdf
Answer: False
Question 2

http://myslu.stlawu.edu/~ehar/Fall11/256/hw/exam2_study_guide_solutions.html
Answer:
Question 3 True/False

https://www.seas.upenn.edu/~cis120/23sp/files/exams/midterm1-22sp-solutions.pdf
Answer: True, False
Question 4

http://seu.wangmengsd.com/ds/Quiz3.pdf
Answer: B
Question 5

https://asishm.myweb.cs.uwindsor.ca/cs254/S11/sampleTests/mid2/matter.pdf
Question 6
Answer:
Question 7
Answer: 402
Question 8

https://cscie22.sites.fas.harvard.edu/files/lectures/tree_traversal_solutions.pdf
Answer:
Question 9

https://cscie22.sites.fas.harvard.edu/files/lectures/tree_traversal_solutions.pdf
Answer:
Question 10
Which of the following functions correctly returns the total number of elements in a binary tree rooted at the
node passed in as the initial argument t, assuming count is initially set to 0 if necessary?

Option C and D are on Next page


C
int tree_size(TreeNode* t, int count) {
if (t == NULL) {
return count;
} else {
return tree_size(t->left, count + 1) + tree_size(t->right, count + 1);
}
}

D
int tree_size(TreeNode* t, int count) {
if (t == NULL) {
return count;
} else {
return tree_size(t->left, count) + tree_size(t->right, count + 1);
}
}

https://moss.cs.iit.edu/cs331/other/2016-spring-midterm2.pdf
Answer: A
Question 11

https://courses.csail.mit.edu/6.006/oldquizzes/solutions/quiz1-s2011-sol.pdf
Answer:
Question 12

https://courses.csail.mit.edu/6.006/oldquizzes/solutions/q1-f2009-sol.pdf
Answer: !(# + log () time

This is a good question related to range search in BST


Question 13

https://www.it.uu.se/edu/course/homepage/algdstr1/ht13/tenta3_sols.pdf
Answer:
Question 14

https://courses.cs.vt.edu/cs2604/SummerI_2005/Koofers/MidtermSoln.pdf
Answer:
Question 15

https://cscie22.sites.fas.harvard.edu/files/practice_final.pdf
Please comment your answer in comment box
Question 16

https://kharms.infosci.cornell.edu/static/cse-247/Modules/10/homework-10.pdf
Please comment your answer in comment box
Question 17

This question is same as GATE 2016 question:


https://gateoverflow.in/39586/gate-cse-2016-set-2-question-40

https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1136/handouts/210S%20Practice%20Second
%20Midterm%20Solutions.pdf
Answer: 2! = 16 ways
Question 18

https://kharms.infosci.cornell.edu/static/cse-247/Modules/10/homework-10.pdf
Please comment your answer in comment box
Question 19:
Consider a Binary tree, we want to check given binary tree is also binary search tree or
not. For that purpose we have written a code where 2 lines are missing.

(Next page…)
Which of the following pair of lines correctly work in place of line1 and line2 ?

https://sites.santafe.edu/~moore/361/hw4solutions.pdf
Answer: A
Question 20:
Consider the following function which indent to check if given binary tree is
also binary search tree or not.

bool isBST(struct BinNode *Root) {


if (Root == NULL)
return true;

if ((Root->left != NULL) && (Root->data < Root->left->data))


return false;

if ((Root->right != NULL) && (Root->data > Root->right->data))


return false;

return (isBST(Root->left) && isBST(Root->right));


}
A. The Function implementation is correct i.e. if given binary tree is
BST then it returns true otherwise returns false

B. The Function implementation is incorrect as it may return false


even when given binary tree is BST

C. The Function implementation is incorrect as it may return true


even when given binary tree is not a BST

D. None of the above

https://courses.cs.vt.edu/cs2604/spring04/Koofers/Capra-S04-MidTerm-Soln.pdf
Answer: C
If the given tree is a binary search tree (BST), both of the if conditions will never be executed since, for any
BST, both conditions will always be false. Therefore, there is no possibility for the function to return False for
a valid BST.

if ((Root->left != NULL) && (Root->data < Root->left->data))


return false;
if ((Root->right != NULL) && (Root->data > Root->right->data))
return false;

Interestingly the given function performs ONLY a local test at each node. That is not enough.

The given function will return true for this


binary tree -
Question 21

http://www.cs.toronto.edu/~hojjat/148s07/tests/PastExams/20051ans.pdf
Answer:
Question 22
We are required to implement a function that determines whether two trees are mirror
images of each other.

For instance, in the two pairs given below, the first pair of trees are mirror images, while the
second pair is not.

https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1204/handouts/310S%20Section%20Solutions%209.pdf
Which option is correct to fill missing line?
Answer: B
Question 23

http://seu.wangmengsd.com/ds/Quiz3.pdf
Question 24

http://seu.wangmengsd.com/ds/Quiz3.pdf
Question 25
Answer:

https://inst.eecs.berkeley.edu/~cs61b/fa14/test-solutions/test2-soln.pdf
Question 26

https://stackoverflow.com/questions/17118761/how-many-permutations-of-a-given-array-result-in-bsts-of-height-2
Answer: 80

(Next page…)
Answer (Contd..)

4 is root

Now our question is now in how many ways can we arrange the remaining 6 numbers(1,2,3,5,6,7)
such that 2 always comes before 1 and 3, and 6 always comes before 5 and 7.

Select 3 places out of 6 for 1,2,3. Among these numbers, 1 and 3 can be permuted in 2! ways.

!# × 2!
"

After placing 1, 2, and 3 in these selected positions, we have three remaining positions for the
numbers 5, 6, and 7.
Since 6 and 7 can also be permuted, we have 2! permutations for these numbers.

Total choice of orders = !! × 2! × 2! = 80


"
Question 27

(Next page…)
https://inst.eecs.berkeley.edu/~cs61b/fa14/test-solutions/test2-soln.pdf
Alternate definition of diameter -

(Next page…)
Which option is correct to fill missing line?
Answer: B
Question 28
Consider the function related which checks if first node passed is ancestor of
second node .
int related(Node* ancestor, Node* descendant)
{

if (ancestor == NULL )
return 0;

if (ancestor == descendant)
return 1;

//Missing line (Next page…)


}
Which of the following option does the job correctly?

A. return related(ancestor, descendant->left) || related(ancestor, descendant->right);

B. return related(ancestor->left, descendant) || related(ancestor, descendant->right);

C. return related(ancestor, descendant->left) || related(ancestor->right, descendant);

D. return related(ancestor->left, descendant) || related(ancestor->right, descendant);


Answer: D
Question 29
Consider the following piece of code given below. And the given tree on right hand side.
void reverseLeaves(Node* curr) {

if (curr == NULL)
return NULL;

// if curr has a left and right child, switch them


if (curr->left != NULL && curr->right != NULL) {
Node* temp = curr->left;
curr->left = curr->right;
curr->right = temp;
}

reverseLeaves(curr->left);
reverseLeaves(curr->right);

return curr; (Next page…)


}
Let pointer root of the given tree
is passed as an argument to the
function then what will be the
resultant tree ?

https://cs.brown.edu/courses/cs016/static/files/
assignments/optional_sol/optional_hw5_sol.pdf
Answer: B
Question 30

https://www.cs.princeton.edu/courses/archive/spring19/cos226/exams/mid-f15-sol.pdf
Answer:
Question 31

http://www.cs.cornell.edu/courses/cs211/2006sp/Exams/final/finalsolcs211sp05.pdf
Answer:
Question 32

http://ccf.ee.ntu.edu.tw/~yen/courses/ds17/midterm-2017-sol.pdf
Answer:
Question 33

https://courses.engr.illinois.edu/cs225/fa2017/exams/practice/fa07mt2soln.pdf
Answer:
Theory Slide

Traversal trick (Stanford


slide)

https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1188/lectures/Lecture18/Lecture18.pdf

You might also like