Practice Midterm Exam Solutions

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

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

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

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

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

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

Section 05: Solutions

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

CSE wi: Practice Midterm

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Section 05: Midterm Review

Section 05: Solutions

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

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE332 Summer 2010: Midterm Exam Sample Solutions

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

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

Final Examination CSE 100 UCSD (Practice)

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

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

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

ECE250: Algorithms and Data Structures Midterm Review

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

CSE373 Winter 2014, Midterm Examination January 29, 2014

Course Review. Cpt S 223 Fall 2010

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Course Review. Cpt S 223 Fall 2009

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

Quiz 1 Practice Problems

Course Review for Finals. Cpt S 223 Fall 2008

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

UNIVERSITY REGULATIONS

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

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

Second Examination Solution

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

Quiz 1 Practice Problems

CS 245 Midterm Exam Winter 2014

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

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

UNIT III BALANCED SEARCH TREES AND INDEXING

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

Priority Queues and Heaps

Solutions to Exam Data structures (X and NV)

Data Structures Question Bank Multiple Choice

Midterm solutions. n f 3 (n) = 3

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

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

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

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

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

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

CS 315 Data Structures mid-term 2

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

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

Spring 2002: Fundamental Structures of Computer Science

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

Basic Data Structures (Version 7) Name:

1. Attempt any three of the following: 15

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

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CSci 231 Final Review

1. AVL Trees (10 Points)

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

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

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

COS 226 Algorithms and Data Structures Fall Midterm

Class Note #02. [Overall Information] [During the Lecture]

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

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

CS 303 Design and Analysis of Algorithms

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CMSC 341 Lecture 14: Priority Queues, Heaps

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1)

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Computer Science Foundation Exam

CSE 331 Introduction to Algorithm Analysis and Design. Sample Mid-term Exam-I: Fall 2018

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

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

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

6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

CSCE 2014 Final Exam Spring Version A

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

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

CSE 247 Data Structures and Algorithms Fall Exam I

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Transcription:

CSE 332: Data Abstractions Autumn 2015 Practice Midterm Exam Solutions Name: Sample Solutions ID #: 1234567 TA: The Best Section: A9 INSTRUCTIONS: You have 50 minutes to complete the exam. The exam is closed book. You may not use cell phones or calculators. All answers you want graded should be written on the exam paper. If you need extra space, use the back of a page. The problems are of varying difficulty. If you get stuck on a problem, move on and come back to it later. It is to your advantage to read all the problems before beginning the exam. Problem Points Score Problem Points Score 1 8 6 15 2 3 7 8 3 3 8 15 4 6 9 10 5 6 10 10 Σ 84 Page 1 of 11

One Liners. This section has questions that require very short answers. To get full credit, you should answer in no more than one sentence per question. 1. X Marks The Spot [8 points] For each of the following rows, circle each option on the right that is true for the function on the left and X each option that is false for the function on the left. n i=0 i 2 ////// O(n) //////// O(n 2 ) O(n 3 ) O(n 4 ) 3 n O(3 n ) ////////// O(3 n/2 ) Ω(3 n ) Ω(3 n/2 ) 10000n 25 ////// O(n) O(n 26 ) Ω(n) ///////// Ω(n 26 ) log n O(log n) O(log ////////////// log n) Ω(log n) Ω(log log n) n n ////////////// O(20000 n ) Ω(20000 n ) ///////////// Θ(20000 n ) Θ(n n + 20000 n ) Page 2 of 11

