COS 226 Algorithms and Data Structures Fall Midterm

Similar documents
COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Fall Midterm

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

Midterm, Spring 2013

COS 226 Algorithms and Data Structures Spring Midterm Exam

COS 226 Midterm Exam, Spring 2011

COS 126 General Computer Science Spring Written Exam 1

COS 226 Algorithms and Data Structures Fall Final

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

COS 226 Algorithms and Data Structures Fall Midterm Solutions

COMP 250 Midterm #2 March 11 th 2013

COS 226 Algorithms and Data Structures Fall Final

COS 226 Algorithms and Data Structures Practice Design Questions

COS 226 Algorithms and Data Structures Spring Second Written Exam

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

COS 226 Algorithms and Data Structures Spring Final

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

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

COS 226 Algorithms and Data Structures Fall Final Exam

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

COS 226 Midterm Exam, Spring 2010

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

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

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

CS 216 Exam 1 Fall SOLUTION

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

Course Review for Finals. Cpt S 223 Fall 2008

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

CS 61B Spring 2017 Guerrilla Section 4 Worksheet. 11 March 2017

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

cs2010: algorithms and data structures

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Computer Science 136 Exam 2

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

CS171 Midterm Exam. October 29, Name:

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

COMP 103. Data Structures and Algorithms

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Course Review. Cpt S 223 Fall 2009

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

Name CPTR246 Spring '17 (100 total points) Exam 3

Largest Online Community of VU Students

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

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

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

UNIVERSITY REGULATIONS

Summer Final Exam Review Session August 5, 2009

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

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

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

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

Randomized Queues and Deques

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

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

ECE250: Algorithms and Data Structures Midterm Review

COP 3502 Spring 2018 Study Union Review

Balanced Binary Search Trees. Victor Gao

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

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

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

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

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Data Structures Question Bank Multiple Choice

Computer Science E-22 Practice Final Exam

Basic Data Structures (Version 7) Name:

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

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

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

ECE 250 Algorithms and Data Structures

182 review 1. Course Goals

) $ f ( n) " %( g( n)

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

Computer Science Foundation Exam

COS 126 General Computer Science Spring Written Exam 2

Data Structure (CS301)

( ) n 3. n 2 ( ) D. Ο

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

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

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

COS 226 Midterm Fall 2007

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

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

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

Transcription:

COS 226 Algorithms and Data Structures Fall 2017 Midterm This exam has 10 questions (including question 0) worth a total of 55 points. You have 0 minutes. This exam is preprocessed by a computer, so please write darkly and write your answers inside the designated spaces. Policies. The exam is closed book, except that you are allowed to use a one page cheatsheet (.5-by-11 paper, one side, in your own handwriting). No electronic devices are permitted. Discussing this exam. Discussing the contents of this exam before solutions have been posted is a violation of the Honor Code. This exam. Do not remove this exam from this room. Write your name, NetID, and the room in which you are taking the exam in the space below. Mark your precept number. Also, write and sign the Honor Code pledge. You may fill in this information now. Name: NetID: Exam room: Precept: P01 P02 P0 P0A P04 P05 P06 I pledge my honor that I will not violate the Honor Code during this examination. Signature

