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

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

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Prelim 2 Solution. CS 2110, 24 April 2018, 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

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

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

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

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

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

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

Prelim 2, CS2110. SOLUTION

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

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Final Exam. CS2110 Spring May 12, Total. Max Score

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

ext Total Score /20 /20 /15 /20 /25 /5 Grader

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

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

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

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

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

Computer Science E-22 Practice Final Exam

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

Recitation 9. Prelim Review

AP Computer Science 4325

CS/ENGRD2110: Final Exam

CPSC 331 Term Test #2 March 26, 2007

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

CMSC132, Practice Questions

CS211 Spring 2005 Prelim 2 April 19, Instructions

Midterm Exam 2 CS 455, Fall 2014

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

XC Total Max Score Grader

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Summer Final Exam Review Session August 5, 2009

EXAMINATIONS 2016 TRIMESTER 2

CS61BL. Lecture 5: Graphs Sorting

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Exam Data structures DIT960/DAT036

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

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

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

COMP 250 Midterm #2 March 11 th 2013

Course Review for Finals. Cpt S 223 Fall 2008

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)

PROBLEM 1 : (Short Answer: 13 points) Three points each, except for last two which are two points 1. Both bubble-sort and selection sort are O(n 2 ) s

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

CS171 Final Practice Exam

(the bubble footer is automatically inserted into this space)

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

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS171 Final Practice Exam

CS 151 Name Final Exam December 17, 2014

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

Topics for CSCI 151 Final Exam Wednesday, May 10

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

COS 226 Algorithms and Data Structures Fall Midterm

CS200 Final Exam Fall 2007

CS 151 Name Final Exam December 17, 2014

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

CS 314 Final Fall 2012

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

CS/ENGRD2110: Prelim 2 SOLUTION

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

CS 314 Exam 2 Spring 2018

CSE 332 Winter 2015: Final Exam (closed book, closed notes, no calculators)

Algorithms: Design & Practice

Announcements. Hash Functions. Hash Functions 4/17/18 HASHING

Transcription:

Prelim 2 CS 2110, 16 November 2017, 7:30 PM 1 2 3 4 5 6 7 Total Question Name Short Heaps Tree Collections Sorting Graph answer Max 1 18 10 25 10 16 20 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck! Write your name and Cornell NetID, legibly, at the top of every page! There are 6 questions on 8 numbered pages, front and back. Check that you have all the pages. When you hand in your exam, make sure your pages are still stapled together. If not, please use our stapler to reattach all your pages! We have scrap paper available. If you do a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam so that we can make sense of what you handed in. Write your answers in the space provided. Ambiguous answers will be considered incorrect. You should be able to fit your answers easily into the space provided. In some places, we have abbreviated or condensed code to reduce the number of pages that must be printed for the exam. In others, code has been obfuscated to make the problem more difficult. This does not mean that it s good style. Academic Integrity Statement: I pledge that I have neither given nor received any unauthorized aid on this exam. I will not talk about the exam with anyone in this course who has not yet taken Prelim 2. (signature) 1. Name (1 point) Write your name and NetID, legibly, at the top of every page of this exam. 1 of 8

