Points off Total off Net Score. CS 314 Final Exam Spring 2017

Similar documents
Points off Total off Net Score. CS 314 Final Exam Fall 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 314 Exam 2 Fall 2017

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

CS 314 Final Fall 2012

CS 314 Exam 2 Spring

CS 314 Exam 2 Spring 2016

CS 314 Final Fall 2011

CS 314 Exam 2 Spring 2018

CS 314 Exam 1 Fall 2016

CS 314 Exam 1 Spring 2018

CS 314 Exam 1 Fall 2017

CS 307 Final Spring 2010

CS 307 Final Fall 2009

CS 314 Midterm 2 Spring 2013

CS 314 Exam 1 Spring 2016

CS 307 Midterm 2 Spring 2011

CS 307 Final Spring 2011

CS 307 Midterm 2 Fall 2010

CS 314 Exam 1 Spring 2017

CS 314 Midterm 1 Spring 2014

CS 307 Final Spring 2008

CS 307 Final Spring 2009

CS 314 Exam 1 Spring 2015

CS 307 Midterm 2 Fall 2008

CS 314 Midterm 2 Fall 2012

CS 314 Midterm 1 Fall 2011

CS 314 Midterm 1 Fall 2012

CS 307 Midterm 2 Spring 2008

CS 307 Midterm 2 Fall 2009

CS 307 Midterm 1 Spring 2009

CS 307 Midterm 1 Fall 2007

1. Answer as shown or -2 unless question allows partial credit. No points off for differences in spacing, capitalization, commas, and braces

Largest Online Community of VU Students

- Alan Perlis. Topic 24 Heaps

Binary Search Trees. Chapter 21. Binary Search Trees

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

Final Examination CSE 100 UCSD (Practice)

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Lecture 23: Binary Search Trees

CS302 Data Structures using C++

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Total Score /1 /20 /41 /15 /23 Grader

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS165 Practice Final Exam

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CS171 Final Practice Exam

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

182 review 1. Course Goals

CSC 207 (17fa) Practice Final Page 1 of 19. Practice Final

CSL 201 Data Structures Mid-Semester Exam minutes

CS314 Exam 2 - Fall Suggested Solution and Criteria 1

CS 312 Midterm 2 Fall 2013

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

CS 307 Midterm 1[corrected] Spring 2008

Computer Science II Fall 2009

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100e

CS165 Practice Final Exam Answer Key

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Prelim 2, CS2110. SOLUTION

CS 312 Final Fall 2013

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS171 Final Practice Exam

USAL1J: Java Collections. S. Rosmorduc

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

COM S 211/ENGRD 211 May 15, 2003

Collections, Maps and Generics

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Final exam. CS 2110, December 15, 2016, 9:00AM

Data Structures and Algorithms Winter term 2016

Second Examination Solution

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

ITI Introduction to Computing II

ITI Introduction to Computing II

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Data Structures Brett Bernstein

TREES Lecture 12 CS2110 Fall 2016

CS 61BL Midterm 2 Review Session. Chris Gioia, Patrick Lutz, Ralph Arroyo, Sarah Kim

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

COMP 250 Midterm #2 March 11 th 2013

1B1b Implementing Data Structures Lists, Hash Tables and Trees

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Spring 2018 Mentoring 8: March 14, Binary Trees

TREES Lecture 12 CS2110 Spring 2018

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CS 104 (Spring 2014) Final Exam 05/09/2014

Total Score /15 /20 /30 /10 /5 /20 Grader

COMP 103 Introduction to Data Structures and Algorithms

