CSE373 Winter 2014, Midterm Examination January 29, 2014

Similar documents
CSE373 Fall 2013, Midterm Examination October 18, 2013

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

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

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

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

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

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

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

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

CSE wi: Practice Midterm

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

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

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

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Practice Midterm Exam Solutions

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

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CSE332 Summer 2012 Final Exam, August 15, 2012

CSE332 Summer 2010: Midterm Exam Sample Solutions

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

CS171 Final Practice Exam

CS171 Midterm Exam. October 29, Name:

CSE331 Winter 2014, Midterm Examination February 12, 2014

UNIVERSITY REGULATIONS

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

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

CSE351 Winter 2016, Final Examination March 16, 2016

CSE 332, Spring 2010, Final Examination 8 June 2010

Exam Data structures DAT036/DAT037/DIT960

CSE 373 Spring Midterm. Friday April 21st

CS171 Final Practice Exam

CSE341, Fall 2011, Midterm Examination October 31, 2011

UNIVERSITY REGULATIONS

COMP 250 Midterm #2 March 11 th 2013

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

CSE 373: Practice Final

CPSC 35 Midterm Exam

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

Midterm solutions. n f 3 (n) = 3

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Computer Science E-22 Practice Final Exam

CSE331 Spring 2015, Final Examination June 8, 2015

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

CSE332 Summer 2010: Final Exam

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Computer Science Foundation Exam

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS 216 Exam 1 Fall SOLUTION

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Section 05: Midterm Review

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

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

CSE332, Spring 2012, Final Examination June 5, 2012

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

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

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

Section 05: Solutions

CS 61B Asymptotic Analysis Fall 2018

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

Sample Exam 1 Questions

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

CSE341 Spring 2016, Midterm Examination April 29, 2016

Recitation 9. Prelim Review

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Section 03: Asymptotic Analysis

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

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

Suppose we want to create a data structure called DataStore that has the following operations:

CS 315 Data Structures mid-term 2

DIT960 Datastrukturer

COMP Data Structures

CSE 373: Data Structures and Algorithms

CSE505, Fall 2012, Midterm Examination October 30, 2012

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Major2 Examination Term: 162 Year: 2016/2017

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

CS 112 Midterm Exam Fall 2016

ECE250: Algorithms and Data Structures Midterm Review

CSE341, Spring 2013, Final Examination June 13, 2013

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Priority Queues. 04/10/03 Lecture 22 1

COS 226 Algorithms and Data Structures Fall Midterm

Spring 2002: Fundamental Structures of Computer Science

Second Examination Solution

Transcription:

CSE373 Winter 2014, Midterm Examination January 29, 2014 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly at 3:20. There are 90 points total, distributed unevenly among 7 questions (many with multiple parts): Question Max Earned 1 14 2 15 3 12 4 17 5 10 6 12 7 10 Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. But clearly circle your final answer. The questions are not necessarily in order of difficulty. Skip around. Make sure you get to all the problems. If you have questions, ask. Relax. You are here to learn.

1. (14 points) For each function f(n) below, give an asymptotic upper bound using big-oh notation. You should give the tightest bound possible (so giving O(2 n ) for every question is unlikely to result in many points). (a) f(n) = n 4 + n 3 + 2n 2 + 3n + 5 (b) f(n) = log 10 (n 2 n 7 n) (c) f(n) = 6n log log(n 2 ) + 2n log 2 n + n log n (d) f(n) = log n(n 4 6) + n 2 (n 3 + 4n) (e) f(n) = 45n 4 45n 4 + 17n 1/2 + 13n (f) f(n) = n2 +3n+2 n+1 (g) f(n) = (14 log n) 2 + log (3 n ) Solution: (a) O(n 4 ) (b) O(log n) (c) O(n log 2 n) (d) O(n 5 ) (e) O(n) (f) O(n) (g) O(n)

