DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Similar documents
DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100

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

Test 1: CPS 100. Owen Astrachan. October 1, 2004

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100e

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Test #1. Login: Initials: 2 PROBLEM 1 : (The Sorted Link (12 points)) Using the following definition of a linked list node, write a function that inse

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS 201 Discussion 5 BIG-OH REVIEW

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

Chapter 2: Complexity Analysis

CSL 201 Data Structures Mid-Semester Exam minutes

APCS Semester #1 Final Exam Practice Problems

Test 2: CPS 100. Owen Astrachan. November 15, 2000

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

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

CSE wi: Practice Midterm

Compsci 100. Owen Astrachan April 14, 2010

CSE 373 Spring Midterm. Friday April 21st

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

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

ASYMPTOTIC COMPLEXITY

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

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

Question 1. [5 points] What output is printed by the following program (which begins on the left and continues on the right)?

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CS 310: Order Notation (aka Big-O and friends)

Algorithmic Analysis. Go go Big O(h)!

Test 1: Compsci 100. Owen Astrachan. February 15, 2006

COS 126 General Computer Science Spring Written Exam 1

Building Java Programs Chapter 13

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

CS 201, Fall 2016 Dec 2nd Exam 3

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

Multiple-choice (35 pt.)

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

CS171 Midterm Exam. October 29, Name:

Section 05: Midterm Review

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

CS:3330 (22c:31) Algorithms

CS S-08 Arrays and Midterm Review 1

Complexity, General. Standard approach: count the number of primitive operations executed.

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

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Computer Science Foundation Exam

CSc 520 Principles of Programming Languages

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

COMP 250 Midterm #2 March 11 th 2013

// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);

Computer Science Foundation Exam

Section 05: Solutions

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

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

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

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

Final Exam. COMP Summer I June 26, points

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

Computer Science 1 Ah

Asymptotic Analysis of Algorithms

CS/COE 1501

Introduction to the Analysis of Algorithms. Algorithm

Data Structures and Algorithms Key to Homework 1

Building Java Programs

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

CS126 Final Exam Review

Searching and Sorting

Unit 10: Sorting/Searching/Recursion

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

CS 151 Name Final Exam December 17, 2014

ASYMPTOTIC COMPLEXITY

Lecture 2: Algorithm Analysis

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Analysis of algorithms

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Introduction to Computer Science

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

COMP 161 Lecture Notes 16 Analyzing Search and Sort

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CS150 Sample Final Solution

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Computer Science Foundation Exam

Computer Science & Engineering 150A Problem Solving Using Computers

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

Computer Science Foundation Exam

Test 2 Practice : Compsci 201

Section 05: Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

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)

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

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

Transcription:

DUKE UNIVERSITY Department of Computer Science Test 1: CompSci 100 Name (print): Community Standard acknowledgment (signature): Problem 1 value 30 pts. grade Problem 2 16 pts. Problem 3 14 pts. Problem 4 15 pts. TOTAL: 75 pts. This test has 10 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts only 75 minutes and there are 75 points on the exam. That means you should spend no more than 1 minute per point. Don t panic. Just read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident. Do not be alarmed if some of the answers are obvious. If you think there is a syntax error or an ambiguity in a problem s specification, then please ask. In writing code you do not need to worry about specifying the proper import statements. Assume that all libraries and packages we ve discussed are imported in any code you write.

Test 1: CompSci 100 2 PROBLEM 1 : (Short Ones (30 points)) Briefly answer the following questions. A. What is the difference between an abstract class and an interface? If I have implemented generic methods that will be used in subclasses, should I put them in an abstract class or interface? B. Given the following line of code Object o = s.substring(0); Give two (2) examples of errors that could occur. Specify whether the error is a run-time or compiletime error. (Note: there are more than two possibilities. You only need to give two. The errors must occur at this line of code, not inside the substring method.)

Test 1: CompSci 100 3 C. Translate the following postfix expressions into BOTH prefix and infix. Use parentheses where necessary. I. a b + c d - / e + prefix: infix: II. a b c + d e - * - prefix: infix: D. What is the output of the following code fragment? What are the contents of the stack at the end? Stack s = new Stack(); s.push("maryland"); s.push("unc"); s.push("duke"); s.push("fsu"); s.push("clemson"); s.push("wake"); s.push("uva"); s.push("gatech"); s.push("ncsu");

Test 1: CompSci 100 4 E. DNA can be represented as long strings of the nucleotides a, t,c, and g. If we wanted to determine whether DNA strings of length 10 6 were anagrams of each other, which normalizer representation would be more efficient, the fingerprint/histogram or the sort method? Briefly justify your answer. F. State whether the following statement is true or false. If it is true, justify using the definition of big-oh. If it is false, give a counterexample. If f(n) O(g(n)) and d(n) O(h(n)), then f(n) + d(n) O(g(n) + h(n)) G. Bill shows you an algorithm to optimally choose classes for all students at Duke. You, being a good Computer Science student, notice that the big-oh complexity of the algorithm is O(2 n ) where n is the number of students. However, Bill demonstrates the program for a sample of 100 students and it returns the schedules almost immediately. Bill says that his algorithm is good enough for Duke. He mentions something about Moore s Law and states that Computers are getting faster at an exponential rate. That is, every 18 months, they double in speed. Even if the program is not fast enough now, it will be soon. Bill is off a little bit on what Moore s law means. However, given that computers continue doubling in speed every 18 months, will the O(2 n ) algorithm ever be practical? Explain why or why not?

