SOLUTIONS (INCOMPLETE, UNCHECKED)

Similar documents
UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES SOLUTIONS V

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

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

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination

Examination Questions Midterm 1

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

CS171 Midterm Exam. October 29, Name:

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

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

CS 216 Exam 1 Fall SOLUTION

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Midterm Exam (REGULAR SECTION)

Basic Data Structures

Basic Data Structures 1 / 24

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

CMPSCI 187 Midterm 1 (Feb 17, 2016)

Summer Final Exam Review Session August 5, 2009

Introduction to Computer Science II (ITI 1121) Final Examination

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE ALGORITHMS AND DATA STRUCTURES Total:

ECE250: Algorithms and Data Structures Midterm Review

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Linked Structures. See Section 3.2 of the text.

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

COMP 250 Midterm #2 March 11 th 2013

DNHI Homework 3 Solutions List, Stacs and Queues

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

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

Largest Online Community of VU Students

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

== isn t always equal?

Introduction to Computer Science II (CSI 1101)

COS 226 Algorithms and Data Structures Fall Midterm

UNIVERSITY REGULATIONS

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

CSCE 2014 Final Exam Spring Version A

CIS Fall Data Structures Midterm exam 10/16/2012

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

data structures and algorithms lecture 6

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

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

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

Computer Science 1 Ah

NATIONAL UNIVERSITY OF SINGAPORE

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CS 151 Name Final Exam December 17, 2014

CE221 Programming in C++ Part 1 Introduction

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

Dynamic Data Structures

Linked Lists, Stacks, and Queues

PA3 Design Specification

CS 367: Introduction to Data Structures Midterm Sample Questions

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Largest Online Community of VU Students

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

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

Stacks and Queues. David Greenstein Monta Vista

Largest Online Community of VU Students

Csci 102: Sample Exam

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

ITI Introduction to Computing II

Introduction to Computer Science II (ITI 1121) Midterm Examination

COMP 103. Data Structures and Algorithms

Introduction to Programming (Java) 4/12

G Programming Languages - Fall 2012

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

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

CSE 373 Spring Midterm. Friday April 21st

ECE Fall st Midterm Examination (120 Minutes, closed book)

ITI Introduction to Computing II

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Computer Science 62. Midterm Examination

Introduction to Computer Science II (CSI 1101) Final Examination

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

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

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

Interfaces & Generics

Queues Fall 2018 Margaret Reid-Miller

Matriculation number:

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

CS350 - Exam 1 (100 Points)

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

mypoly

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

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

ECE 122. Engineering Problem Solving with Java

Associate Professor Dr. Raed Ibraheem Hamed

Midterm Exam 2 CS 455, Spring 2011

CS111: PROGRAMMING LANGUAGE II

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Week 6. Data structures

Midterm Sample Answer ECE 454F 2008: Computer Systems Programming Date: Tuesday, Oct 28, p.m. - 5 p.m.

Building Java Programs

Transcription:

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES Midterm Examination Instructor: R.E.Seviora 1.5 hrs, Oct 27, 2000 SOLUTIONS (INCOMPLETE, UNCHECKED) Student id: 1. 2. 3. 4. Total: Do all problems. The number in brackets denotes the relative weight of the problem (out of 100). If information appears to be missing from a problem, make a reasonable assumption, state it and proceed. If the space to answer a question is not sufficient, use the last (overflow) page. Closed book. Only numeric display calculators are allowed. PROBLEM 1 [20] A. Running Time Analysis Determine the running times predicted by the simplified model of the computer for the following program fragment for (int i=0; i<n; ++i) for (int j=i; j<n; ++j) ++k; 11/2n 2 B. Solving Recurrences Solve the following recurrence: O(1) T ( n) = at( You may assume that n is a power of a. Show all your work. n = 1 n/a ) + O( n) n > 1, a >= 2