2. (15 points) Describe the worst case running time of the following code in big-oh notation in terms of the appropriate variables. You should give the tightest bound possible. (a) void nero(int n) { for(int i=0; i < n; i++) { System.out.println("!"); for(int k=0; k < n*n*n; k++) { System.out.println("!"); (b) void claudius(int n) { for(int i=0; i < n; i++) { int j = 1; while(j < n) { System.out.println("j = " + j); j = j * 2; (c) int vespasian(int n, int m) { for(int i=m; i > 0; i--) { for(int j=0; j < 200; j++) { for(int k=0; k < n; k += 3) { System.out.println("$"); (d) int tonyblair(int n, int a) { if (a < 12) { for(int i=0; i < n; i++) { System.out.println("*"); tonyblair(n-1, a); else { for(int k=0; k < 3000; k++) { for(int j=0; j < n * k; j++) { System.out.println("#"); (e) int augustus(int n) { int ans = 1; for(int i=0; i < n; i++) { ans += augustus(i); return ans;

Solution: (a) O(n 3 ) (b) O(n log n) (c) O(nm) (d) O(n 2 ) or anything about non-terminating or infinity (e) O(2 n )

3. (12 points) The tree T, shown here, is a binary search tree where the keys are integers. 7 5 11 0 9 14 1 T has the following sequence of operations applied: T.insert(8) T.delete(0) T.insert(17) T.delete(11) Please draw T after each operation using the space below (you can show just the keys, not the corresponding values). Also, for each step, say if T is an AVL tree. Note the operations are sequential, so each one is applied to the result of the previous operations. T.insert(8) Is T an AVL Tree?

T.delete(0) Is T an AVL Tree? T.insert(17) Is T an AVL Tree? T.delete(11) Is T an AVL Tree?

Solution: 7 5 11 0 9 14 NO, 1 8 7 5 11 1 9 14 YES, 8 7 5 11 1 9 14 YES, 8 17

7 5 14 1 9 17 7 5 9 1 8 14 YES, 8 or 17

4. (17 points) In this problem, you will write some Java code for simple operations on binary search trees where keys are integers. Assume you already have the following code. Assume the method bodies, even though not shown, are correct and implement the operations as we defined them in class. public class BinarySearchTreeNode { public int key; public SomeType value; public BinarySearchTreeNode left; public BinarySearchTreeNode right; public class BinarySearchTree { private BinarySearchTreeNode root; public void insert(int key, SomeType value) {... public void delete(int key) {... public SomeType find(int key) {... Don t overlook part (c). (a) Add a method public int keysum() to the BinarySearchTree class that returns the sum of all the keys in the tree. You will need a helper method. (b) Add method public void deletemin() to the BinarySearchTree class that deletes the minimum element in the tree (or does nothing if the tree has no elements). You may want a helper method. (c) For your answers in part (a) and part (b), give a worst-case big-oh running time in terms of n, where n is the number of nodes in the tree. Do not assume the tree is balanced. There is room below and on the next page for solutions.

More space for solutions. Solution: (a) public int keysum() { return keysumhelper(root); int keysumhelper(binarysearchtreenode node) { if (node!= null) { int sum = node.key); sum += keysumhelper(node.left); sum += keysumhelper(node.right); return sum; return 0; (b) public void deletemin() { if(root == null) return; else if(root.left == null) // root is min root = root.right; else deleteminnode(root); // recursive version void deleteminnode(binarysearchtreenode prev) { if(prev.left.left == null) prev.left = prev.left.right; // partial credit: prev.left = null; else deleteminnode(prev.left); // iterative version [helper method not needed] void deleteminnode(binarysearchtreenode prev) { while(prev.left.left!= null) prev = prev.left; prev.left = prev.left.right; // partial credit: prev.left = null; // A third version that uses the provided delete method public void deletemin() { if(root == null) return; BinarySearchTreeNode n = root; while(n.left!= null) n = n.left; delete(n.key); (c) Both operations are O(n) in the worst case.

5. (10 points) Below are five data sets or situations that you have been tasked with representing in software. For each one, state which of the abstract data types we have studied you feel is best suited to represent the data set or situation. In each case, please briefly (in no more than two sentences) describe how the ADT will be used and justify your choice. For reference, we have covered the following ADTs so far: Stack, Queue, Set, Dictionary, and Priority Queue. (a) A line of people in a cafeteria. (b) The names of candidates for political office and their party affiliation (e.g. Democrat, Republican). (c) Active tech support requests where the most urgent will be resolved first. (d) A catalog of items and their prices. The items have alphanumeric id codes. (e) The species of animals living at a zoo (just the species, not how many there are).

Solution: (a) Use a queue since a cafeteria line is FIFO. (b) Use a dictionary with the candidates names as keys and the party affiliation as values to associate the information and facilitate party lookup via name. (c) Use a priority queue with the urgency of the requests as the priority, so deletemin will return the next request to be resolved. (d) A dictionary with id codes as keys and prices as values to facilitate price lookup via id code. (e) A set since there shouldn t be duplicate species and there s no ordering.

6. (12 points) An empty binary min-heap H in which the priorities are integers has the following sequence of operations applied to it: H.insert(10) H.insert(5) H.insert(7) H.insert(8) H.deleteMin() H.insert(3) For each operation, please draw the appropriate tree and the corresponding array representation using the space below (you can show just the priorities, not the corresponding items). Note the operations are sequential, so each one is applied to the result of the previous operations. H.insert(10) H.insert(5) H.insert(7)

H.insert(8) H.deleteMin() H.insert(3) Solution: 10 10

5 10 5 10 5 10 7 5 10 7 5 8 7 10 5 8 7 10 7 8 10 7 8 10

3 7 10 8 3 7 10 8

7. (10 points) Answer each of the following; no explanations required. (a) What is the worst-case asymptotic running time for the best algorithm for finding the mode (i.e. the most frequently occurring value) of the keys in a binary search tree? (b) What is the worst-case asymptotic running time for the best algorithm for finding and deleting the maximum element in an AVL tree? (c) What is the worst-case asymptotic running time of sorting a sequence of integers using a priority queue implemented with a binary min-heap? (d) What is the worst-case asymptotic running time for the best algorithm for finding something in a sorted array? (e) What is the worst-case asymptotic running time of finding and removing all values greater than 12 from a stack implemented with a linked-list (leaving the rest of the stack in its original order)? Solution: Note: We give answers here with O, but Θ would technically be more precise. (a) O(n) (b) O(log n) (c) O(n log n) (d) O(log n) (e) O(n)