COS 226 Algorithms and Data Structures Spring Midterm

Similar documents
COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall 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

COS 226 Algorithms and Data Structures Practice Design Questions

COS 226 Algorithms and Data Structures Fall Final

COS 226 Algorithms and Data Structures Spring Midterm Exam

COS 226 Algorithms and Data Structures Spring Final Exam

Midterm, Spring 2013

Final Exam. COS 226 Algorithms and Data Structures Fall 2015

COS 226 Midterm Exam, Spring 2010

COS 226 Algorithms and Data Structures Fall Final Exam

CS210 Study Guide Swami Iyer. 1 Programming Model (Algorithms 1.1) 2. 2 Data Abstraction (Algorithms 1.2) 4

COS 226 Algorithms and Data Structures Fall Midterm Solutions

COS 126 General Computer Science Spring Written Exam 1

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

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

COS 126 General Computer Science Spring Written Exam 2

COS 226 Algorithms and Data Structures Fall Final

COS 226 Algorithms and Data Structures Spring Second Written Exam

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

COS 226 Algorithms and Data Structures Spring Final

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

COS 126 General Computer Science Fall Midterm 1

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

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

Final Examination CSE 100 UCSD (Practice)

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

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

COS 126 MIDTERM 1, FALL

COS 126 General Computer Science Spring Midterm 1

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

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

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

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

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

COS 226 Final Exam, Spring 2009

ECE242 Data Structures and Algorithms Fall 2008

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

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

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

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

COS 126 Written Exam 2, Fall 2009

COS 226 Midterm Fall 2007

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE332 Summer 2012 Final Exam, August 15, 2012

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

1. AVL Trees (10 Points)

Questions Total Points Score

COS 126 Written Exam 2 (Spring 2015)

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

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

Priority Queues 1 / 15

COS 226 Final Exam, Spring 2010

Practice Midterm Exam Solutions

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

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

1 (15 points) LexicoSort

COS 126 Midterm 1 Written Exam, Fall 2009

COS 126 Midterm 1 Written Exam Spring 2015

Computer Science E-22 Practice Final Exam

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

Computer Science Spring 2005 Final Examination, May 12, 2005

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CISC 621 Algorithms, Midterm exam March 24, 2016

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

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

COS 126 Midterm 1 Written Exam Fall 2011

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

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

cs2010: algorithms and data structures

COS 226 Written Exam 2 Fall 2016

Examination Questions Midterm 2

COS 126 Midterm 1 Written Exam Fall 2014!

(the bubble footer is automatically inserted in this space)

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

Solutions to Exam Data structures (X and NV)

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

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Lecture 13: AVL Trees and Binary Heaps

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

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

Data Structures in Java

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

AP Computer Science 4325

COS 126 Midterm 2 Programming Exam Fall 2012

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

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Transcription:

COS 226 Algorithms and Data Structures Spring 2012 Midterm This test has 9 questions worth a total of 60 points. You have 80 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 before turning in the test. I pledge my honor that I have not violated the Honor Code during this examination. Problem Score Problem Score 0 5 1 6 2 7 3 8 4 Sub 1 Sub 2 Total Name: Login ID: Precept: P01 Th 12:30 Diego Botero P01A Th 12:30 David Shue P01B Th 12:30 Joey Dodds P02 Th 1:30 Josh Hug P03 Th 3:30 Josh Hug P04 F 11 Joey Dodds P04A F 11 Jacopo Cesareo 1

