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

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

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

Prelim 2, CS2110. SOLUTION

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

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

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

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, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

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

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

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

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. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False 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

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

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

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

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

Recitation 9. Prelim Review

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

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

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

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

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

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

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

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

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

"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

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

"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

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

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

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

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

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

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

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

Computer Science E-22 Practice Final Exam

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

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

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

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

CS/ENGRD2110: Final Exam

Second Examination Solution

COS 226 Algorithms and Data Structures Fall Midterm

XC Total Max Score Grader

CSE 373 Spring Midterm. Friday April 21st

"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

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

Final Exam in Algorithms and Data Structures 1 (1DL210)

Final Examination CSE 100 UCSD (Practice)

ECE242 Data Structures and Algorithms Fall 2008

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Midterm solutions. n f 3 (n) = 3

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Section 05: Midterm Review

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CPSC 331 Term Test #2 March 26, 2007

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

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

COS 226 Midterm Exam, Spring 2009

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

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

CSL 201 Data Structures Mid-Semester Exam minutes

EECS Sample Midterm Exam

COS 226 Algorithms and Data Structures Spring Midterm Exam

CSE 373 Final Exam 3/14/06 Sample Solution

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

Section 05: Solutions

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

ECE250: Algorithms and Data Structures Midterm Review

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

CS61BL. Lecture 5: Graphs Sorting

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

COS 226 Algorithms and Data Structures Spring Midterm

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

CSE wi: Practice Midterm

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

UNIVERSITY REGULATIONS

CS126 Final Exam Review

Final Exam in Algorithms and Data Structures 1 (1DL210)

INF2220: algorithms and data structures Series 1

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

Transcription:

Prelim 2 5:30 PM, 25 April 2017 1 2 3 4 5 6 Total Question Name Short Search/ Collections Trees Graphs answer sort stuff Max 1 26 18 15 20 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 5 questions on 7 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 at the top of every page of this exam. 1 of 7

