UNIVERSITY REGULATIONS

Similar documents
UNIVERSITY REGULATIONS

CPSC 311, 2010W1 Midterm Exam #2

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

CPSC 121 Sample Final Examination December 2013

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 121: MIDTERM 2 Group March 12, 2014

CPSC 121 Midterm 1 Friday October 14th, Signature: Section (circle one): 11:00 15:30 17:00

The University of British Columbia Final Examination - December 02, 2014 Mathematics 308. Closed book examination. No calculators.

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

This examination has 11 pages. Check that you have a complete paper.

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CPSC 320 Midterm 2. July 13, 2007

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

The University of British Columbia

Full Name: CS Account:

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 261: MIDTERM 1 February 14, 2017

CPSC 121 Midterm 1 Friday February 5th, Signature: Section (circle one): Morning Afternoon

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

CS171 Midterm Exam. October 29, Name:

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

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

EXAMINATION INSTRUCTIONS

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

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

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

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

MATH 253/101,102,103,105 Page 1 of 12 Student-No.:

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

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

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

UNIVERSITY REGULATIONS

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Computer Science 304

Section 1: True / False (2 points each, 30 pts total)

CSE 247 Data Structures and Algorithms Fall Exam I

CSE373 Winter 2014, Midterm Examination January 29, 2014

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Section 05: Solutions

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

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

CSE 131 Introduction to Computer Science Fall Exam II

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

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

CS 411 Midterm Feb 2008

Solution to CSE 250 Final Exam

CSE 373: Data Structures and Algorithms

Section 05: Solutions

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

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

Midterm 2. Replace by cover page.

OF VICTORIA EXAMINATIONS- DECEMBER 2011 CSC

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

ECE250: Algorithms and Data Structures Midterm Review

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

CSE wi: Practice Midterm

CPSC 410 Advanced Software Engineering Mid-term Examination (Term I ): Solution Instructor: Gail Murphy

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

EECS Sample Midterm Exam

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

Midterm solutions. n f 3 (n) = 3

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

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

Practice Midterm Exam Solutions

Unit #3: Recursion, Induction, and Loop Invariants

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

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

CPSC 311, 2011W1 Midterm Exam #2 2011/11/09

ext Total Score /20 /20 /15 /20 /25 /5 Grader

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Computer Science Foundation Exam

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

EXAMINATION INSTRUCTIONS

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

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Problem Set 6 Due: 11:59 Sunday, April 29

CS 216 Exam 1 Fall SOLUTION

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

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

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

Principles of Algorithm Design

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Problem Score Maximum MC 34 (25/17) = 50 Total 100

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Midterm Exam 2B Answer key

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Transcription:

CPSC 221: Algorithms and Data Structures Midterm Exam, 2015 October 21 Name: Student ID: Signature: Section (circle one): MWF(101) TTh(102) You have 90 minutes to solve the 8 problems on this exam. A total of 75 marks is available. You may want to complete what you consider to be the easiest questions first! Ensure that you clearly indicate a legible answer for each question. You are allowed one sheet of paper (US Letter size or metric A4 size) of notes. Besides this, no notes, aides, or electronic equipment are allowed. Good luck! UNIVERSITY REGULATIONS 1. Each candidate must be prepared to produce, upon request, a UBCcard for identification. 2. Candidates are not permitted to ask questions of the invigilators, except in cases of supposed errors or ambiguities in examination questions. 3. No candidate shall be permitted to enter the examination room after the expiration of one-half hour from the scheduled starting time, or to leave during the first half hour of the examination. 4. Candidates suspected of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action: having at the place of writing any books, papers or memoranda, calculators, computers, sound or image players/recorders/transmitters (including telephones), or other memory aid devices, other than those authorized by the examiners; speaking or communicating with other candidates; and purposely exposing written papers to the view of other candidates or imaging devices. The plea of accident or forgetfulness shall not be received. 5. Candidates must not destroy or mutilate any examination material; must hand in all examination papers; and must not take any examination material from the examination room without permission of the invigilator. 6. Candidates must follow any additional examination rules or directions communicated by the instructor or invigilator. P1 P2 P3 P4 P5 P6 P7 P8 Total 12 11 9 8 5 10 10 10 75

This page intentionally left blank. If you put solutions here or anywhere other than the blank provided for each solution, you must clearly indicate which problem the solution goes with and also indicate where the solution is at the designated area for that problem s solution.

