CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Similar documents
CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

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

UNIVERSITY REGULATIONS

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

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

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

Final Exam Solutions PIC 10B, Spring 2016

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Final: CSS 342 SAMPLE. Data Structures, Algorithms, and Discrete Mathematics I

CSCE 2014 Final Exam Spring Version A

Absolute C++ Walter Savitch

COS 226 Algorithms and Data Structures Spring Midterm Exam

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Dynamic Data Structures

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

UNIVERSITY REGULATIONS

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

Computer Science II CSci 1200 Test 2 Overview and Practice

CS 216 Fall 2007 Midterm 1 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

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Midterm Exam 2 CS 455, Spring 2011

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

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

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

PRACTICE MIDTERM EXAM #2

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Solution to CSE 250 Final Exam

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

CS32 Final Exam. E03, S15, Phill Conrad, UC Santa Barbara Tuesday, 06/08/2015, 8am 11am

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

CSE 131 Introduction to Computer Science Fall Exam II

Templates and Vectors

COMP Data Structures

Data Structures CS 315 Spring 2015

CS 216 Exam 1 Fall SOLUTION

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

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Computer Science E-22 Practice Final Exam

Algorithmic Analysis and Sorting, Part Two

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

This examination has 11 pages. Check that you have a complete paper.

Section 1: True / False (2 points each, 30 pts total)

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Midterm Practice TA: Brian Choi Section Webpage:

COMP 524 Spring 2018 Midterm Thursday, March 1

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CSCI 102 Fall 2010 Exam #1

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

The University of British Columbia

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

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

CPSC 2380 Data Structures and Algorithms

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

Final Exam. Name: Student ID: Section: Signature:

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Science E-119 Practice Midterm

About this exam review

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

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

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

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

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

Part I: Short Answer (12 questions, 65 points total)

Computer Science II CSci 1200 Test 1 Overview and Practice

CSE 333 Final Exam 3/19/14

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

1 Short Answer (10 Points Each)

CS197c: Programming in C++

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Largest Online Community of VU Students

Data Structures Lecture 3 Order Notation and Recursion

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

(Refer Slide Time: 01.26)

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

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

+ Abstract Data Types

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

CS102 Sorting - Part 2

PIC 10B Discussion Week 7. Professor: Michael Lindstrom TA: Thomas Tu

Midterm Exam (REGULAR SECTION)

Project 1. due date Sunday July 8, 2018, 12:00 noon

Transcription:

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011 (12:30pm - 1:50pm, Thursday, March 24) Instructor: Bill Cheng ( This exam is closed book, closed notes, closed everything. No cheat sheet allowed. No calculators, cell phones, or any electronic gadgets. )

Print Your Name: Page 1 of 12 Signature: USC Username: CSCI 102L - Data Structures Midterm Exam #2 Spring 2011 Instructor: Bill Cheng Problems Problem #1 (12 points) Problem #2 (30 points) Problem #3 (16 points) Problem #4 (21 points) Problem #5 (24 points) Total (103 points) ( This exam is closed book, closed notes, closed everything. No cheat sheet allowed. No calculators, cell phones, or any electronic gadgets. To be eligible for regrades, you must write your entire exam in ink. )