Midterm Examination Page 2/7 E&CE 250 F 00 PROBLEM 2 [25] A. Big Oh Analysis Do a big Oh analysis of the following algorithm: 01 public class Example 02 { 03 public static int geoseriessum (int x, int n) 04 { 05 int sum = 0; 06 for (int i = 0; i <= n; ++i) 07 { 08 int prod = 1; 09 for (int j = 0; j < i; ++j) 10 prod *= x; 11 sum += prod; 12 13 return sum; 14 15 Stmt Time TOTAL = O(n 2 ) 5 O(1) 6a O(1) 6b O(n) 6c O(n) 8 O(n) 9a O(n) 9b O(n 2 ) 9c O(n 2 ) 10 O(n 2 ) 11 O(n) 13 O(n) B. Miscellanea (a) The course introduced three different types for representation of integers: int, Integer and Int. Summarize briefly the differences between these three types. int is a Java primitive type; the value of int is stored directly Integer is the Java wrapper for int; it wraps int inside an object Int is an E&CE250 wrapper for int; it was introduced because of the need to view Int as an instance of the type Comparable (defined in E&CE 250)which Integer does not subtype (b) What is the difference between extends and implements in Java? extend: - relationship between classes; - a class C which extends superclass S inherits from C all the fields and methods visible to it; - also a relationship between interfaces - only one class (or interface) can be extended implements: - relationship between a Java class and a Java interface - a class implementing an interface must implement all the methods in the interface (unless they are declared abstract in the class) - a class may implement several interfaces

Midterm Examination Page 3/7 E&CE 250 F 00 C. Visitors Consider a container class whose instances will contain objects of the type Float. The value encapsulated in each object can be obtained using the method float floatvalue() of the class Float. Implement a sum-of-squares Visitor whose visit method computes the sum of squares of the values in the objects in the container, i.e. 2 S = V objects k where V k is the value encapsulated in the k-th object. The computed value of the above sum should be accessible through the method float getsumofsquares()of the Visitor. Note that the Visitor s method isdone() is inherited from the class AbstractVisitor. It will always return false, which is appropriate for the case considered. public class SumOfSquaresVisitor extends AbstractVisitor { //fields protected float sumsq = 0.0 //methods public void visit (Object obj) { float val = ((Float) obj).floatvalue(); sumsq += val*val; public float getsumofsquares () { return sumsq; PROBLEM 3 [25] A. Singly-linked List (a) The singly linked list class considered in the lectures had the structure shown below. Fill in the dotted (...) entries. public class LinkedList { protected Element head; protected Element tail; public final class Element { Object datum; Element next; Element (Object datum, Element next) { this.datum = datum; this.next = next; //etc (b) A more uniform implementation of a singly liked list could have just a single Element, called the header node, plus other nodes if the list is nonempty. A diagram showing an empty list and a list with two Integer objects is below. The header node is shown in gray.

Midterm Examination Page 4/7 E&CE 250 F 00 4 5 (i) Show the fields of the class LinkedListHN (linked list based on header node). protected Element headernode; (ii) Show the no-arg (i.e. no parameters) constructor for LinkedListHN. public LinkedListHN () { headernode = new Element(); headernode.next = headernode; (iii) Show the algorithm for the method void prepend (Object obj) of LinkedListHN. void prepend (Object obj) { Element tmp = new Element (obj, headernode.next); headernode.next = tmp; B. Project 2 The objective of Project 2 was to represent a polynomial in x n n 1 an x + an 1 x +.. + a1x + a0 where n 0 is the degree of the polynomial, using an array. The implementation was required to be reasonably lean in terms of computing time, and minimal: the array length at all times was to be n+1. The PolynomialAsArray class declaration looked as follows: class PolynomialAsArray { double[] a; //array of coefficients PolynomialAsArray () { //constructs the polynomial 0x 0... //etc (a) Draw a diagram that shows the objects stored in the memory of the computer after each of the following statements. Identify the objects that are the same (in the = = sense). PolynomialAsArray p, q; p = new PolynomialAsArray(); p.setcoefficient(3,13.0); p.setcoefficient(0, 10.0); p.setcoefficient(2, 12.0);

Midterm Examination Page 5/7 E&CE 250 F 00 p.setcoefficient(3, 0.0); q = p.plus(p); (b) Derive an expression for the amount of memory required to represent a polynomial with degree m. Write your expression in terms of the following parameters: sizeof(double), sizeof(int),... PROBLEM 4 [30] A. Enumeration In the lectures, we showed the getenumeration() method for StackAsArray. Show an implementation of getenumeration() for QueueAsArray. public class QueueAsArray extends AbstractContainer implements Queue { protected Object[] array; protected int head; protected int tail; //... //Enumeration methods: nextelement() and hasmoreelements() public Enumeration getenumeration () { { return new Enumeration () { protected int position = -1, enumcount = 0; public Object nextelement () { if (enumcount++ >= getcount()) throw new NoSuchElementException (); while (array[++position]==null) {; return array[position]; public boolean hasmoreelements() { return (enumcount < getcount ());

Midterm Examination Page 6/7 E&CE 250 F 00 B. Stacks An application you are designing will use the abstract data type Stack (with operations push, pop, gettop). Furthermore, the application will need to frequently find the largest item on the stack. You are to develop a new class StackMax, with an additional method findmax() which returns a reference to the largest element on the stack. In view of the anticipated frequency of invocations of this method, it is required that findmax() has the worst case running time of O(1). Note that push, pop and gettop should remain O(1). Show the fields and the algorithms for push, pop and findmax for StackMax. You are not required to show the implementation for remaining methods. It is suggested that you base your implementation on one of the stack class implementations discussed in the course, specifically StackAsLinkedList, as opposed to implementing all methods from scratch. If you are unable to devise an O(1) solution for findmax(), show a slower algorithm for part mark. public class StackMax extends AbstractContainer implements Stack { //fields protected Stack s1 = new StackAsLinkedList (); protected Stack s2 = new StackAsLinkedList (); //methods public Object gettop () { return s1.gettop (); public Object findmax () { return s2.gettop (); public void push (Object arg) { Comparable object = (Comparable) arg; s1.push (object); if (object.islt ((Comparable) s2.gettop ())) s2.push (object); else s2.push (s2.gettop ()); public Object pop () { s2.pop (); return s1.pop (); C. Queues - Running Times The QueueAsLinkedList class presented in the lectures was implemented using the LinkedList class of Chapter 4 of the text. The latter had both the head and the tail pointer in its implementation. (a) In the table below, show the big Oh bounds for the running time for the methods given. (b) Consider the case when the underlying LinkedList class was replaced by another implementation in which the tail field was dropped. In the table below, show the big Oh bounds for this case. Base: LinkedList with head,tail fields Base: LinkedList with head field only constructor O(1) O(1) purge O(1) O(1) enqueue O(1) O(n) dequeue O(1) O(1) gethead O(1) O(1)

Midterm Examination Page 7/7 E&CE 250 F 00 OVERFLOW SHEET [Please identify the question(s) being answered.]