2 PRINCETON UNIVERSITY 0. Miscellaneous. (1 point) In the space provided on the front of the exam, write your name and Princeton NetID; circle your precept number; and write out and sign the honor code. 1. Analysis of algorithms. (5 points) Consider the following code fragment, where a[] is an array of N Point2D objects. int min = N; for (int i = 0; i < N; i++) { Selection.sort(a, a[i].by_polar_order); for (int j = 0; j < N; j++) { for (int k = j+1; k < N; k++) { if (a[j].distanceto(a[k]) <= 1.0) { min = Math.min(min, k - j); (a) Suppose that the code fragment takes 30 seconds when N = 2, 000. Estimate the running time (in seconds) as a function of the input size N. Use tilde notation to simply your answer. (b) Suppose that you replace the call to Selection.sort() with a call to Merge.sort(). What is the order of growth of the running time of the modified 11-line code fragment as a function of N? Circle the best answer. 1 N N log N N 2 N 2 log N N 3 2 N

COS 226 MIDTERM, SPRING 2012 3 2. Data structure and algorithm properties. (8 points) (a) Match up each quantity on the left with the best matching quantity on the right. You may use a letter more than once or not at all. Min height of a binary heap with N keys. A. 1 Max height of a binary heap with N keys. B. 1 2 lg N Min height of a 2-3 tree with N keys. C. log 3 N Max height of a 2-3 tree with N keys. D. ln N Min height of left-leaning red-black BST with N keys. E. lg N Max height of left-leaning red-black BST with N keys. F. 2 lg N Min height of a weighted quick union tree with N items. G. 2 ln N Max height of a weighted quick union tree with N items. H. N (b) A sorting algorithm is parsimonious if no pair of items is compared more than once. Circle the following sorting algorithms (as implemented in lecture and the textbook) if they are parsimonious; cross them out if they are not parsimonious. insertion sort selection sort top-down mergesort heapsort

4 PRINCETON UNIVERSITY 3. Data structures. (8 points) The Java library StringBuilder represents a mutable sequence of characters. Suppose that it is implemented using a resizing array (doubling when full and halving when one-quarter full), maintaining one instance variable N to count the number of characters in the sequence and another instance variable a[] to hold the sequence of characters. public class StringBuilder { private int N; // number of characters in the sequence private char[] a; // the character sequence a[0], a[1],..., a[n-1]... (a) Using the 64-bit memory cost model from the textbook, how much memory (in bytes) does a StringBuilder object use to store a sequence of N characters? Simplify your answer using tilde notation. Best case: Worst case: (b) What is the order of growth of the amortized running time of each of operation below? Write down the best answer in the space provided, using one of the following functions. 1 log N N N N log N N 2 operation description running time charat(int i) return the ith character in sequence deletecharat(int i) delete the ith character in the sequence append(char c) append c to the end of the sequence set(int i, char c) replace the ith character with c

COS 226 MIDTERM, SPRING 2012 5 4. 8 sorting and shuffling algorithms. (8 points) The column on the left is the original input of strings to be sorted or shuffled; the column on the right are the string in sorted order; the other columns are the contents at some intermediate step during one of the 8 algorithms listed below. Match up each algorithm by writing its number under the corresponding column. Use each number exactly once. lynx bass lion bass bass bass bass gnat wren bass bass bear frog bear bear bear bear bass worm bear bear crab mole clam crab clam clam bear oryx clam crab lion hawk crab lynx crab crab crab swan crab lion goat wren frog frog frog crow lion wolf crow goat duck lynx gnat goat goat deer goat mule deer mole frog crab goat lion hawk dove duck mole dove frog dove swan hawk mole lion duck frog puma duck swan clam bear lion clam lynx frog dove seal frog clam hawk clam lynx hawk mole gnat clam deer gnat hawk deer bass lynx swan swan goat hawk lion goat wren crow goat mole wren wren hawk deer goat hawk mule gnat mule mule gnat gnat mule crow bear lion oryx lynx oryx oryx lynx lynx oryx lynx lynx lynx gnat lynx gnat swan mule mule lynx lynx gnat lynx lynx puma lynx wren oryx oryx lynx oryx lynx mole puma worm puma puma crow puma puma puma frog mule worm seal worm worm puma worm worm worm crab oryx seal oryx seal seal seal crow seal seal bass puma crow mule crow crow worm deer lion mule crow seal deer wolf deer deer deer dove wren wren clam swan wolf wren wolf wolf dove duck wolf wolf hawk wolf dove swan dove dove duck seal mole swan dove worm duck mole duck duck wolf wolf swan mole duck wren ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 0 1 (0) Original input (1) Sorted (2) Selection sort (3) Insertion sort (4) Mergesort (top-down) (5) Mergesort (bottom-up) (6) Quicksort (standard, no shuffle) (7) Quicksort (3-way, no shuffle) (8) Heapsort (9) Knuth shuffle

6 PRINCETON UNIVERSITY 5. Red-black BSTs. (8 points) Midterm, Spring 2012 Consider the following left-leaning red-black BST. Some of the colors and key values are suppressed. red link O F? B L S Z A D J M Q T Y C I K P H (a) Which one or more of the keys below could be the one labeled with a question mark? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (b) Which one or more of the keys below must be red (link between it and its parent is red)? P Q S T Y (c) How many left rotation, right rotation, and color flip operations would be used to insert each key below into the original red-black BST above? E N G rotateleft() rotateright() flipcolors()

COS 226 MIDTERM, SPRING 2012 7 6. Hashing. (6 points) Suppose that the following keys are inserted in some order into an initially empty linearprobing hash table of size 7 (assuming no resizing), using the following table of hash values: key hash A 5 B 2 C 5 D 1 E 4 F 1 G 3 (a) Give the contents of the linear-probing array if the keys are inserted in alphabetical order: A, B, C, D, E, F, G. 0 1 2 3 4 5 6 (b) Which of the following could be the contents of the linear-probing array if the keys are inserted in some other order? I. 0 1 2 3 4 5 6 A F D B G E C II. 0 1 2 3 4 5 6 F A D B G E C III. 0 1 2 3 4 5 6 C A B G F E D Circle the best answer. (a) I only. (b) I and II only. (d) I, II and III. (e) None. (c) I and III only.

8 PRINCETON UNIVERSITY 7. Comparing two arrays of points. (8 points) Given two arrays a[] and b[], containing M and N distinct points in the plane, respectively, (with N M), design an algorithm to determine how many points are in common between the two arrays. The running time of your algorithm should be proportional to N log M in the worst case and use at most a constant amount of extra memory. Partial credit for N log N or for using a linear amount of extra memory. (a) Give a crisp and concise English description of your algorithm in the box below. Your answer will be graded on correctness, efficiency, clarity, and conciseness. (b) What is the order of growth of the worst case running time of your algorithm? Circle the best answer. M N M log M M log N N log M N log N MN N 2 (c) How much extra memory does your algorithm use? Circle the best answer. 1 log M log N M N MN N 2

COS 226 MIDTERM, SPRING 2012 9 Priority queue API 8. Randomized priority queue. (8 points) Describe how to add the methods sample() and delrandom() to our binary heap implementation of the MinPQ API. The two methods return a key that is chosen uniformly at random among the remaining keys, with the latter method also removing that key. public class MinPQ<Key extends Comparable<Key>> MinPQ() create an empty priority queue void insert(key key) insert a key into the priority queue Key min() return the smallest key Key delmin() return and remove the smallest key Key sample() return a key that is chosen uniformly at random Key delrandom() return and remove a key that is chosen uniformly at random You should implement the sample() method in constant time and the delrandom() method in time proportional to log N, where N is the number of keys in the data structure. For simplicity, do not worry about resizing the underlying array. Your answer will be graded on correctness, efficiency, clarity, and conciseness. sample(): 85 delrandom():