COS 226 Algorithms and Data Structures Spring Midterm

Similar documents
COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Final

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Midterm Exam, Spring 2011

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Midterm Exam, Spring 2009

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

COS 226 Algorithms and Data Structures Spring Midterm Exam

Midterm, Spring 2013

COS 126 General Computer Science Spring Written Exam 1

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Fall Midterm Solutions

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

COS 226 Midterm Exam, Spring 2010

COS 226 Algorithms and Data Structures Spring Second Written Exam

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

COS 126 Written Exam 2, Fall 2009

COS 226 Algorithms and Data Structures Fall Final

CSL 201 Data Structures Mid-Semester Exam minutes

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

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

COS 126 General Computer Science Spring Written Exam 2

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

Computer Science E-22 Practice Final Exam

Data Structures and Algorithms 2018

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

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

COS 226 Algorithms and Data Structures Fall Final

ECE242 Data Structures and Algorithms Fall 2008

COS 226 Algorithms and Data Structures Practice Design Questions

Midterm 1 Written Exam

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

CS171 Final Practice Exam

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

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

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

COS 226 Algorithms and Data Structures Fall Final Exam

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

CS171 Final Practice Exam

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

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

Summer Final Exam Review Session August 5, 2009

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

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Final Exam. COS 226 Algorithms and Data Structures Fall 2015

COS 226 Midterm Fall 2007

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

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

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

Examination Questions Midterm 2

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

COS 126 General Computer Science Fall Midterm 1

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

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

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

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

COS 226 Algorithms and Data Structures Spring Final Exam

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

Final Examination CSE 100 UCSD (Practice)

Tables, Priority Queues, Heaps

Binary Search Trees Treesort

1. AVL Trees (10 Points)

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

CS 2150 (fall 2010) Midterm 2

If you are still writing when pens down is called, your exam will be ripped up and not graded sorry to have to be strict on this!

Largest Online Community of VU Students

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

Section 1: True / False (1 point each, 15 pts total)

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

Computer Science Foundation Exam

COS 126 Written Exam 2 (Spring 2015)

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

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

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

CSC 207 (16fa) Practice Exam 2 Page 1 of 13. Practice Exam 2 (Exam 2 date: Friday 11/18) Logistics:

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

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

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

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

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

ECE250: Algorithms and Data Structures Midterm Review

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

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

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Questions Total Points Score

CS 186/286 Spring 2018 Midterm 1

CSE 214 Computer Science II Searching

Transcription:

COS 226 Algorithms and Data Structures Spring 2015 Midterm This test has 9 questions worth a total of 55 points. You have 0 minutes. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Write out and sign the Honor Code pledge just before turning in the test. Again, because this exam is preprocessed by computer: make sure your writing is dark, do not write any answers outside of the designated frames, and do not write on the corner marks. I pledge my honor that I have not violated the Honor Code during this examination. Name: netid: Room: Precept: P01 P01A P02 P0 P04 P05 P05A P06 P06A P06B P07 Problem Score Problem Score 0 5 1 6 2 7 4 Sub 1 Sub 2 Total P01 Th 11 Andy Guna P01A Th 11 Shivam Agarwal P02 Th 12:0 Andy Guna P0 Th 1:0 Swati Roy P04 F 10 Robert MacDavid P05 F 11 Robert MacDavid P05A F 11 Shivam Agarwal P06 F 2:0 Jérémie Lumbroso P06A F 2:0 Ryan Beckett P06B F 2:0 Josh Wetzel P07 F Jérémie Lumbroso