2. Short Answer (18 points) (a) True / False 8 points Circle T or F in the table below. (a) T F If x.equals(y) returns false, then x.hashcode()!= y.hashcode() evaluates to true. (b) T F ArrayList<String> is a subtype of ArrayList<Object>. (Recall that String[] is a subtype of Object[].) (c) T F The expected time complexity to search for a value in a binary tree is O(log n), assuming n is the number of nodes in the tree. (d) T F A connected graph with n nodes has at least n edges. (e) T F A JButton in a GUI must be listened to by only one actionperformed procedure, which defines what happens when the button is clicked. (f) T F Breadth-first search maintains a queue of nodes that have been visited. (g) T F The worst-case time complexity to compute the out-degree of a node is asymptotically slower for the adjacency-list representation of a graph than for the adjacency-matrix representation. (h) T F Quicksort is unstable. (b) 6 points Choose the tightest asymptotic complexity from O(n 2 ), O(n), O(n log n), and O(log n) for the following snippets of code: 1. for (int i= 1; i < n; i= i+1) { for (int j= i; j > 1; j= j/2) { count++; Answer: 2. for (int i= 1; i < n; i= i+1) { str= str + i; // str is an instance of String Answer: (c) 4 points Hashing. The array to the right, of size 5, is an implementation of a hash set. The table below it shows each element s hashcode. Answer questions (1) and (2) below. Each is independent of the other. Do not be concerned with resizing the array. 0 1 2 3 4 hashset a c b element a b c d hashcode 0 9 16 14 (1) 2 points. List the bucket indexes that linear probing will probe if d is inserted into the hash set. (2) 2 points. List the bucket indexes that quadratic probing will probe if d is inserted into the hash set. 2 of 8

3. Heaps (10 Points) This question explores parts of algorithm heapsort, which sorts array c shown to the right. We use the identifier m for the expression c.length, and you can too. In these programming problems, you may write swap x and y instead of the real Java statements to swap the two variables. (a) 5 points Assume that 0 <= j < m and that c[0..j-1] is a max-heap, as shown to the right. State in one (short) sentence what has to be done to make c[0..j] into a heap. Details are not wanted; just state what has to be done. 0 m c? 0 j m c max-heap? (b) 5 points Write the body of method sortheap, given below. It will use a loop, and its loop invariant is shown to the right. You may assume the existence of the following function: /** Bubble c[0] down to its appropriate position in c[0..j-1]. Precondition: c[0..j-1] is a max-heap except that c[0] may be out of place. */ public static void bubbledown(int[] c, int j) { /** Sort c. Precondition: c is a max-heap. */ public static void sortheap(int[] c) { 0 j m c max-heap sorted <= >= loop invariant. Note that it says that c[0..j-1] <= c[j..m-1] 3 of 8

4. Trees (25 Points) (a) 4 points Write down the inorder traversal sequence and postorder traversal sequence for the tree to the right. a b c d e f (b) 8 points Class TNode, to the right, is used in building binary trees. Complete recursive function aredifferent below. (Trees p and q are the different if they have different structure or if the value of any node is different.) public class TNode { String val; // value stored in the node TNode left; // left subtree (null if empty) TNode right; // right subtree (null if empty) /** Return true if trees p and q are different trees. Note: p == null or q == null denotes an empty tree. **/ public boolean aredifferent(tnode p, TNode q) { What is the tightest worst-case time complexity of this function in terms of np and nq, the number of nodes in trees p and q? Circle one of these possibilities: O(np) O(nq) O(np + nq) O(max(np, nq)) O(min(np, nq)) 4 of 8

(c) 10 points Complete the recursive function isbst, below. /** Return true iff t is a binary search tree in which all values are greater * than s1 and less than s2 using lexicographic ordering ---all values are * strictly between s1 and s2. * Note: String implements Comparable. You may use String method compareto. */ * Note: t = null denotes the empty tree. */ public static boolean isbst(tnode t, String s1, String s2) { (d) 3 points Draw the BST by starting with an empty BST and inserting these values, one by one, into it: [j, a, b, x, u, v]. Use the conventional dictionary ordering of characters. 5 of 8

5. Collections and Interface (10 Points) Answer these questions based on the part of Java collections framework shown in the diagram below. In this picture, italic font indicates an Interface, and bold font indicates a non-abstract class. Collection E Set E List E Map K, V HashSet E LinkedList E ArrayList E HashMap K, V (a) 2 points Does the following statement compile? If not, explain why. Set Integer s= new Set<>(); (b) 3 points Let h have type HashSet E and ll have LinkedList E. What are the worst-case time complexities of h.add(e), ll.add(e), and ll.add(k, e) for some object e, assuming there are n elements in the set or list? (c) 5 points Class HashSet E contains the following method: /** Add e to this set if it is not in the set. Return true iff e was added. */ public boolean add(e e) {... Assume method contains is not in class HashSet E. Implement the following function using function add. It should take expected time O(1). You can use other HashSet methods, but no loops. /** Return true iff s contains x */ public static boolean contains(hashset<string> s, String x) { 6 of 8

6. Sorting (12 Points) (a) 4 points For each of the following sorting algorithms, fill in their tightest expected space and time complexity in terms of big O. For Quick Sort, assume it is the version that reduces the space as much as possible. Expected Space Complexity Expected Time Complexity Quick Sort Merge Sort (b) 8 points To the left is a framework of mergesort. On the right are the precondition and postcondition of method merge. /** Sort b[h..k-1] */ public void mergesort(int[] b, int h, int k) { if (k - h <= 2) return; int t= (h + k) / 2; mergesort(b, par1, par2); mergesort(b, par3, par4); merge(b, h, t, k-1); h h Sorted t Sorted precondition of method merge Merged, Sorted postcondition of merge (1) 4 points. Above, the recursive calls to mergesort have missing arguments, denoted by par1, par2, par3, par4. Write what they should be according to the pre- and postcondition of merge. par1 par2 par3 par4 k k (2) 2 points. There is an error in the above code. Explain what is wrong and give the corrected version. and the corrected version. (3) 2 points. The statement int t = (k + h) / 2; would be better written as int t = h + (k - h) / 2;. Explain the reason in one sentence. 7 of 8

7. Graphs (20 Points) (a) 8 points Answer questions based on the graph to the right. (1) 2 points Show the topological sorting sequence for the graph. (2) 4 points Complete the adjacency list for the graph, in the format shown for node c, which we provided. The order of nodes in a list does not matter, a a b c d null d e e d c b (3) 2 points What is the minimal number of edges you can add to this graph to make it nonplanar? (b) 4 points State the theorem that is proved about the invariant in our development of Dijkstra s shortest path algorithm. (c) 4 points Here is one algorithm for constructing a spanning tree of a connected graph with n nodes and e edges: Start with all the nodes of the graph but no edges. Repeat until no longer possible: Add any edge of the graph that does not introduce a cycle. How many edges will this algorithm add? (d) 4 points Complete the following method. Make it recursive. You can write visit n without explaining how to visit a node and n is visited or n is unvisited to check whether a node has been visited. You can also use an English phrase to get all the neighbors of a given node. /** Visit all nodes reachable along unvisited paths from node v. */ /** Precondition: v has not been visited. */ public void visitnodes(node v) { 8 of 8