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

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

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Mergesort again. 1. Split the list into two equal parts

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Sorting Algorithms Day 2 4/5/17

Data Structures and Algorithms

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

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

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

CSCE 2014 Final Exam Spring Version A

! 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)

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)

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.

Real-world sorting (not on exam)

Faster Sorting Methods

Mergesort again. 1. Split the list into two equal parts

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

CS171 Midterm Exam. October 29, Name:

! 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)

CS 411 Analysis of Algorithms, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, October 17, 2012.

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

! 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)

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

CMSC 341 Lecture 7 Lists

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Tables, Priority Queues, Heaps

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

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

COS 226 Midterm Exam, Spring 2009

Short Notes of CS201

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

SORTING AND SEARCHING

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS201 - Introduction to Programming Glossary By

CS250 Final Review Questions

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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 315 Data Structures Spring 2012 Final examination Total Points: 80

Measuring algorithm efficiency

Data Structure (CS301)

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Computer Science E-22 Practice Final Exam

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

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

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

Lesson 12 - Operator Overloading Customising Operators

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

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

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

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

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

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

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

#101 Page: 1 Name: CS32 Midterm Exam. E01, W15, Phill Conrad, UC Santa Barbara

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

CS103L SPRING 2017 UNIT 8: RECURSION

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Arrays. Week 4. Assylbek Jumagaliyev

CMSC 341 Lecture 10 Binary Search Trees

CS 221 Review. Mason Vail

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

CS250 Final Review Questions

CS 115 Exam 3, Spring 2010

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

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING ADVANCED DATA STRUCTURES AND ALGORITHMS EXAM EXAMINATION JUNE 2014

Introduction to the Analysis of Algorithms. Algorithm