2 PRINCETON UNIVERSITY 0. Initialization. (1 point) In the space provided on the front of the exam, write your name, NetID, and the room in which you are taking the exam; mark your precept number; and write and sign the Honor Code pledge. 1. Memory and data structures. (4 points) Suppose that you implement a weighted quick-union data type using the following parent-link representation: public class WeightedQuickUnion { private final int n; private final Node[] nodes; private static class Node { private int size = 1; private Node parent = null; // number of elements // array of n nodes // subtree size // parent link // construct a weighted quick-union data structure with n elements public WeightedQuickUnion(int n) { this.n = n; this.nodes = new Node[n];... Using the 64-bit memory cost model from lecture and the textbook, how much memory does a WeightedQuickUnion object use as a function of the number of elements n? Use tilde notation to simplify your answer. bytes

COS 226 MIDTERM, FALL 2017 2. Five sorting algorithms. (5 points) The column on the left is the original input of strings to be sorted; 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 five sorting algorithms listed below. Match each algorithm by writing its number in the box under the corresponding column. Use each number exactly once. 0 prim heap flip edge flip miss edge 1 left left heap find left load find 2 load load java flip load loop flip push lazy left hash loop list hash 4 sink node load heap miss left heap 5 time hash loop java null lazy java 6 loop loop miss lazy prim heap lazy 7 flip flip null left push java left null null prim list sink flip list 9 miss miss push load swim hash load 10 trie edge rank loop time edge loop 11 swim find sink miss trie find miss 12 java java sort time find node node 1 sort list swim sort heap null null 14 rank prim time rank java prim prim 15 heap rank trie sink list push push 16 list sort list null rank rank rank 17 find swim find swim sort sink sink 1 tree tree tree tree tree sort sort 19 edge trie edge prim edge swim swim 20 hash time hash push hash time time 21 node sink node node node tree tree 22 lazy push lazy trie lazy trie trie 2 type type type type type type type 0 6 (0) Original input (1) Selection sort (2) Insertion sort () Mergesort (top-down) (4) Heapsort (5) Quicksort (standard, no shuffle) (6) Sorted

4 PRINCETON UNIVERSITY. Analysis of algorithms. (6 points) Consider an array that contains two successive copies of the integers 1 through n, in ascending order. For example, here is the array when n = : 1 2 4 5 6 7 1 2 4 5 6 7 Note that the length of the array is 2n, not n. (a) How many compares does selection sort make to sort the array as a function of n? Use tilde notation to simplify your answer. compares (b) How many compares does insertion sort make to sort the array as a function of n? Use tilde notation to simplify your answer. compares (c) How many compares does mergesort make to sort the array as a function of n? Assume n is a power of 2. Use tilde notation to simplify your answer. compares

color flip COS 226 MIDTERM, FALL 2017 5 Midterm, Fall 2014 4. Red black BSTs. (6 points) Suppose that you insert the key 1 into the following left-leaning red black BST: red link 1 12 2 6 16 22 2 4 10 14 20 26 0 2 24 Which of the following color flips and rotations result? Mark all that apply. Color flips: Midterm, Spring 2015 1 22 26 2 0 1 2 Rotations: rotate right rotate left T 1 left 1 right 2 left 2 right 0 left 0 right 2 left 2 right left right T1 T1 T2 T2 T Examples of color flips and rotations (for reference): color flip 1 1 T1 T2 T T4 Midterm, Spring 2015 T1 T2 T T4 rotate right rotate left T T1 T1 T2 T2 T

6 PRINCETON UNIVERSITY 5. Hash tables. (5 points) Suppose that the following keys are inserted into an initially empty linear-probing hash table, but not necessarily in the order given, key hash B G 5 I N 5 O 1 P 5 R 4 and it results in the following hash table: 0 1 2 4 5 6 P R O B I N G For each description at left, mark all keys that apply. Assume that the initial size of the hash table is 7 and that it neither grows nor shrinks. B G N O R could have been first key inserted could have been last key inserted must have been inserted before I must have been inserted after I

COS 226 MIDTERM, FALL 2017 7 6. Programming assignments. (6 points) Answer the following questions about the COS 226 programming assignments. (a) Consider implementing Percolation using a union find implementation that supports union and find in m time per operation on a set of m elements. What is the order of growth of the running time to perform one percolation experiment (open random sites until the system percolates) on an n-by-n system? Mark the best answer. n 1/2 n n /2 n 2 n 5/2 n n 7/2 n 4 (b) Consider implementing a Deque using a singly linked list, storing the first item in the deque in the first node in the linked list (and the last item in the deque in the last node). Which of the following operations could be implemented to run in constant time in the worst case? Mark all that apply. addfirst() addlast() removefirst() removelast() isempty() (c) Consider nearest-neighbor search in a 2d tree with n 1 points. Which of the following are features of the algorithm and pruning rule specified in the assignment? Mark all that apply. Guarantees to return a nearest neighbor. Guarantees log n time per operation in the worst case. Guarantees log n time per operation in the worst case if the 2d tree is balanced. Achieves log n time per operation on inputs likely to arise in practice.

PRINCETON UNIVERSITY 7. Data structure and algorithm properties. (6 points) Match each quantity on the left by writing the letter of the best matching term at right. You may use each letter more than once or not at all. Assume each algorithm is the standard version, presented in the textbook. Maximum number of key compares to binary search for a key in a sorted array. A. constant B. 1 2 log 2 n Maximum number of key compares to perform a Delete- Max operation in a binary heap containing n keys. C. log n D. log e n E. log 2 n Minimum height of a weighted quick-union tree with n elements. F. 2 log e n G. 2 log 2 n Minimum height of a binary search tree with n keys. H. 4.11 log e n I. linear Minimum number of black links on path from the root to a null link in a left-leaning red black BST containing n keys. Maximum number of times an array can be resized (doubled or halved) during a sequence of n push and pop operations (starting from an empty data structure) in a resizing-array implementation of a stack.

COS 226 MIDTERM, FALL 2017 9. Largest bandwidth. ( points) Given n time intervals (l i, r i ) and associated bandwidths b i > 0, the bandwidth demand at time t is the sum of the bandwidths of all intervals that contain t. Design an algorithm to find a time t that has the largest bandwidth demand. InMidterm the example Spring below, 2017: there are n = 7 time intervals. The largest bandwidth demand is 29 and occurs at time t Maximum Bandwidth = 14.5: the bandwidth of the interval (12, 15) is 10 and the bandwidth of the interval (14, 20) is 19. bandwidth = 1 + 5 + 6 = 24 (t = 5.5) max bandwidth = 10 + 19 = 29 (t* = 14.5) 12 bandwidth of interval 6 5 19 1 10 time 0 1 2 4 5 6 7 9 10 11 12 1 14 15 16 17 1 19 20 Give a crisp and concise English description of your algorithm in the space below. Your answer will be graded for correctness, efficiency, and clarity. For full credit, the worstcase running time must be proportional to n log n. You may assume that all of the interval endpoints are distinct. 42

10 PRINCETON UNIVERSITY 9. Data structure design. ( points) Midterm Spring 2017: Uni-Queue Create a data type UniQueue that implements a FIFO queue of strings with no duplicates, according to the following API: public class UniQueue UniQueue() void enqueue(string s) String dequeue() create an empty uni-queue add the string to the uni-queue (if it is not already in the uni-queue) remove and return the string least recently added to the uni-queue boolean isempty() is the uni-queue empty? That is, if a duplicate key is added to the uni-queue, it is ignored. Here is an example: UniQueue queue = new UniQueue(); queue.enqueue("ant"); // [ "ant" ] queue.enqueue("bear"); // [ "ant", "bear" ] queue.enqueue("cat"); // [ "ant", "bear", "cat" ] queue.enqueue("bear"); // [ "ant", "bear", "cat" ] queue.enqueue("dog"); // [ "ant", "bear", "cat", "dog" ] queue.dequeue(); // [ "bear", "cat", "dog" ] queue.dequeue(); // [ "cat", "dog" ] queue.enqueue("bear"); // [ "cat", "dog", "bear" ] Your answer will be graded for correctness, efficiency, and clarity. (a) Declare the instance variables for your UniQueue data type. You may declare nested classes or use any of the data types that we have considered in this course. 4 public class UniQueue {

COS 226 MIDTERM, FALL 2017 11 (b) Briefly describe how to implement enqueue() and dequeue(), using either crisp and concise prose or code. public void enqueue(string s) { Assume that the argument to enqueue() is not null. public String dequeue() { Do not worry about underflow. (c) What is the order of growth of each operation as a function of the number of items n in the data structure? Mark whether it is an amortized bound and/or makes the uniform hashing assumption. operation order of growth amortized bound uniform hashing assumption enqueue() dequeue()