Test 1: CompSci 100 5 PROBLEM 2 : (Complex (16 points)) Give big-oh runtime complexities with brief justifications. A. Give the big-oh complexity in terms of n, the length array A, of the running time of the following code: * returns the sum of the odd elements in an Array int sumoddelems(int A[]) int sum = 0; for (int i = 1; i < A.length; i += 2) sum += A[i]; return sum; B. Give the big-oh complexity in terms of n, the length array A, of the running time of the following code: * Given 2 arrays, A & B each storing n integers where n >= 1 * return the number of elements in B equal to the sum of prefix * sums in A int prefixsums(int A[], int B[]) if (A.length!= B.length) return 0; int c = 0; for (int i=0; i < A.length; i++) int sum = 0; for (int j=0; j < A.length; j++) sum += A[0]; for (int k = 1; k <= j; k++) sum += A[k]; if (B[i] == sum) c += 1; return c;

Test 1: CompSci 100 6 C. Give the big-oh complexity in terms of n of the running time of the following code: * returns the integer binary logarithm of n int lg(int n) if (n==1) return 0; return 1 + lg(n/2); D. Below are two methods, sumpoly1 and sumpoly2 that both correctly calculate the sum of a polynomial with coefficients a0,..., a n 1, that is: n 1 sumpoly(x) = a i x i = a 0 + a 1 x +... + a n 1 x n 1 double sumpoly1(double a[], double x, int index) if (index >= a.length) return 0; double prod = 1.0; for (int i=0; i < index; i++) prod *= x; return a[index] * prod + sumpoly1(a, x, index+1); i=0 double sumpoly2(double a[], double x, int index) if (index >= a.length) return 0; return a[index] + x*sumpoly2(a, x, index+1); I. Give the call to sumpoly1 that returns 5x 4 + 3x 2 17x + 1 II. What is the runtime complexity of sumpoly1 in terms of the length of a, the coefficients in the polynomial? Use big-oh and justify your answer briefly, using recurrences where possible. III. What is the runtime complexity of sumpoly2 in terms of the length of a, the coefficients in the polynomial? Use big-oh and justify your answer briefly, using recurrences where possible.

Test 1: CompSci 100 7 PROBLEM 3 : (Find your perfect match (14 points)) The match method is given an array of prefixes and suffixes and attempts to match each prefix to form a valid word. Each prefix and each suffix is used exactly once in the match. You can determine what strings are words by searching in the TreeSet dict. If a valid match is found after a given index, match returns true and the entries in the suffix array have been rearranged such that that the suffix matched to prefix[0] is at index 0, prefix[1] s match is at index 1, and so on. If no match is found, the function returns false and the entries in the suffix array are unchanged. If attempting to match these two arrays: prefixes show great cook read top suffixes est s ier ing ed match(prefixes, suffixes, dict, 0) would return true and the suffixes array would be rearranged as shown below. suffixes ier est ed ing s Hints: Use the contains method in the TreeSet class to determine if a prefix + suffix combination is in dict Recursion works well here. The base case is where all prefixes and suffixes have matched, that is when where there are no prefixes past index to consider. The recursive case should consider all unused suffixes. You may use the helper swap function below that exchanges strings in an array. void swap(string[] a, int i, int j) String temp = a[i]; a[i] = a[j]; a[j] = temp;

Test 1: CompSci 100 8 Complete match below. * @return true if entries in pref and suff can be combined * to create words that match strings in dict * Assume pref and suff are of the same length boolean match(string[] pref, String[] suff, TreeSet dict, int index)

Test 1: CompSci 100 9 PROBLEM 4 : (Love is not always rational (15 points)) The class RationalNumber.java is appended to the back of this exam. The class represents rational numbers. All rational numbers can be represented as n/d where n and d are integers. A. Fill in the compareto method for RationalNumber. Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. public int compareto(object o) B. Complete method the method add that returns the result of adding this rational number to another passed as an argument. Note that i j + k il + kj =. l jl ] * returns the result of adding this rational number to one * passed as a parameter. A public RationalNumber add(rationalnumber op2)

Test 1: CompSci 100 10 C. We would like to write an algorithm to find two rational numbers in A that sum to x. The method below, findpair, takes a sorted ArrayList of RationalNumbers, index bounds i and j, and a RationalNumber x. findpair returns true if there exists a pair of RationalNumbers in the ArrayList between i and j (inclusive) that sum to x. The code for findpair is started below. Fill in the recursive calls below. * Given sorted sublist A[i..j], determines whether there is any * pair of elements that sums to x public boolean findpair(arraylist A, int i, int j, RationalNumber x) if (i == j) return false; RationalNumber Ai = (RationalNumber)A.get(i); RationalNumber Aj = (RationalNumber)A.get(j); RationalNumber pairsum = Ai.add(Aj); int comp = pairsum.compareto(x); if (comp == 0) return true; else if (comp < 0) return ; else if (comp > 0) return ; D. Given that Collections.sort runs in O(n log n) time, what is the worst-case big-oh time complexity of addstox in terms of n, the size of the ArrayList. Briefly justify your answer. public boolean addstox(arraylist A, RationalNumber x) Collections.sort(A); // nlogn return findpair(a, 0, A,size()-1, x);