2. Short Answer (26 points.) (a) Asymptotic complexity. 8 points. Be sure to answer both (1) and (2) of this question. (1) Two correct algorithms Af and Ag have running times given by the functions f(n) and g(n) respectively. Assume f(n) is O(g(n)) and g(n) is O(f(n)). Could there be a reason to choose one algorithm over another in a practical setting? Answer YES or NO and give a short reason for your answer. (2) Give the tightest asymptotic complexity in terms of n (e.g. O(n 3 )) of the following algorithm. Explain your answer. int sum= 0; for (int h= 1; h <= n; h= h + 1) { for (int k= Math.min(10000, h); 0 < k; k= k - 1) { sum= sum + 10; sum= sum + 9999; (b) Hashing. 5 points. We are implementing a set in an array b using linear probing. The set currently has size n. Give the tightest worst-case complexity formula for enumerating all elements of the set. Do not use the load factor, which for the purpose of this question is unknown. Hint: think about what has to be done to find all the elements in the set. (c) Hashing. 7 points. An instance of class CMSGroup below maintains information about two students who grouped for an assignment on the CMS. We include only information that is necessary for this question. Class Student, among other things, has its own functions equals and hashcode. We expect some program written by the instructors to use a hash set of objects of CMSGroup to maintain information about grouping, so class CMSGroup needs functions equals and hashcode. Complete these functions below. Notes: (1) The order of the students shouldn t matter. (2) Choose a reasonable but simple hashcode; it could be based on the hashcodes of the students. 2 of 7

/* An instance maintains info about a grouped pair for a CMS assignment **/ public class CMSGroup { Student s1; // s1 and s2 are different Students Student s2; // according to function equals in class Student. /** Return true iff g is a CMSGroup whose students are equal to those in this object. */ public @Override boolean equals(object g) { public @Override int hashcode() { (d) 6 points Write the steps involved in executing a procedure call m(p, q) on static procedure m. 3. Sorting (18 points.) (a) 8 points A stable sorting algorithm maintains the relative order of equal values. For example if b[i] and b[j] are equal, i < j, and b[i] and b[j] are moved to b[i1] and b[j1] by the sorting algorithm, then i1 < j1. Suppose a 3-element array contains [3, 2, 3]. A sorting algorithm that changes it to [2, 3, 3] is not stable because it switched the order of the two 3 s. For each of the following sorting algorithms, tell us whether (1) it is stable, (2) it is unstable, or (3) it depends on the implementation. 1. insertion sort 2. selection sort 3. quicksort 4. mergesort 3 of 7

(b) 6 points The code for merge sort is given below. It contains errors. Fix the errors. Assume that method merge has the specification shown after the method. /** Sort b[h..k]. */ public static void mergesort(comparable[] b, int h, int k) { if (h < k) return; int e= h / 2; merge(b, h, e, k); mergesort(b, h, e); mergesort(b, e, k); /** b[h..j] and b[j+1..k] are sorted. * Merge them together so that b[h..k] is sorted. */ public static void merge(comparable[] b, int h, int j, int k) {... (4) 4 points Give the following information about the time- and space-complexity of Quicksort on an array of size n. 1. Quicksort, worst-case time: 2. Quicksort, expected time: 3. Our initial Quicksort algorithm has worst-case space: 4. The initial Quicksort algorithm can be modified to get worst-case space down to: 4 of 7

4. Collections classes/interfaces (15 points.) This question concerns interface Set<E>, which, among others, has these methods: boolean contains(e ob): Return true iff this set contains ob. Iterator<E> iterator(): Return an iterator over the elements in this set. int size(): Return the number of elements in this set. E[] toarray(): Return an array containing all the elements in this set. For this question, consider a deck of 0 <= n <= 52 cards, which are objects of class Card. (a) 5 points. Interface Set<E> lacks methods to extract elements from the set, but a Set<E> is iterable. Write the body of function pickanycard below it will probably use a for-each loop. /** Return any element of s. Precondition: s is not empty. */ public static Card pickanycard(set<card> s) { (b) 5 points. Is the element returned by (a) guaranteed to be a randomly selected element? BRIEFLY justify your answer. Hint: Remember that Set<E> is an interface. (c) 5 points. Assume that a variable r of type Random is available and that r was initialized correctly. Variable r has an instance method nextint(int max), and each call r.nextint(m) returns a new random integer in the range 0..m. Write a one-line implementation of pickanycard given below. Hint: look at the methods available in interface Set<E>. You don t have to use a for-each loop (but you can)! We give partial credit for solutions that require several lines of code, but for full credit, your solution must (1) be a single return statement, (2) be correct, and (3) if called often enough, would return every card in the deck at least once. Your solution need not be time-efficient. /** Return a random element of s. Precondition: s is not empty. */ public static Card pickanycard(set<card> s) { 5 of 7

5. Trees (20 points.) (a) 10 points Consider the following class Node. Complete function isbst given after it. /** An instance is a node of a tree (or a subtree rooted at that node). */ public static class Node { public int val; // the value in this node public Node left; // left subtree (or null if none) public Node rite; // right subtree (or null if none)... /** Return true if n is the root of a binary search tree (BST) whose values are in the range h..k. Precondition: n is not null and is indeed the root of a binary tree. */ public static boolean isbst(node n, int h, int k) { (b) 5 points A heap of integers is maintained in an int array b. Suppose the heap has size 10 and b[0..9] contains these 10 integers: [1 4 6 5 6 9 8 8 7 9] Below, show what b[0..8] is like after one call to poll(). We cannot give partial credit if you don t show your work. (c) 5 points It has been said that a binary tree with no duplicate values in its nodes can be reconstructed from its preorder and inorder traversals. Write down the first two steps in doing this how do you tell (1) what the root of the tree is and (2) what is in its left and right subtrees? Do not give us an example. Just tell us in English what these two steps are. 6 of 7

6. Graphs (20 points.) (a) 8 points Complete the body of the following procedure. Keep things abstract: to visit a node p write simply visit p and to ask whether p has been visited write if (p has been visited) or if (p has not been visited). Similarly, you can write about the neighbors of a node in an abstract way, as we have done in lecture. /** Visit all nodes reachable from node n along paths of unvisited nodes. */ public void DFS(Node n) { (b) 4 points State the theorem that is proved in our development of Dijkstra s shortest path algorithm. It talks about a node that can be moved to the Settled set. (c) 4 points Topological sort numbers the nodes of a DAG (Directed Acyclic Graph). It is used, for example, to determine an order in which tasks can be performed on an assembly line. Write down the property that is used to determine which node is chosen first in a topological sort. 7 of 7