0. Initialization. (1 point) In the space provided on the front of the exam, write your name and Princeton netid; fill in your precept number; write the name of the room in which you are taking the exam; and write and sign the honor code. 1. Memory and Data Structures. ( points) Using the 64-bit memory cost model from lecture, how many bytes does each SearchNode object use? public class SearchNode implements Comparable<SearchNode> { private int moves; // number of moves to get to this node private int priority; // cache of the priority private Board board; // the current board private SearchNode prev; // the previous node } // [constructor and instance methods omitted] When an object is referenced, do not include the memory for the object, but do include the memory for the reference to the object. Also, note that SearchNode is a top-level class (and not a nested class). bytes

2. Eight Sorting Algorithms and a Shuffling Algorithm. ( points) The column on the left is the original input of strings to be sorted or shuffled; the column on the right are the strings in sorted order; the other columns are the contents at some intermediate step during one of the algorithms listed below. Match up each algorithm by writing its number under the corresponding column. Use each number exactly once., 0 cyan cafe bark bole gold cyan bark bole aqua aqua 1 cafe aqua cafe bone flax bone bole bone bark bark 2 ruby bole aqua cafe dust lava bone cafe bole bole bole bone bole cyan fawn bole buff cyan bone bone 4 flax buff cyan flax cyan fawn cafe fawn buff buff 5 bone bark bone rose bole flax cyan flax cafe cafe 6 rose cyan buff ruby cafe cafe fawn gold cyan cyan 7 sage cyan cyan sage buff jade flax jade cyan cyan fawn leaf fawn bark cyan gray gold leaf dust dust 9 leaf gold leaf buff bone rose gray rose fawn fawn 10 gold jade gold fawn aqua buff jade ruby flax flax 11 jade lava jade gold bark gold lava sage gold gold 12 lava fawn lava gray gray sage leaf bark gray gray 1 bark gray sage jade jade leaf rose buff jade jade 14 gray sage gray lava kobi ruby ruby cyan kobi kobi 15 buff kobi rose leaf lava bark sage gray lava lava 16 kobi rose kobi aqua leaf kobi kobi kobi ruby leaf 17 cyan dust flax cyan mint cyan cyan lava rose mint 1 dust silk dust dust palm dust dust dust leaf palm 19 silk palm silk kobi rose silk silk silk silk rose 20 palm sand palm mint ruby palm palm palm palm ruby 21 sand flax sand palm sage sand sand sand sand sage 22 aqua mint ruby sand sand aqua aqua aqua sage sand 2 mint ruby mint silk silk mint mint mint mint silk ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 0 9 (0) Original input (1) Selection sort (2) Insertion sort () Mergesort (top-down) (4) Mergesort (bottom-up) (5) Quicksort (standard, no shuffle) (6) Quicksort (Dijkstra -way, no shuffle) (7) Heapsort () Knuth shuffle (9) Sorted

. Analysis of Algorithms and Sorting. ( points) Consider the following function that sorts an array of N comparable keys. public static void sort(comparable[] a) { int N = a.length; for (int i = 0; i < N; i++) // outer loop for (int j = N-1; j > i; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); } (a) Which of these invariants does the above code satisfy at the end of each outer i loop? Check all that apply: Entries a[0] through a[i] are in sorted order. Entries a[0] through a[i] contain the smallest keys in the entire array. Entries a[n-i-1] through a[n-1] are in sorted order. Entries a[n-i-1] through a[n-1] contain the largest keys in the entire array. (b) Which of the following are properties of the sorting algorithm? Check all that apply. The sorting algorithm is stable. The sorting algorithm is in-place. (c) How many compares does sort() make to sort an array of N keys in the best case? 0 N log 2 N 1/4 N 2 1/2 N 2 /4 N 2 (d) How does the number of exchanges made by sort() relate to the number of exchanges made by insertion sort? Check all that apply. Insertion sort makes strictly more exchanges for some arrays. Insertion sort makes strictly fewer exchanges for some arrays. Insertion sort makes strictly more exchanges for all arrays. Insertion sort makes strictly fewer exchanges for all arrays. Insertion sort makes exactly the same number of exchanges for all arrays.

4. Red-Black BSTs. (6 points) Consider the following left-leaning red-black BST. Midterm, Fall 2014 1 red link 12 Midterm, Spring 2 2015 6 16 22 2 4 10 14 20 rotate right 26 0 rotate left 2 T 24 T1 Suppose that you insert the key 2 into this left-leaning red-black BST above. T1 T2 T2 T (a) Which of the following color flips result? Check all that apply. Color flip 1 Color flip 22 Color flip 2 Color flip 24 Color flip 26 Color flip 2 1 T1 T2 T T4 color flip 1 T1 T2 T T4 (b) Which of the following rotations result? Check all that Midterm, apply. Spring 2015 Rotate 1 left Rotate 1 right Rotate 22 left Rotate 22 right Rotate 2 left Rotate 2 right Rotate 24 left Rotate 24 right Rotate 26 left Rotate 26 right Rotate 2 left Rotate 2 right T1 T2 T rotate right color flip rotate left T1 T2 T 1 1 T1 T2 T T4 T1 T2 T T4

5. Hash Tables. (6 points) (a) What is a collision in a hash table implementation of a symbol table? Check the best definition. Two key-value pairs that have equal keys but different values. Two key-value pairs that have different keys and hash to different indices. Two key-value pairs that have different keys but hash to the same index. Two key-value pairs that have equal keys but hash to different indices. (b) A linear-probing hash table of length 10 uses the hash function h(x) = x mod 10. After inserting six integer keys into an initially empty hash table, the array of keys is: 0 1 2 4 5 6 7 9 42 2 4 52 46 Which of the following choice(s) are insertion sequences resulting in the above hash table? Assume that the length of the hash table does not change during the insertions. Check all that apply. 46, 42, 4, 52, 2, 4, 42, 2, 52,, 46 46, 4, 42, 2, 52, 42, 46,, 2, 4, 52 42, 2, 4, 52, 46, (c) Suppose that your hash function does not satisfy the uniform hashing assumption. Which of the following can result? Check all that apply. Poor performance for insert. Poor performance for search hit. Poor performance for search miss. Uneven distribution of lengths of chains in separate-chaining hash table. Large clusters in linear-probing hash table. Linear-probing hash table can become 100% full.

6. Properties of Data Structures. (6 points) Check whether each of the following statements is true or false. (a) Binary heaps. Let a[] be a max-oriented binary heap that contains the N distinct integers 1, 2,..., N in a[1] through a[n]. Then, key N must be in a[1]; key N 1 must be in either a[2] or a[]; and key N 2 must be in either a[2] or a[]. The order of growth of the total number of compares to insert N distinct keys in descending order into an initially empty max-oriented binary heap is N. A -heap is an array representation (using 1-based indexing) of a complete -way tree, where the key in each node is greater than (or equal to) its children s keys. In the worst case, the number of compares to insert a key in a -heap containing N keys is 1 log N. True False (b) Binary search trees. It is possible to insert any batch of N keys into an initially empty binary search tree using at most 2N compares. The height of any left-leaning red-black BST with N keys is always between 1 log 2 N and 2 log 2 N. The subtree rooted at any node of a 2- tree is itself a 2- tree. True False

7. Algorithm Design. ( points) Rotation of sorted array Let a = a 0, a 1,..., a N 1 be an array of length N. An array b is a circular shift of a if it consists of the subarray a k, a k+1,..., a N 1 followed by the subarray a 0, a 1,..., a k 1 for some integer k. In the example below, b is a circular shift of a (with k = 7 and N = 10). sorted array a[] circular shift b[] 0 1 2 4 5 6 7 9 0 1 2 4 5 6 7 9 1 2 5 6 9 4 55 9 4 55 9 1 2 5 6 9 Suppose that you are given an array b that is a circular shift of some sorted array (but you have access to neither k nor the sorted array). Assume that the array b consists of N comparable keys, no two of which are equal. Design an efficient algorithm to determine whether a given key appears in the array b. The order of growth of the running time of your algorithm should be log N (or better) in the worst case, where N is the length of the array. Briefly describe your algorithm, using either crisp and concise prose or code. correctness, efficiency, and clarity. Your answer will be graded on 9

. Data Structure Midterm Design. Spring 2015: (9 points) Threaded Set Design an efficient data type to store a threaded set of strings, which maintains a set of strings (no duplicates) and the order in which the strings were inserted, according to the following API: public class ThreadedSet ThreadedSet() create an empty threaded set void add(string s) add the string to the set (if it is not already in the set) boolean contains(string s) is the string s in the set? String previouskey(string s) the string added to the set immediately before s (null if s is the first string added; exception if s is not in set) Here is an example: ThreadedSet set = new ThreadedSet(); set.add("aardvark"); // [ "aardvark" ] set.add("bear"); // [ "aardvark", "bear" ] set.add("cat"); // [ "aardvark", "bear", "cat" ] set.add("bear"); // [ "aardvark", "bear", "cat" ] // (adding a duplicate key has no effect) set.contains("bear"); set.contains("tiger"); set.previouskey("cat"); set.previouskey("bear"); set.previouskey("aardvark"); // true // false // "bear" // "aardvark" // null 40 Your answer will be graded on correctness, efficiency, and clarity. (a) Declare the instance variables for your ThreadedSet data type. You may declare nested classes or use data types that we have considered in this course. public class ThreadedSet { }

(b) Briefly describe how to implement each of the operations, using either crisp and concise prose or code. void add(string s): boolean contains(string s): String previouskey(string s): (c) Under reasonable technical assumptions, what is the order of growth of each of the methods as a function of the number of keys N in the data structure? Assume that the length of all strings is bounded by a constant. 1 log N N N N log N N 2 add() contains() previouskey()