Uploaded by ccisung1

[KEY] CS 1332 - 2018Fall - Practice Exam 2

advertisement
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
CS 1332 Practice Exam 2 Key
Fall Semester 2018
Name (print clearly including your first and last name): _________________________________________
Signature: ____________________________________________________________________________________
GT account username (msmith3, etc): _________________________________________________________
GT account number (903000000, etc): ________________________________________________________
☕ You must have your BuzzCard or other form of identification on the table in front of you during
the exam. When you turn in your exam, you will have to show your ID to the TAs before we will
accept your exam. It is your responsibility to have your ID prior to beginning the exam.
☕ You are not allowed to leave the exam room and return. If you leave the room for any reason,
then you must turn in your exam as complete.
☕ Signing and/or taking this exam signifies you are aware of and in accordance with the
Academic Honor Code of Georgia Tech and the Georgia Tech Code of Conduct.
☕ Notes, books, calculators, phones, laptops, smart watches, headphones, etc. are not allowed.
☕ Extra paper is not allowed. If you have exhausted all space on this test, talk with your
instructor. There are extra blank pages in the exam for extra space.
☕ Pens/pencils and erasers are allowed. Do not share.
☕ All code must be in Java.
☕ Efficiency matters. For example, if you code something that uses O(n) time or worse when
there is an obvious way to do it in O(1) time, your solution may lose credit. If your code
traverses the data 5 times when once would be sufficient, then this also is considered poor
efficiency even though both are O(n).
☕ Style standards such as (but not limited to) use of good variable names and proper indentation
is always required. (Don’t fret too much if your paper gets messy, use arrows or whatever it
takes to make your answer clear when necessary.)
☕ Comments are not required unless a question explicitly asks for them.
Do not write below this line. It may not be scanned, therefore it will not be graded.
1A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
This page is purposely left blank. You may use it for extra space, just mention on the page of the
question that you want work here to be graded.
Do not write below this line. It may not be scanned, therefore it will not be graded.
2A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
1) Build Heap - Diagramming
Perform the Build Heap algorithm on the following array to output a M
​ in​ heap. Draw the array
following each downheap at the given index in the algorithm. Write your final answer in the Output
row of the table.
Index 0
1
2
3
4
5
6
7
8
70
20
10
45
30
15
5
25
index 4
70
20
10
25
30
15
5
45
index 3
70
20
5
25
30
15
10
45
index 2
70
20
5
25
30
15
10
45
index 1
5
20
10
25
30
15
70
45
Output:
5
20
10
25
30
15
70
45
Do not write below this line. It may not be scanned, therefore it will not be graded.
3A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
2) Binary Search Tree - Tracing
The following code recurses over a Binary Search Tree (BST) and prints to standard out occasionally.
Please trace through the code for the given BST and write what would be printed out after the code
terminates. Do ​not​ write the return value of the method. Just write each print statement’s output on a
new line. Ensure that the print statements’ outputs are correct and in the correct order. ​The method is
run on the root of the BST.
public class Node<T> {
public T data;
public Node<T> left;
public Node<T> right;
}
public int mysteryRec(Node current) {
if (current == null) {
return 0;
}
int left = mysteryRec(current.left);
int right = mysteryRec(current.right);
int tmp = left + current.data + right;
if (tmp > 15) {
System.out.println(current.data);
}
return tmp;
}
Do not write below this line. It may not be scanned, therefore it will not be graded.
4A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
3) Hash Maps - Diagramming
The hash map below maps ints to Strings. is backed by an array of capacity 5, and has a current size of 2.
Perform the specified operations in order on the hash table. The maximum load factor for this hash table is
0.5​​. If you need a collision resolution strategy, use ​linear probing​​. If you need to resize the table, resize it to a
capacity of ​(2 × initial capacity) + 1. ​ When resizing, ​completely redraw​​ the backing array like the backing
array given to you. The hashcode of an int is the number itself. The compression function is mod the table
length. Use “​DEL​​” as the delete marker if necessary.
Operations: ​add(22, “JavaScript”), add(39, “Haskell”), remove(51), add(80,
“Python”)
table[0] =
table[1] = (51, “C++”)
table[2] =
table[3] = (28, “Java”)
table[4] =
table[0] = (22, “JavaScript”)
table[1] =
table[2] =
table[3] = (80, “Python”)
table[4] =
table[5] =
table[6] = (28, “Java”)
table[7] = DEL
table[8] = (39, “Haskell”)
table[9] =
Do not write below this line. It may not be scanned, therefore it will not be graded.
5A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
table[10] =
4) AVL - Diagramming
Given both of the following AVL trees below. Perform the specified operation and draw the new tree in the
box to the right of the original tree. If you want to show multiple steps in a box, circle the final tree. If needed
in an operation, use the ​predecessor​​.
A)
add(10):
B)
remove(90):
Do not write below this line. It may not be scanned, therefore it will not be graded.
6A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
5) Tree Identification - Multiple Choice
Given the initial trees, the given operations that are performed on them, and the final trees that result
after the operations: select the best option of which tree type each tree is. If needed for an operation,
use the p
​ redecessor​.
Initial Tree
Operations
add(60)
add(40)
add(30)
Final Tree
Tree Type? (Choose one)
Binary Search Tree
AVL Tree
Heap
None of the Above
add(60)
add(70)
add(40)
Binary Search Tree
AVL Tree
Heap
None of the Above
add(60)
add(40)
remove(50)
add(30)
Binary Search Tree
AVL Tree
Heap
None of the Above
Do not write below this line. It may not be scanned, therefore it will not be graded.
7A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
6) ​Big O - Matching
For each of the operations listed below, determine the time complexity of the operation. Select the
bubble corresponding to your choice in the space provided. Unless otherwise stated, assume the
worst-case​ t​ ime complexity. However, make sure you choose the tightest Big-O upper bound possible
for the operation. Do n
​ ot​ use an amortized analysis for these operations unless otherwise specified.
A.) Average case of adding to a skip list with a coin with tails on both sides (recall: tails
promotes a node).
⃝ O(1)​
⃝ O(log n)​
​ ⃝ O(n)​ ​
⃝ O(n log n)​
⃝ O(n​2​)
B.) Running BuildHeap on an array sorted in ascending order to turn it into a Min Heap.
⃝ O(1)​
​ ​ ⃝ O(log n)​
​ ​ ⃝ O(n)​ ​
⃝ O(n log n)​
⃝ O(n​2​)
C.) Average case of adding to a hashmap using linear probing where the hash function always
returns 0.
⃝ O(1)​
⃝ O(log n)​
​
⃝ O(n)​
⃝ O(n log n)​
⃝ O(n​2​)
⃝ O(n log n)​
⃝ O(n​2​)
D.) Average case of remove() in a Max Heap.
⃝ O(1)​
⃝ ​O(log n)​
⃝ O(n)​
E.) Runtime of finding the height of the root node in an AVL tree.
⃝ O(1)​
⃝ O(log n)​
⃝ O(n)​
⃝ O(n log n)​
⃝ O(n​2​)
Do not write below this line. It may not be scanned, therefore it will not be graded.
8A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
7) Binary Search Tree - Coding
Given the starter code below, you are to implement the helper method logic for the ​evenCount()
method. Details on what the method should do are given below. Your code must be ​recursive​. Do ​not
add any import statements. You m
​ ust​ be as efficient as possible. Do ​not​ assume any other methods
are available to you that are not shown. There are no getters/setters; just ​access the values directly
since Node is an inner class.
import java.util.*;
public class BST {
private class BSTNode {
int data;
BSTNode left;
BSTNode right;
}
private BSTNode root;
private int size;
/**
* Counts the number of nodes in the tree with even data.
*
* DO NOT MODIFY THIS METHOD!
*
* Example:
4
*
/
\
*
2
6
*
/ \
/ \
*
1
3 5
7
*
* Here, evenCount() should return 3 (nodes 4, 2, and 6 are the only nodes
* with even data).
*
* @return the number of nodes in the tree with even data.
*/
public int evenCount() {
return evenCountHelper(root);
}
// See next page for the method you need to implement!
Do not write below this line. It may not be scanned, therefore it will not be graded.
9A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
/**
* Recursive helper method to count the number of nodes with even data.
*
* YOU MUST IMPLEMENT THIS METHOD RECURSIVELY! Do NOT change the method signature of
* this method.
*
* @param current the current node in traversing the BST
* @return the number of nodes in current’s subtree with even data
*/
​private int evenCountHelper(BSTNode current) {
​if (current == null) {
return 0;
}
int left = evenCount(current.left);
int right = evenCount(current.right);
if (current.data % 2 == 0) {
return left + right + 1;
} else {
return left + right;
}
}
Do not write below this line. It may not be scanned, therefore it will not be graded.
10A
Copyright © 2018.​ All rights reserved. Unauthorized reproduction, distribution, or transmission of this document is prohibited.
This page is purposely left blank. You may use it for extra space, just mention on the page of the
question that you want work here to be graded.
Do not write below this line. It may not be scanned, therefore it will not be graded.
11A
Download