Transcription:

Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2017 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points. 2. You have 3 hours to complete the test. 3. Place your final answers on this test. Not on scratch paper. Answer in pencil. 4. You may not use a calculator or any other electronic devices while taking the test. 5. When answering coding questions, ensure you follow the restrictions of the question. 6. Do not write code to check the preconditions. 7. On coding questions, you may implement your own helper methods. 8. On coding questions make your solutions as efficient as possible given the restrictions of the question. 9. Test proctors will not answer any questions regarding the content of the exam. If you think a question is ambiguous or has an error, state your assumptions and answer based on those assumptions. 10. When you complete the test show the proctor your UTID, give them the test and all the scratch paper, used or not, and leave the room quietly. 1. Short answer - 1 point each, 20 points total. Place your answer on the line next to or under the question. Assume all necessary imports have been made. a. If a question contains a syntax error or other compile error, answer Compile error. b. If a question would result in a runtime error or exception answer Runtime error. c. If a question results in an infinite loop answer Infinite loop. d. Recall when asked for Big O your answer shall be the most restrictive correct Big O function. For example, Selection Sort has an average case Big O of O(N 2 ), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N 3 ) or O(N 4 ). I want the most restrictive, correct Big O function. (Closest without going under.) A. Consider the following hashcode method for a linked list class. What is the most significant issue with the results of the method? public int hashcode() { // Instance method in a linked list class. int result = 0; for (int i = 0; i < size(); i++) { int temp = get(i).hashcode(); result = temp + result; return result; CS 314 Final Exam Spring 2017 1

B. Given a LinkedList that contains N elements what is the order, Big O, of the hashcode method in part A? C. Draw the variables, references, and objects that exist after the following code executes. Draw node objects as shown below and boxes for variables. (The example has both instance variables set to null. The example does not show any of the variables that actually refer to the Node object. You must show all variables and their references in your drawing.) Use arrows to show references and a forward slash to indicate variables that store null. Assume the Node class is the one from our singly linked list examples in lecture. data next Node n1 = new Node(null, null); // data, next Node n2 = new Node(new Node(n1, n1), n1); n1.setdata(new int[] {2, 5); n1 = (Node) n2.getdata(); n2.getnext().setnext(n2); D. The following method takes 0.20 seconds to complete when N = 1,000,000. What is the expected time for the method to complete when N = 4,000,000? The question uses the Java TreeSet class. public static TreeSet<Integer> d(int N) { TreeSet<Integer> result = new TreeSet<>(); for (int i = N; i >= 10; i -= 3) { result.add(i); return result; CS 314 Final Exam Spring 2017 2

E. The following method takes 0.03 seconds to complete when N = 500,000. What is the expected time for the method to complete when N = 1,000,000? The question uses the BinarySearchTree class from assignment 9. public static BinarySearchTree<Integer> e(int N) { BinarySearchTree<Integer> result; result = new BinarySearchTree<>(); Random r = new Random(); // O(1) for (int i = 0; i < N; i++) { int val = r.nextint(50); // O(1) result.add(val); return result; F. The following values are added, one at a time, in the order shown, to an initially empty binary search tree. The tree uses the naive insertion algorithm presented in class. What is the height of the resulting tree? 5, -5, 0, 7, 0, 9, -5, 3, -2, -1 G. What is the result of a post order traversal of the resulting tree from part F? H. What is output by the following method if list initially contains these values in the order shown? [6, 3, 8, 8, 9, 9, 7, 9] public static void h(list<integer> list) { if (list.size() > 0) { Iterator<Integer> it = list.iterator(); int prev = it.next(); while (it.hasnext()) { int curr = it.next(); if (curr == prev) { it.remove(); it.remove(); prev = curr; System.out.println(list); CS 314 Final Exam Spring 2017 3

I. The following values are inserted one at a time into an initially empty max heap. Draw the resulting max heap. 9, 4, 9, 2, 4, 10, 4 J. When discussing the map coloring problem, we represented the countries of South America using a graph. What were the vertices in the graph and when did an edge exist between two vertices? K. The outdegree of a vertex in a directed graph is the number of edges that originate at the vertex and lead to another vertex. Recall our graph class on assignment 11 used an adjacency list of edges. The other alternative presented for representing a graph was an adjacency matrix. Which representation makes it easier to determine the outdegree of a vertex and why? L. What is the order, Big O, of the following method? N = data.length. public static int el(int[] data, int tgt) { int result = 0; for (int i = 0; i < data.length; i++) { int temptgt = tgt * data[i]; for (int j = 1; j <= data.length; j *= 2) { int tot = 0; for (int k = 0; k < j; k++) { tot += data[k]; if (tot > temptgt) { result++; return result; CS 314 Final Exam Spring 2017 4

M. What can we be sure the following code will output? String s1 = ""; String s2 = "" ; Random r = new Random(); for (int i = 0; i < 4; i++) { s1 += (char) (r.nextint(5) + 'M'); s2 += (char) (r.nextint(5) + 'A'); System.out.println(s1.compareTo(s2)); N. Given the following Huffman codes for the given values, draw the resulting Huffman code tree. Value Huffman Code 48 11 53 101 32 0 65 100 O. The following method takes 1 second to complete when data.length = 500,000 and set.size() = 1,000,000. What is the expected time for the method to complete when data.length = 2,000,000 and set.size() = 4,000,000. All elements in data are distinct and all elements in data are present in set. public static int methodo(int[] data, HashSet<Integer> set) { int result = 0; for (int x : data) if (set.remove(x)) result++; return result; CS 314 Final Exam Spring 2017 5

P. Recall the standard count format from the Huffman coding assignment. The frequency of every possible value in the original file was listed as a 32 bit integer. Here is an alternative header format that explicitly lists all the values in the original file, the length of their new Huffman code, and the code itself. <Original Value><Length of Huffman Code><Huffman Code> If <Original Value> and <Length of Huffman Code> are both 8 bit integers, when will this result in a smaller header than the standard count format? Q. Draw a Red Black Tree with the maximum possible height that contains the values 1, 2, 3, 4, 5, 6, 7. Label red nodes with the word RED. Do not label black nodes. Show the values in each node. CS 314 Final Exam Spring 2017 6

R. What is the order, Big O, of the following method? The method uses the Java PriorityQueue class which in turn uses a min heap as its internal storage container. N = data.length. public static Collection<Integer> r(int[] data) { PriorityQueue<Integer> result = new PriorityQueue<>(); for (int x : data) { result.add(x); return result; S. What is output by the following code? int[] vals = {6, 4, 3, 2, 3, 0, 2; TreeMap<Integer, Integer> ts = new TreeMap<>(); for (int x : vals) { ts.put(vals[x], x); System.out.print(ts); // The Map tostring takes the form: // {key1=value1, key2=value2,..., keyn=valuen T. What is output by the method call t(14)? public static void t(int x) { if (x <= 2) { System.out.print("t"); else { int y = x / 2; System.out.print(y); t(y); System.out.print(y + x); CS 314 Final Exam Spring 2017 7

2. Linked Lists - 16 points. Complete the combine instance method for the LinkedList314 class. The method combines the calling object and the parameter into a new linked list object. For this question the linked lists always store ints. The nodes in the resulting list store the sum of the two corresponding elements (same position in list) from the original lists. The size of the resulting list is equal to the size of the smaller of the two original lists. You may use the provided zero argument constructor. You may not use any other methods in the LinkedList314 class unless you implement them yourself as a part of your solution. Do not use recursion. The LinkedList314 class uses singly linked nodes. The list only has a reference to the first node in the chain of nodes. When the list is empty, first stores null. If the list is not empty, the last node in the chain of nodes next reference stores null. You may use the nested Node class. You may not use any other Java classes or native arrays. public class LinkedList314 { // Refers to first node in the chain of nodes. private Node first; // No other instance variables public LinkedList314() { first = null; // The nested Node class. private static class Node { private int data; private Node next; public Node(int d) { data = d; Examples of calls to combine(linkedlist314 other): calling: [] -> returns [], empty list other: [] calling: [5] -> returns [], empty list other: [] calling: [5, 20, 12, 20] -> returns [15] other: [10] calling: [5, 7, 7] -> returns [9, 14, 19] other: [4, 7, 12, 20] Complete the combine method for the LinkedList314 class on the next page CS 314 Final Exam Spring 2017 8

/* pre: other!= null post: per the question description. Neither list is altered. */ public LinkedList314 combine(linkedlist314 other) { CS 314 Final Exam Spring 2017 9

3. Trees - 16 points. Complete a private instance method for a binary search tree class that checks the Red Rule for Red - Black trees. The binary search tree class uses a Red - Black tree as its internal storage container. Recall the nodes in a Red - Black tree are colored red or black. The Red Rule states a node that is red cannot have any children that are also colored red. Complete the private instance method for the BST class. You may use the nested RBNode class. You are only checking if the Red Rule is met. Do not check if the Path Rule is met. Do not use any other Java classes or methods. public class BST<E extends Comparable<? super E>> { private int size; // number of elements in this BST private RBNode<E> root; //root of tree. root == null iff size == 0 private static class RBNode<E extends Comparable<? super E>> { private E data; private RBNode<E> left; // left child, null if no left child private RBNode<E> right; // right child, null no rt. child private boolean isblack; // true if node is colored black // false if node is colored red Complete the method on the next page. CS 314 Final Exam Spring 2017 10

/* pre: none post: Return true if Red Rule met in this tree, false otherwise. */ private boolean redrulemet() { CS 314 Final Exam Spring 2017 11

4. Binary Trees - 16 points. Complete a helper method for a binary tree class that alters the binary tree so that it is a full binary tree. Recall a full binary tree is one in which every node is a leaf or has 2 children. A full binary tree does not have any internal nodes with a single child. The binary tree class for this question stores chars, but it is not a binary search tree. The helper method changes the binary tree to a full binary tree by removing any and all internal nodes that have a single child. Consider the following examples: original result A A In this example notice how the internal nodes that / \ / \ stored B and E have been removed. B C D C / / \ / \ D E F G F / G original result H H No change, already a full binary tree, / \ / \ I J I J / \ / \ K L K L original result O Q Nodes storing O and P removed. Node that contains \ Q becomes root of tree P / Q public class BinaryTree { private BNode root; // root == null iff tree is empty private static class BNode { private char data; private BNode left; private BNode right; Complete the helper method for the BinaryTree class. You may use the BNode class. Do not use any other methods from the BinaryTree class unless you implement them as part of your answer. Do not create any data structures or use any other Java classes or methods. CS 314 Final Exam Spring 2017 12

/* pre: none post: This tree is a full binary tree. Internal nodes that had a single child have been removed from the tree per the problem description. */ public void makefull() { root = help(root); // complete the helper method including the method signature CS 314 Final Exam Spring 2017 13

5. Hash tables - 16 points. Complete the remove method for a hash table that uses closed addressing and buckets of singly linked nodes to store the elements of the table. Consider the following model of the internal storage container of a hash table that stores three elements. Forward slashes represent variables that store null. The hash table does not allow duplicates. If the parameter sent to the remove method is not present, the hash table is not altered. If the item to be removed is the only element present in a chain at a given index, the array element at that index shall be set to null. You may use the provided Node class which is nested in the Hashtable class. You may use the hashcode method (which returns an int), the equals method from the Object class, and the absolute value (abs) method from the Math class. Do not use any other Java classes or methods. The Hashtable class and nested Node class for this question: public class Hashtable<E> { private int size; // number of elements in Hashtable private Node<E>[] con; // container for chains of elements public boolean remove(e val) // to be completed private static class Node<E> { private E data; private Node<E> next; // set to null if last node in chain Hash table after removing 15. Complete the method on the next page. CS 314 Final Exam Spring 2017 14

/* pre: val!= null post: val is no longer present in this Hashtable. size is updated appropriately. Returns true if this Hashtable was altered as a result of this method call, false otherwise. */ public boolean remove(e val){ CS 314 Final Exam Spring 2017 15

6. Graphs - 16 points. A clique is a subset of vertices in an undirected graph such that every pair of distinct vertices in the subset are adjacent. In other words, every pair of distinct vertices in the subset have an edge connecting them. Consider the following undirected graph. Trivially, any pair of vertices that share an edge form a clique. So for example, (A, B) form a clique. (B, C, F) form a clique. Every vertex has an edge to every other vertex in the subset. Note, (B, C, F) are part of a larger clique, (B, C, D, F) but are still considered a clique themselves. (B, C, E, F) do not form a clique because there is no edge connecting C and E. Write an instance method for the Graph class that determines if a given set of vertices form a clique or not. Assume all elements in the given set are present in the Graph. Recall the following classes: public class Graph { // The vertices in the graph. private Map<String, Vertex> vertices; private static class Vertex { private String name; private List<Edge> adjacent; private int scratch; private static class Edge { private Vertex dest; private double cost; // equals NOT overridden You may use the Map, Set, Vertex, Edge, List, and Iterator, classes and the equals method. Do not create ANY additional data structures besides Iterators. Do not use recursion. CS 314 Final Exam Spring 2017 16

/* pre: names!= null, all elements of names, represent vertices in this Graph. post: Returns true if the vertices specified by the elements of names form a clique in this Graph, false otherwise. */ public boolean formsaclique(set<string> names) { CS 314 Final Exam Spring 2017 17