FORM 2 (Please put your name and form # on the scantron!!!!)

BM267 - Introduction to Data Structures

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Security Coding Module - Buffer Overflow Data Gone Wild CS1

CS 163 Practice Final Exam Winter 2012

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

CS 115 Exam 3, Spring 2011

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Lecture 11. Lecture 11: External Sorting

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

Sorting Pearson Education, Inc. All rights reserved.

Midterm Exam 2 CS 455, Spring 2011

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

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

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

1 / 20 2 / 20 3 / 20 4 / 20 5 / 20

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

CS 216 Exam 1 Fall SOLUTION

Transcription:

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions The Midterm Exam was given in class on Wednesday, March 18, 2009. 1. [4 pts] Parameter Passing in C++. In the table below, the names of the three ways to pass parameters in C++ are written in the first row. In the rest of the table, circle YES or NO to answer the questions for each parameter-passing method. BY VALUE BY REFERENCE BY REFERENCE- TO-CONST Does this method make a copy of the object passed? YES / NO YES / NO YES / NO allow const objects to be passed? YES / NO YES / NO YES / NO allow for polymorphism/virtual dispatch? allow implicit type conversions to be performed? YES / NO YES / NO YES / NO YES / NO YES / NO YES / NO 2. [6 pts total] Properties of Algorithms. 2a. [2 pts] What does it mean for an algorithm to be scalable? An algorithm is scalable if it works well with large (and ever larger) problems. 2b. [2 pts] What does it mean for an algorithm to be in-place? An algorithm is in-place if its space usage, beyond that required for its input, is at most some fixed constant. More concisely, an algorithm is in-place if its additional space usage is O(1). 2c. [2 pts] What does it mean for an algorithm to be stable? An algorithm that reorders a list is stable if it never changes the relative order of equivalent items, that is, if it never changes the relative order of items unnecessarily. Exam page 2

3. [6 pts total] Recursion vs. Iteration. 3a. [4 pts] Recursion has a number of disadvantages, as contrasted with iteration. Give at least two of these. Here are three. Function-call overhead. Memory management with poor error-recovery ability. The inherent, and often non-obvious, inefficiency of many (but not all!) recursive algorithms. 3b. [2 pts] Under what circumstances is it very easy to convert recursive code to iterative form? When the code is tail recursive, that is, when the recursive call is the last operation that a function performs. 4. [6 pts total] Linked Lists vs. Arrays. 4a. [3 pts] Give one advantage of a Linked List, as compared to an array. Here are two. A Linked List has much faster insert and remove (by iterator, or at the beginning) operations than an array. These are generally O(1) for a Linked List, but O(n) for an array. A Linked List allows for more flexible memory management than an array. It does not require large contiguous memory blocks, and needs no preallocation. 4b. [3 pts] Give one disadvantage of a Linked List, as compared to an array. Here are two. A Linked List has a much slower look-up by index operation than an array. This operation is O(n) for a Linked List, but O(1) for an array. A Linked List uses more memory than an array. Exam page 3

5. [6 pts total] The Big Three. 5a. [3 pts] What does the C++ Law of the Big Three state? The Law of the Big Three states that, if you declare one of the Big Three (copy constructor, copy assignment operator, destructor) in a class, then you generally should declare all of them. 5b. [3 pts] In what circumstances do we usually need to write the Big Three? When we need to write the Big Three for a class, it is generally because an object of the class owns some resource. 6. [6 pts total] Order of Functions. Write the order of each function, using big-o. 6a. [3 pts] void func_a(int arr[], int n) // n is size of arr { cout << "Item 0 = " << arr[0] << endl; cout << "Item 1 = " << arr[1] << endl; cout << "Item 2 = " << arr[2] << endl; } Order (big-o): O(1). 6b. [3 pts] void func_b(int arr[], int n) // n is size of arr { for (int a = 0; a < n; ++a) for (int b = 0; b < a; ++b) for (int c = 0; c < b; ++c) arr[c] += arr[b]; } Order (big-o): O(n 3 ). Exam page 4

7. [7 pts total] Resource Ownership. 7a. [3 pts] In the context of this class, what does it mean to own a resource? To own a resource is to be responsible for releasing it. 7b. [4 pts] Give two examples of resources that might be owned, and what it means to release each one. Here are three. Dynamically allocated memory: freeing the memory. An open file: flushing and closing the file. Various synchronization primitives (mutexes, etc.): freeing and returning to the system for reuse. 8. [10 pts total] Order of Operations. In each part, indicate the order of the given operation, using big-o and, based on this, in words. Assume that a good algorithm is used, within the constraints given. Use n to denote the size of the input. 8a. [2 pts] Find an item with a given value in a sorted array. Big-O: O(log n). In Words: Logarithmic time. [Use Binary Search.] 8b. [2 pts] Print the middle item in an array of known size. Big-O: O(1). In Words: Constant time. 8c. [2 pts] Print the middle item in a Linked List of known size. Big-O: O(n). In Words: Linear time. 8d. [2 pts] Sort an array with Quicksort. Big-O: O(n 2 ). In Words: Quadratic time. [Remember: Worst case.] 8e. [2 pts] Sort a Linked List. Big-O: O(n log n). In Words: Log-linear time. [Use Merge Sort.] Exam page 5

9. [6 pts] The Introsort algorithm is, for many purposes, the fastest sort known. However, in certain situations, some other algorithm is faster. List two such situations. For each, say which sorting algorithm would be faster in that situation. Here are three. When sorting nearly sorted data. Use Insertion Sort. When a stable sort is required. Use Merge Sort. When sorting a Linked List. Use Merge Sort. 10. [6 pts] One strategy for dealing with a possible error condition in a function is to signal the client code that an error has occurred. What are two other strategies? Hint: These must not involve signaling the client code. Prevention: Requiring client code to ensure that an error condition does not occur. Containment: Handling the error condition entirely inside the function. Exam page 6

11. [12 pts total] Impossible? Your friend Egbert is known for making wild claims. In each part, Egbert claims he has a new algorithm. Indicate whether Egbert claim is possible or impossible (circle one). If it is possible, explain how to do it. If impossible, explain why. 9a. [4 pts] Egbert says, I have a new algorithm that can sort a list in linear time, assuming that each item s position is no more than 1000 away from its position in the final sorted list. POSSIBLE IMPOSSIBLE Insertion Sort can do this. 1000 is a constant, and so this is nearly sorted data. 9b. [4 pts] Egbert says, I have a new algorithm that can find the largest number in any given unsorted array, in logarithmic time. POSSIBLE IMPOSSIBLE To find the largest number in an arbitrary unsorted array requires looking at every item in the array. Since Egbert s algorithm is required to read all of its input, it can be no faster than linear time. 9c. [4 pts] Egbert says, I have a new algorithm that can sort any list in linear time, given only an appropriate comparison function. POSSIBLE IMPOSSIBLE Egbert is saying he has a linear-time general-purpose comparison sort. But we know that is impossible; a general-purpose comparison sort must do Ω(n log n) comparisons, in the worst case, and so cannot be linear time. Exam page 7