Problem 1. Polymorphism (12 points) Page 2 of 12 Given the following partial declarations: class Car {... virtual void print() const { cout << "Car." << endl; virtual double getmpg() const { return 0; ; class Truck : Car (...) {... virtual void print() const { cout << "Truck." << endl; virtual double getmpg() const { return...; ; (a) (3 points) What would get printed by the following code? Truck a_truck; Car &c1 = a_truck; c1.print(); Car *c2 = &a_truck; c2->print(); Car c3 = a_truck; c3.print(); (b) (3 points) In the code at the top of this page, it s really not a good idea to return a zero as the MPG (Miles Per Gallon) for the Car class. The getmpg() member function should be a pure virtual function for the Car class. Please write the code to fix it (for the Car class only). (You do not have to worry about breaking the code in part (a).) (c) (3 points) Continuing from your answer to part (b), by making the getmpg() member function a pure virtual function for the Car class, (1) which is the first line of code in part (a) that will not compile (1 point) and (2) why (2 points)? (d) (3 points) What is the difference between an Abstract Base Class and an Interface Class?

Problem 2. Vector-backed Templated Stack (30 points) Page 3 of 12 In this problem, you will be implementing all the functions of a vector-backed templated stack class called MyStack. Below and on the left, you are given the class definition for MyStack: class MyStack { private: std::vector<t> v; public: MyStack() { ; ~MyStack() { ; int size() const; bool empty() const; class vector { public: int size() const; bool empty() const; void push_back(const T& value); void erase(iterator position); const T& at(const int index) const; ; void push(const T& value); void pop(); const T& top() const; void print() const; ; iterator end(); You must not alter the class definition of MyStack (nor can you add anything to it). Above and on the right, you were also given a partial class definition for a templated vector class. These are the only member functions of the vector class that you are allowed to use in this problem. If you call a member function of vector that s not in the above class definition for vector, you will not receive any credit for it. For example, push front() and operator[] are not available for you to use. You can assume that the vector class is implemented correctly. You cannot ask the exam proctors to explain anything about the above since they are covered in details in class. (a) (2 points) Please implement the size() member function for the MyStack class. int MyStack<T>::size() (b) (2 points) Please implement the empty() member function for the MyStack class. bool MyStack<T>::empty()

Problem 2. (cont...) Page 4 of 12 (c) (2 points) Please implement the push() member function for the MyStack class. void MyStack<T>::push(const T& value) (d) (8 points) Please implement the pop() member function for the MyStack class. (You do not have to use the namespace stuff.) Your implementation must work well with the following code that invokes pop(): MyStack<int> s; try { s.pop(); catch(logic_error(&err)) { cerr << err.what() << endl; If you are not familiar with the erase() function of the vector class, to the last element in a vector, the iterator you must pass to a vector v is v.end()-1. void MyStack<T>::pop()

Problem 2. (cont...) Page 5 of 12 (e) (8 points) Please implement the top() member function for the MyStack class. (You do not have to use the namespace stuff.) Your implementation must work well with the following code that invokes top(): MyStack<int> s; try { cout << "Top of an empty stack is: " << s.top() << endl; catch(logic_error(&err)) { cerr << err.what() << endl; const T& MyStack<T>::top() const (f) (8 points) Please implement the print() member function for the MyStack class to print out all the elements in the stack, starting from the top of the stack. (You do not have to use the namespace stuff.) You can assume that class T s << operator has been overloaded properly. You should print a space character between each element. Finally, if the stack is empty, you must print the word Empty. void MyStack<T>::print() const

Problem 3. Recursion (16 points) Page 6 of 12 You are given a vector v of positive integers. For convenience, let s assume that all the values in the vector are distinct. Let v[a..b] denote part of the vector v, starting at index position a and ending at index position b. Recall from lectures that the QuickSort code looks like the following: void quicksort(vector<int>& v, int start, int end) { if (start < end) { int pivot_idx=partition(v, start, end); quicksort(v, start, pivot_idx-1); quicksort(v, pivot_idx+1, end); void quicksorthelper(vector<int>& v) { quicksort(v, 0, v.size()-1); int partition(vector<int>& v, int start, int end) { int pivot = v[end]; int i = start-1; for (int j=start; j < end; j++) { if(v[j] <= pivot) { i++; swap(v[i],v[j]); i++; swap(v[i],v[end]); return i; In the above code, the partition(v,start,end) function first selects v[end] as the pivot and then rearranges v[start..end] into 3 parts: all the values less than the pivot value (i.e., the left part, v[start..pivot idx-1]), the pivot itself (i.e., the middle part, v[pivot idx..pivot idx]), and all the values greater than the pivot value (i.e., the right part, v[pivot idx+1..end]). It returns the index of the middle part. Then Quick Sort recursively sorts the left part and then the right part. Please note that you cannot ask the exam proctors to explain anything about the above since they are covered in details in class. Your job is to implement a function, kth smallest(v,k), which will return the k th smallest value in v where k is a zero-based index. For example, if v contains the values [24, 89, 73, 17, 31, 55, 64, 40], kth smallest(v,0) will return 17, kth smallest(v,5) will return 64, kth smallest(v,7) will return 89, and so on. You are given the following code (and the function prototype of a recursive function kth smallest(v,start,end,k)) as a starting point (you must not change this code): int kth_smallest(vector<int>& v, int start, int end, int k); int kth_smallest_helper(vector<int>& v, int k) { if (k < 0 k >= v.size()) { throw out_of_range("k is out of range."); return kth_smallest(v, 0, v.size()-1, k); Please note that you cannot ask the exam proctors to explain anything about the code above since that s what you need to figure out by exploring the similarities and differences between the recursive functions quicksort(v,start,end) and kth smallest(v,start,end,k).

Problem 3. (cont...) Page 7 of 12 (a) (12 points) Please implement the recursive function kth smallest(v,start,end,k). You must not sort the vector v (although you are allowed to partition the vector). You will get zero point if you sort the vector v or if you did not use recursion. (Hint: start with the recursive Quick Sort code. Also, you might want to think about part (b) below first.) int kth_smallest(vector<int>& v, int start, int end, int k) (b) (4 points) Continuing from your answer to part (a), please indicate which are the base cases of your code and which are the recursive cases of your code. You can either write the conditions in the space below or mark and label your code above. (Hint: there are two cases each.)

Problem 3. (cont...) Page 8 of 12 (c) (This problem intentionally left blank so you can use this page for whatever you want.)

Problem 4. Short Answers (21 points) Page 9 of 12 Using Big O notation, give the amount of time it takes to do the following operations in the absolute worst case and also briefly explain why and describe what the worst case is, if applicable. Just giving the Big O only give you 1 point. Your explanation is worth 2 points. You must not use average case analysis or amortized analysis. (a) (3 points) Calling push back on a vector that already contains N items. (b) (3 points) Append a node to the end of a doubly-linked list if the length of the list is less than 100. If the doubly-linked list is longer, insert the node right before the last node in the list. (Recall that a doubly-linked list object maintains the length of the list, a pointer to the head of the list, and another pointer to the tail of the list. Furthermore, each node in the doubly-linked list has a next and a prev pointer.) The length of the list is N. (c) (3 points) Quick Sort a sorted list of length N using the partition(v,start,end) function given in Problem 3.

Problem 4. (cont...) Page 10 of 12 (d) (3 points) Find a specific integer value (which may or may not be present) in a sorted vector of N integers using binary search. (e) (3 points) Selection Sort a sorted list of length N. (f) (3 points) Better Bubble Sort a sorted list of length N. (g) (3 points) Merge Sort a reverse sorted list of length N.

Problem 5. Sorting (24 points) Page 11 of 12 NOTE: There is NO CODE required for this problem. For all the sorting problems here, please sort the values from small to large and please use the following list as the starting point (i.e., the vector v): 73 24 64 31 40 89 17 55 For the Selection Sort algorithm, your answer might look something like this: 73 24 64 31 40 89 17 55 (start) 17 24 64 31 40 89 73 55 (swap 17 & 73) 17 24 64 31 40 89 73 55 (24 stays) 17 24 31 64 40 89 73 55 (swap 31 & 64) 17 24 31 40 64 89 73 55 (swap 40 & 64) etc. Unless otherwise specified, please use a style similar to the one above when you give your answers. Although you don t have to use exactly the same style as above, it is important to make sure that your answer is unambiguous. (a) (16 points) Show what happens when you call partition(v,0,7) where partition(v,start,end) is given in Problem 3 (which is the same as the one discussed in class). Please do not forget to give the return value. Show the order of the numbers in the list after each major step in the algorithm like in the example above. Please draw vertical partition bars to clearly indicate the 3 partitions created during the execution of the partition(v,start,end) function. Feel free to add additional text and/or drawings if you want to help explain what you are doing. You must use the partition(v,start,end) function given in Problem 3 to receive any credit here. 73 24 64 31 40 89 17 55 (start)

Problem 5. (cont...) Page 12 of 12 (b) (8 points) Sort the values using the Merge Sort algorithm. Make it clear that you understand what is going on in each phase of the algorithm. Feel free to add additional text and/or drawings if you want to help explain what you are doing. 73 24 64 31 40 89 17 55 (start)