1 Asymptotic Relationships [12 marks] Consider the following functions: A. 100 B. lg(n)/ log(n) C. 3n 5 D. 100n + 2n lg(n) E. 5n 5+lg n n 0.1 F. lg(n!) For each function, write down the LETTERs for the other functions which have that relationship to it. If no other functions have that relationship, draw a line through the box. I have done the first one for you as an example: The function...... is big-o of:... is big-ω of:... is big-θ of: A A, B, C, D, E, F A, B A, B B C D E F

2 Big-Θ Proof [11 marks] For this problem, you must formally prove that n/10 Θ(n). Since this is a formal proof, it will be based on the definition of big-θ, and since that definition has two main parts, you should clearly label those two major parts of your proof. (If you don t remember, recall that the floor function x computes the largest integer less than or equal to x. In other words, it rounds x down to the closest integer. You might find it helpful in your proof to note that x 1 < x x.)

3 Big Factoring [9 marks] Computing prime factorizations (i.e., the set of prime numbers that multiply together to equal the given number) is an important problem in cryptography. (Recall that a prime number is a number that is evenly divisible by only 1 and itself. For example, 2, 3, and 5 are all prime numbers, but 6 isn t, because 6 is evenly divisible by 2 (as well as by 3).) The following code computes the prime factorization of a number. (In real life, more efficient algorithms are used, but this is simple enough for us to analyze.) void factor(int n) { while (n>1) { for (int i=2; i<=n; i++) { // The next line checks whether n is evenly divisible by i if (n%i == 0) { // If so, than i is a prime factor. cout << i << endl; // Print i out n = n/i; // Factor i out of n break; // and break out of the for loop return; 1. Give a big-θ runtime bound in terms of n for factor, under the assumption that n is a power of 2 (in other words, that n = 2 k for some integer k). You do not need to show your work (but it might help with partial credit). (Hint: Think carefully about how the loops and if statements will behave when n is a power of 2.) 2. Give a big-θ runtime bound in terms of n for factor, under the assumption that n is prime. You do not need to show your work (but it might help with partial credit). (Hint: Think carefully about how the loops and if statements will behave when n is prime.)

4 Tribonacci Numbers [8 marks] The Tribonacci numbers are a straightforward extension to Fibonacci numbers, where Trib(n) = Trib(n 1) + Trib(n 2) + Trib(n 3) and Trib(0) = 0, Trib(1) = 0, and Trib(2) = 1. 1. Write down the next three Tribonacci numbers: 2. The following code computes Tribonacci numbers: int trib(int n) { if (n==0) return 0; if (n==1) return 0; if (n==2) return 1; return trib(n-1) + trib(n-2) + trib(n-3); Write down recurrence relations for the runtime of trib. Your answer must reflect the recursive structure of the code. 3. Write down recurrence relations for the space (or equivalently, the maximum call-stack depth) used by trib. Your answer must reflect the recursive structure of the code. (You may assume that the space/call-stack-depth used by trib(n-1) is greater than that used by trib(n-2), which is greater than that used by trib(n-3).) 4. Give a tight big-θ bound on the space complexity (or equivalently, the maximum call-stack depth) of trib in terms of n. You do not need to explain or prove your answer, but if you do provide a brief explanation, we might be able to give you partial credit. 5. Which recursive calls are tail calls? Circle your answer(s) here: trib(n-1) trib(n-2) trib(n-3) none of them

5 Express Lists I [5 marks] One of the drawbacks of linked lists is that there s no fast way to get through the list, other than by visiting each node one-by-one, in order. In this question (and subsequent questions), we ll explore an idea to have a linked list that can run faster by jumping over some nodes in the list. (What we ll do on this exam isn t a great idea, but it s similar to something we might see later in the course, which really is cool.) The idea is that each node will have in addition to the usual next pointer that points to the next node in the list a pointer jump that points to a node farther down the linked list. So, the declaration for the Node datatype is: struct Node { int data; Node * jump; Node * next; ; In this question, let s assume the jump pointer always jumps over one node, so the list would look like this: 10 42 123 365 null 895 null Your task on this question is to write code that will correctly delete a node from a list like this, restoring all the pointers to point to the correct places. To make your programming easier, we will assume that your function will receive as parameters a pointer to the node to delete, as well as pointers to the (immediate) predecessor of the node to delete, and the express predecessor, i.e., the node whose jump pointer points to the node being deleted. Your code must run in constant time and space. Be sure not to leak memory. void delete_node(node * target, Node * pred, Node * jump_pred) { \\ target is the node to be deleted \\ pred is the node that points to target through its next pointer \\ jump_pred is the node that points to target through its jump pointer \\ YOUR CODE GOES HERE null

6 Express Lists II [10 marks] In this question, we will continue working with the Express List data structure from Question 5, but you don t need to have solved that question to do this one. (You do need to read it, though!) In addition, in this question, we will assume that the data in the list are in increasing order. The following function returns a node in the express list whose data field matches the specified key, or NULL if there is no such node: Node * find_key(node * list, int key) { if (list == NULL) return NULL; // Take the jump pointers through the list as far as possible. Node * cur = list; while ( (cur->jump!= NULL) && (cur->jump->data < key) ) { cur = cur->jump; // Now, follow the next pointers as usual. while ( (cur!= NULL) && (cur->data <= key) ) { if (cur->data == key) return cur; cur = cur->next; return NULL; This question asks you to compute big-θ bounds on the runtime of find_key under various assumptions. If you can come up with only a good big-o or big-ω bound, that will be worth partial credit. You do not need to prove your answer, but briefly explain your reasoning. 1. Give a big-θ bound on the worst-case running time of find_key, in terms of the size n of the list. You do not need to prove your answer, but briefly explain your reasoning. 2. Give a big-θ bound on the best-case running time of find_key, in terms of the size n of the list. You do not need to prove your answer, but briefly explain your reasoning.

3. Now, assume that instead of jumping over 1 node, the jump pointer jumped over 9 nodes. Give a big-θ bound on the worst-case running time of find_key, in terms of the size n of the list. You do not need to prove your answer, but briefly explain your reasoning. 4. Now, assume that instead of jumping over 9 nodes, the jump pointer jumped over n nodes, where n is the total length of the list. Give a big-θ bound on the worst-case running time of find_key, in terms of n. You do not need to prove your answer, but briefly explain your reasoning. 5. Now, assume that instead of jumping over n nodes, the jump pointer jumped over n/2 nodes, where n is the total length of the list. Give a big-θ bound on the worst-case running time of find_key, in terms of n. You do not need to prove your answer, but briefly explain your reasoning.

7 Express Lists III [10 marks] Convert the find_key function from the preceding question (Question 6) so that it doesn t use any iteration no while-loops, no for-loops, no do-loops, no loops of any kind. (Hint: Use recursion. Your solution should have a total of three functions, including find_key.)

8 Priority Queues Using Queues [10 marks] For this question, your challenge is to implement a priority queue with only a single (normal, non-priority) queue in which to store the data. You are also allowed to declare int variables, but not arrays, vectors, pointers, or any other data structures. You also are not allowed to call new, nor use recursion. The Queue class provides these methods: void enqueue(int); // Puts an int into the queue. int dequeue(); // Takes the int from the front of the queue and returns it. int size(); // Returns the number of elements stored in the queue. Here s most of the code for the PriorityQueue class, including an implementation of the insert method. Your task is to write the deletemin method. (You may assume that deletemin is never called with an empty priority queue.) class PriorityQueue { Queue q; // Storage for items in the priority queue // Note that you are NOT allowed to declare additional Queues public: PriorityQueue(); PriorityQueue(); void insert(int); int deletemin(); // Takes the smallest int out of the queue and returns it. int isempty();... void PriorityQueue::insert(int n) { // I ll just enqueue it, not worrying about order. // deletemin can worry about finding the smallest item. q.enqueue(n); int PriorityQueue::isEmpty() { return (q.size() == 0); int PriorityQueue::deleteMin() { // YOUR CODE GOES HERE (AND POSSIBLY ON THE NEXT PAGE)...

(additional space if needed for Priority Queues Using Queues) Briefly explain why this question would be impossible to solve if you had been allowed only a single stack instead of a single queue (with the same restrictions about no other data structures and no recursion).

This page intentionally left blank. If you put solutions here or anywhere other than the blank provided for each solution, you must clearly indicate which problem the solution goes with and also indicate where the solution is at the designated area for that problem s solution.

This page intentionally left blank. If you put solutions here or anywhere other than the blank provided for each solution, you must clearly indicate which problem the solution goes with and also indicate where the solution is at the designated area for that problem s solution.