Practice test for Midterm 1; solutions

Similar documents
Chapter 9 STACK, QUEUE

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)

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

CMPT 125: Practice Midterm Answer Key

Abstract Data Types 1

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

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

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

Abstract Data Types 1

Dynamic Data Structures

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS Data Structure Spring Answer Key- Assignment #3

CSCE 2014 Final Exam Spring Version A

! 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 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

CS 216 Exam 1 Fall SOLUTION

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

1 Short Answer (7 Points Each)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Review Questions for Final Exam

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Queue Implementations

PRACTICE MIDTERM EXAM #2

Linked Structures. See Section 3.2 of the text.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

CS171 Midterm Exam. October 29, Name:

Data Structure Advanced

Practice test for midterm 2

811312A Data Structures and Algorithms, , Exercise 1 Solutions

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

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

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

List, Stack, and Queues

Final Exam Solutions PIC 10B, Spring 2016

CIS Fall Data Structures Midterm exam 10/16/2012

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

! A data type for which: ! An ADT may be implemented using various. ! Examples:

csci 210: Data Structures Stacks and Queues

CSCE 110 PROGRAMMING FUNDAMENTALS

Data Structure. Recitation VII

Linked List using a Sentinel

Midterm 3 practice problems

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

Chapter 7: Stacks. Exercises 7.2

Practice test for midterm 1

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

Computer Science 62. Midterm Examination

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Largest Online Community of VU Students

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

CS 261: Data Structures. Dynamic Array Queue

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Q2: Write an algorithm to merge two sorted arrays into a third array? (10 Marks) Ans Q4:

Cpt S 122 Data Structures. Data Structures

Solution for Data Structure

CS350 - Exam 1 (100 Points)

Data Structures and Algorithms

UNIVERSITY REGULATIONS

Exam 3 Chapters 7 & 9

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Apply to be a Meiklejohn! tinyurl.com/meikapply

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Analysis of Algorithms. CS 1037a Topic 13

CS24 Week 4 Lecture 2

THE UNIVERSITY OF WESTERN AUSTRALIA

CS32 Discussion Week 3

CMSC 341 Lecture 7 Lists

Lists, Stacks, and Queues

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

1 P age DS & OOPS / UNIT II

Object Oriented Programming COP3330 / CGS5409

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Data Structures and Algorithms

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

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Queues. Queue ADT Queue Implementation Priority Queues

Largest Online Community of VU Students

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

CS103L SPRING 2017 UNIT 8: RECURSION

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Transcription:

