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

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

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

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

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?

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

CS171 Midterm Exam. October 29, Name:

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.

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

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

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)

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

Data Structures and Algorithms

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

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

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

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

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

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

O(n): printing a list of n items to the screen, looking at each item once.

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

Sorting Algorithms Day 2 4/5/17

Short Notes of CS201

CSCE 2014 Final Exam Spring Version A

CS201 - Introduction to Programming Glossary By

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

(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

Computer Science E-22 Practice Final Exam

Recursive Search with Backtracking

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

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

CS 216 Exam 1 Fall SOLUTION

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

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

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

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

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

Allocation & Efficiency Generic Containers Notes on Assignment 5

Review for Test 1 (Chapter 1-5)

SORTING. Comparison of Quadratic Sorts

Lab 7 (50 pts) Due Sunday, May 20)

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Security Coding Module - Buffer Overflow Data Gone Wild CS1

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

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

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

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

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

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Dynamic Data Structures

BM267 - Introduction to Data Structures

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

L14 Quicksort and Performance Optimization

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Sorting and Selection

1. Write step by step code to delete element from existing Doubly Linked List. Suppose that all declarations are done

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

Example Final Questions Instructions

Programming II (CS300)

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Object Oriented Design

CS 221 Review. Mason Vail

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

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

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

CS103L SPRING 2017 UNIT 8: RECURSION

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

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

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

About this exam review

Faster Sorting Methods

Suppose that the following is from a correct C++ program:

Measuring algorithm efficiency

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

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

UNIVERSITY REGULATIONS

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

CMSC 341 Lecture 7 Lists

Summer Final Exam Review Session August 5, 2009

"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

Lecture 15a Persistent Memory & Shared Pointers

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

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Programming II (CS300)

ECE242 Data Structures and Algorithms Fall 2008

Midterm 1. CS Intermediate Data Structures and Algorithms. October 23, 2013

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

CS201 Latest Solved MCQs

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

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

COS 226 Midterm Exam, Spring 2009

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

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Real-world sorting (not on exam)

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Transcription:

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions The Midterm Exam was give in class on Wednesday, October 21, 2009. 1. [4 pts] When we define a (non-template) class, we generally use two kinds of files. Indicate what each of these files is called and what it is for. Header. Holds the class definition and prototypes of associated global function. Primarily for the interface of the class. Source. Holds definitions of member functions and global functions. Primarily for the implementation of the class. 2. [4 pts total] SRP. 2a. [2 pts] What is the Single Responsibility Principle? Every module should have exactly one well defined responsibility. 2b. [2 pts] Why is following SRP a good thing? It makes code easier to design, and easier to understand and maintain. It also improves errorhandling capability. 3. [4 pts] What important distinctions between arrays and Linked Lists do we need to consider when analyzing the efficiency of algorithms that deal with them? (Give at least two.) Arrays are random-access, while Linked Lists are not. That is, in an array one can quickly access an item by its index. In a Linked List, this is slow. Inserting and deleting at a given position is much faster in a Linked List than in an array. (exam page 2)

4. [6 pts total] The Big Three. 4a. [3 pts] What does the C++ Law of the Big Three state? If you declare one of the Big Three (copy constructor, copy assignment operator, destructor), then you should probably declare all of them. 4b. [3 pts] In what circumstances do we usually need to write the Big Three? We usually need to write the Big Three when we an object manages a resource. 5. [6 pts total] Order of Functions. Write the order of each function below, using big-o. 5a. [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). 5b. [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 3)

6. [6 pts total] Resource Ownership. 6a. [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. 6b. [3 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. A dynamic object. Destroying the object and freeing its memory. An open file. Flushing and closing the file. 7. [6 pts total] Recursion vs. Iteration. 7a. [4 pts] Recursion has a number of disadvantages, as contrasted with iteration. Give at least two of these. Here are three: Recursion has more function-call overhead. Recursion has poor error-handling in the case of stack overflow. Recursion can easily (but not always!) lead to highly inefficient algorithms. 7b. [2 pts] Under what circumstances is it very easy to convert recursive code to iterative form? When the code uses tail recursion. (exam page 4)

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. 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. 8e. [2 pts] Sort a Linked List. Big-O: O(n log n). In Words: Log-linear time. 9. [4 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. Prevent the error, using preconditions. Contain the error, by fixing it inside the function. (exam page 5)

10. [9 pts total] Properties of Algorithms. 10a. [3 pts] What does it mean for an algorithm to be scalable? An algorithm is scalable if it works well with increasingly large problems. 10b. [3 pts] What does it mean for an algorithm to be in-place? An algorithm is in-place if it does not require significant additional space, that is if is uses only constant additional space (beyond that required for its input). 10c. [3 pts] What does it mean for an algorithm to be stable? An algorithm that rearranges a list (e.g., a sorting algorithm) is stable if it never changes the relative order of equivalent items. 11. [4 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: Sorting a small list. Insertion Sort. Sorting a nearly sorted list. Insertion Sort. Sorting a Linked List. Merge Sort. (exam page 6)

12. [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. 12a. [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 Such an algorithm must read all of its input, which requires at least linear time. 12b. [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 This is nearly sorted data. Insertion Sort can do this. 12c. [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 that he has a linear-time general-purpose comparison sort. However, we know that such sorts always require at least log-linear time. (exam page 7)