2. find The Error [3 points] Consider a dictionary implemented using a sorted array. Consider the following argument: We claim that find() is amortized O(1) in this implementation. Consider a sequence of lg(n) operations. Each operation takes lg(n) time; so, the amortized cost of each operation is lg(n) lg(n) = O(1). This argument is faulty. Explain why it s faulty and give a correct (tight) asymptotic bound for the actual amortized cost. Solution: This argument is faulty because it is using an incorrect formula to compute the amortized cost. The argumnt is using the formula: However, the correct formula is: Time per operation Number of operations Amortized cost Cost of first (usually expensive) operation + Cost of next X - 1 operations Amortized cost X... where X is the number of operations. Using this formula, and if we let X = lg n (which, by the way, is a very weird amount of operations to do, we can see that the amortized cost be: lg n + (lg n 1) lg n lg n = (lg n)2 lg n O(lg n) 3. It s Olementary, My Dear Watson [3 points] Let f : N N and g : N N be increasing functions with f(n) g(n) for any n N. Consider the statement: f(n) Ω(g(n)) and g(n) Ω(f(n)) Is this statement always, sometimes, or never true? Explain your answer in one sentence. Solution: This statement is sometimes true. For example, the statement is true if f(n) = 2n and g(n) = 3n, but is false if f(n) = 1 and g(n) = n. Page 3 of 11

4. The-ta Knows Best! [6 points] For each of the following, give a Θ( ) bound, in terms of n, for the worst case runtime of the method. (a) (2 points) 1 int hello(int n) { 2 if (n == 0) { 3 return 0; 4 } 5 for (int i = 0; i < n; i++) { 6 for (int j = 0; j < n n; j++) { 7 System.out.println("HELLO"); 8 } 9 } 10 return hello(n 1); 11 } Θ(n 4 ) (b) (2 points) 1 void whee(int n) { 2 for (int i = 1; i < n; i = 2) { 3 for (int j = 1; j < n; j = 3) { 4 System.out.println("WHEE!"); 5 } 6 } 7 for (int k = n/2; k < n; k++) { 8 System.out.println("WOAH!"); 9 } 10 } Θ(n) (c) (2 points) 1 void flipflop(int n, int sum) { 2 if (n > 10000) { 3 for (int i = 0; i < n n n; i++) { 4 sum++; 5 } 6 } 7 else { 8 for (int i = 0; i < n n n n; i++) { 9 sum++; 10 } 11 } 12 } Θ(n 3 ) Page 4 of 11

5. Analysis [6 points] For each of the following, determine the runtime of the algorithm/operation: (a) (2 points) Worst case find in a B-Tree O(lg(L) + lg(m) log M n) (b) (4 points) Best case insert in a hash table with size n and a current λ = 1 if the collision resolution strategy is: Separate Chaining O(1) Double Hashing O(n) Page 5 of 11

6. Choose The Data Structure [15 points] For each of the following, decide which data structure is most appropriate to solve the problem: AVL Tree B-Tree Bit Set FIFO Queue Hash Table Linked List Heap Stack MTF List Vanilla BST (a) (3 points) An operating system needs to schedule when to run each of the current processes. Solution: We should use a heap. Processes in an operating system have different priorities, and we should service them in order of priority. (b) (3 points) You want to store 1MB of non-comparable data and you expect to run the find operation very frequently. Solution: We should use a hash table. We can t use any of the trees, because the data is not comparable. Since find is a frequent operation, we want it to be O(1). We don t know anything about the keys other than that they re comparable; so, we can t necessarily use a trie or a bitset. (c) (3 points) You want to keep track of ASCII characters that are allowed in a valid password. Solution: We should use a bit set. There are very few ASCII characters, and most of them are likely to be allowed in a password. So, the bitset is going to be dense and the keys are ints. (d) (3 points) Google (which has petabytes of data) wants to store search queries in such a way that they can easily find the closest alphabetical query to a new user query. Solution: We should use a B-Tree. Since there is a lot of data, we expect disk accesses to matter; so, a B-Tree is the right choice. (e) (3 points) A grocery store wants to make a database of their products (they only have about 5000 unique products) so that employees can look up the aisle that a particular product is in by name. Solution: If we assume that the products are listed by id number, then we should use a bitset (because there are very few products). Otherwise, we should use a hash table, because we want as fast a lookup as possible. Page 6 of 11

Basic Techniques. This part will test your ability to apply techniques that have been explicitly identified in lecture and reinforced through sections and homeworks. Remember to show your work and justify your claims. 7. AVL Trees [8 points] Insert 1, 2, 7, 6, 8, 3, 4, 5 into an AVL tree in that order. You do not have to show your intermediary steps, but no work and a wrong answer will receive no credit. Solution: 6 2 7 1 4 8 3 5 Page 7 of 11

8. Hashing [15 points] You know in advance that you will never put more than six strings into a particular hash table. (a) (2 points) If you had a choice between 6 and 7 as your table size, which would you choose? Why? Solution: We should choose 7. Despite knowing that there is a maximum of 6 strings, we don t know precisely what hashing function those strings will end up using. Therefore, to minimize the chances of clustering, we should make the table size a prime number. (b) (3 points) Insert 1, 5, 9, 12, 37, 16 into your hash table using the hash function h(x) = x and separate chaining. Solution: 1 9 37 16 5 12 T[0] T[1] T[2] T[3] T[4] T[5] T[6] (c) (2 points) What is the load factor of your hash table from part (b)? Solution: The load factor is λ = num entries num buckets = 6 7 (d) (3 points) Insert the same numbers into an empty version of your hash table using the hash function h(x) = x and quadratic probing. Solution: 1 9 37 16 5 12 T[0] T[1] T[2] T[3] T[4] T[5] T[6] (e) (3 points) Does your hash table from part (d) have primary clustering? clustering? What about secondary Solution: It arguably has both primary clustering and secondary clustering. We can see this, because when we insert 16, we go through spots that have several distinct hash codes. Note that, in general, we do not expect to have primary clustering when we use quadratic probing, but, for example, if the hash table has λ = 1, there must be both primary and secondary clustering. (f) (2 points) What is the load factor of your hash table from part (d)? Solution: The load factor is λ = 6 7. Page 8 of 11

9. B-Trees [10 points] Consider the following B-Tree : 01 03 07 05 9 11 13 15 (a) (2 points) What are M = 4 and L = 2? (b) (5 points) List (and fix, if possible) everything that you can find that is wrong with the above B-Tree. Solution: (a) 5 and 7 should be swapped in the second leaf (b) The leaves containing 9 and 11 should be consolidated; the leaves containing 13 and 15 should be consolidated (c) The inner nodes are missing the values (left-most inner node should have 5 in the first entry, the right-most inner node should have 13 in the first entry, and the root node should have 9 in the first entry) (c) (3 points) Suppose that you know the following facts about a computer system: 1 page on disk is 1024 bytes Keys are 4 bytes Pointers are 8 bytes Key/Value Pairs are 32 bytes If you want to use a B-Tree on this system, what should you choose M and L to be? Solution: Let p = 1024 be the page size, k = 4 be the key size, t = 8 be the pointer size, and e = 32 be the leaf entry size. We also know that p el and that p tm + k(m 1). We can solve the left-most equation to obtain L = 32. We can solve the right-most equation to obtain p+k 85.67 M. We round down k+t to obtain M = 85. Page 9 of 11

A Moment s Thought! This section tests your ability to think a little bit more insightfully. The approaches necessary to solve these problems may not be immediately obvious. Remember to show your work and justify your claims. 10. Treaps! [10 points] In this problem, we define and analyze a new data structure called a Treap. A treap is a combination of a BST (tre-) and a heap (-eap). Operations on treaps are defined as follows: void insert(n) Generate a random priority p. Do a standard BST insert into the treap of (n, p) completely ignoring the priority. Fix the Treap so that the keys follow the BST Property and the priorities follow the Min-Heap Property. (Note that the treap does not have to obey the Heap STRUCTURE Property.) (a) (6 points) Suppose that we have a random number generator that will generate the following sequence of random numbers: 5, 3, 7, 2, 4,... Insert c, b, e, q, h into a treap in that order. Hint: After doing each BST insert, use AVL Rotations on the inserted element until the Heap Property is satisfied. Solution: q, 2 b, 3 h, 4 c, 5 e, 7 Is your resulting treap an AVL tree? Why or why not? Solution: It is not an AVL tree, because it is not balanced. Page 10 of 11

(b) (4 points) Suppose we implement deletemin on a treap as follows (where delete is defined with lazy deletion): 1 int deletemin() { 2 min = findminkey() 3 return delete(min); 4 } What is the best case runtime of this operation? Solution: The best case runtime is O(1). Consider the case where the minimum key also has the minimum priority. There would be no nodes to the left of the root, so we would know to return the root immediately. What is the worst case runtime of this operation? Solution: The worst case runtime is O(n). Consider the case where the minimum key also has the largest priority, and the tree is pathological (is essentially a linked list). We might potentially need to traverse through the entire treap to find the min key. Page 11 of 11