Practice test for Midterm 1; solutions November 1, 2 1 1 C++ review & UML diagrams Write a function which takes a vector of int and returns true if all the elements of the vector are positive ( 0) and false otherwise: bool all_pos(vector<int> data) { for(int i = 0; i < data.size(); ++i) if(data[i] < 0) return false; return true; Draw the UML class diagram for the following set of classes: class engine { int cylinders; ; class car { int milage; engine e; ; class ford : public car { bool is_huge_pickup; ; 1

engine + cylinders : int 1 1 car + milage : int + e : engine ford + is_huge_pickup : bool What does the following function do: bool f(vector<int> x) { bool g = true; for(int i = 0; i < x.size() - 1; ++i) if(x[i] > x[i+1]) g = false; return g; Give examples of non-empty vectors, one which will cause the function to return true, and one which will cause it to return false. Checks to see if the vector is sorted ascending. True: 1,2,3,4,... False: 4,3,22,... 2

This function is intended to count the number of words (non-space sequences separated by spaces) in a string, but the lines of the function are all in the wrong order. Write them in the correct order. 1 2 w++; 3 i++; 4 int count_words(string s) { 5 return w; 6 if(s[i] == ' ' && s[i+1]!= ' ') 8 while(i < s.length() - 1) { 9 int i = 0; 1 int w = s[0] == ' '? 0 : 1; int count_words(string s) { int w = s[0] == ' '? 0 : 1; int i = 0; while(i < s.length() - 1) { if(s[i] == ' ' && s[i+1]!= ' ') w++; i++; return w; Given the following ordered array class class ordered_array {... void remove(int x); private: int* data; int current_size; int max_size; ; Implement the remove method, which should remove the element x if it exists in the array, shifting everything after it down to fill in the space. 3

void ordered_array::remove(int x) { // Find x int p = -1; for(int i = 0; i < current_size; ++i) if(data[i] == x) { pos = i; break; if(pos < 0) return; // Not found // Shift everything down for(int j = pos; j < current_size-1; ++j) data[j] = data[j+1]; current_size--; Note that this implementation will still work correctly if current_size == 0. 4

2 Big-O analysis; Vectors, lists, stacks, and queues This function searches for all occurences of a pattern p in a string s: void find_all(string s, string p) { for(int i = 0; i < s.length() - p.length(); ++i) { bool found = false; for(int j = 0; j < p.length(); ++j) if(s[i + j]!= p[j]) found = false; if(found) cout << "Found at " << i << endl; It p has length m and s has length n, and we assume that n is much larger than m, what is the big-o complexity of this function? O(mn) because the outer loop runs n m times, but if m n this is just O(n). The inner loop runs O(m) times, giving us O(mn). Here is a function that checks a vector to see if it is sorted: bool is_sorted(vector<int>& v) { for(int i = 0; i < v.size() - 1; ++i) if(v[i] > v[i+1]) return false; return true; Analyze the cost of this function, in terms of the number of comparisons C, the number of increments I, and the number of vector lookups L. What is the best case cost? What is the worst case cost? When (for what inputs) do the best/worst cases occur? The loop runs n 1 times at the worst. The best case is when the first two elements are out of order. The worst case is when the whole vector is sorted. Best case: 2C + 2L (2C because the loop does a comparison the first time, too). Worst case: (2n 1))C + 2(n 1)L Given the following implementation of vector::push_back trace through the cost of the first 1 pushbacks, if a cheap pushback (i.e., a single copy) has a cost of 1, and the initial size and capacity are. 5

void vector::push_back(int x) { if(size == capacity) { // Full, reallocate to make room int* old_data = data; data = new int[1 + capacity + capacity / 2]; // Copy everything to the new array for(int i = 0; i < capacity; ++i) data[i] = old_data[i]; capacity = 1 + capacity + capacity / 2; delete[] old_data; // Add new element data[size++] = x; PB Size Cap Cost 1 1 1 1 1 2 2 2 2 3 3 4 3 4 4 4 1 5 5 5 6 6 1 1 8 8 11 8 9 9 11 1 1 1 11 1 Given the following node definition class list { struct node { int value; node* next; ; void insert(node* prior, int x); private: node* head; 6

Implement the insert method which inserts a new value after an existing node. Be sure to handle the case when the list is empty! void list::insert(node* prior, int x) { if(prior == nullptr) head = new node{x, nullptr; else prior->next = new node{x, prior->next; Suppose we have a singly-linked list with methods head() and at(), and the above node type. The following function prints out the elements of a list: void print(list l) { for(int i = 0; i < l.size(); ++i) cout << l.at(i) << ","; What is the time complexity of this function? If necessary, rewrite the function so that its time complexity is O(n). Currently, the time complexity of this is O(n ) (because.at has time complexity O(n), and we are doing it n times). The correct loop is void print(list l) { node* n = l.head(); while(n!= nullptr) { cout << n->value << ","; n = n->next; Given the above list type, implement the stack operations push and pop: void push(list l, int x); void pop(list l); (These are written as functions so that any list can be treated as a stack.)

void push(list l, int x) { // Push to front l.head = new node{x, l.head; void pop(list l) { node* n = l.head; l.head = l.head->next; delete n; Given the above list type, implement the queue operations enqueue and dequeue: void enqueue(list l, int x); void dequeue(list l); (These are written as functions so that any list can be treated as a queue.) This list doesn t have a tail pointer, so no matter how we do it, at least one of enqueue or dequeue will be O(n). void enqueue(list l, int x) { l.head = new node{x, l.head; void dequeue(list l) { // Remove last element by finding second-to-last node* n = l.head; while(n && n->next && n->next->next) { n = n->next; // Now n is the 2nd to last element. if(n!= nullptr) { delete n->next; n->next = nullptr; 8