CS 331 Summer 2017 Final Exam

Similar documents
CS 331 Fall 2017 Midterm Exam 2

CS 331 Midterm Exam 2

CS 331 Midterm Exam 2

CS 331/401 Summer 2018 Midterm Exam

CS 331 Midterm Exam 2

CS 331 Midterm Exam 1

Student Number: Lab day:

Figure 1: A complete binary tree.

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

CS 445: Data Structures Final Examination: Study Guide

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

What is an algorithm?

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community

CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

CS-141 Exam 2 Review April 4, 2015 Presented by the RIT Computer Science Community

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Extended Introduction to Computer Science CS1001.py

Computer Science E-22 Practice Final Exam

Lecture 16: Binary Search Trees

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CS301 - Data Structures Glossary By

Code No: R Set No. 1

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Algorithm Analysis Advanced Data Structure. Chung-Ang University, Jaesung Lee

Largest Online Community of VU Students

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSL 201 Data Structures Mid-Semester Exam minutes

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

Walk through previous lectures

Structure and Interpretation of Computer Programs

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

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

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

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

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

namibia UniVERSITY OF SCIEnCE AnD TECHnOLOGY

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

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

CS24 Week 8 Lecture 1

Spring 2017 CS 1110/1111 Exam 1

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

Binary Search Tree. Revised based on textbook author s notes.

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

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

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

Total No. of Questions :09] [Total No. of Pages : 02. II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC First Semester CSE/IT DATA STRUCTURES USING C

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS 241 Analysis of Algorithms

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

Computer Programming II C++ (830)

Sorting algorithms: Bubble, Selection, and Tree Sort

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

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

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

Structure and Interpretation of Computer Programs

Fundamentals of Data Structure

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

Chapter 2: Basic Data Structures

Computer Programming II Python

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Data Structure (CS301)

Module 08: Searching and Sorting Algorithms

CSI33 Data Structures

EXAMINATION INSTRUCTIONS

) $ f ( n) " %( g( n)

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Home Works and Assignments

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Basic Data Structures (Version 7) Name:

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

UNIVERSITY REGULATIONS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Transcription:

CS 331 Summer 2017 Final Exam Instructions: This exam is closed-book, closed-notes. Calculators are not permitted. For numbered, multiple-choice questions, fill your answer in the corresponding row on the bubble sheet. For problems that require a written solution (labeled with the prefix WP ), write your answer in the space provided on the written solution sheet. Please write legibly and clearly indicate your final answer. Turn in the exam question packet, bubble sheet, and written solution sheet separately. Good luck! Page 1 of 8

Concepts (30 points): 1. An algorithm calls for appending N elements to an array-backed list, while invoking merge sort on the list after each append operation to keep the list sorted. What is the time complexity of the algorithm? (a) O(N) (b) O(N log N) (c) O(N 2 ) (d) O(N 2 log N) 2. What is the time complexity of locating (but not removing) the maximum element in a max-heap of N elements? (a) O(1) (c) O(N) (d) O(N log N) 3. What is the time complexity of inserting a new element into a max-heap of N elements and ensuring the heap property is maintained? (a) O(1) (c) O(N) (d) O(N log N) 4. What is the time complexity for inserting a new element into an unbalanced binary search tree containing N elements, assuming that the new elements value is neither the new minimum nor maximum value after insertion? (a) O(1) (c) O(N) (d) O(N log N) 5. Which data structure would you choose to help maintain multiple key/value associations, and where a full-ordering of keys or values is not important? (a) a queue (b) a hashtable (c) a heap (d) a binary search tree Page 2 of 8

6. Which data structure would you choose for an application where it is necessary to track multiple objects, but where only the object with the largest value of some specified attribute needs to be retrieved and processed? (a) a queue (b) a hashtable (c) a heap (d) a binary search tree 7. Which data structure would you choose for an application where it is necessary to track multiple objects, but where only the object that has been tracked for the longest needs to be retrieved and processed? (a) a queue (b) a hashtable (c) a heap (d) a binary search tree 8. Which data structure would you choose for an application where it is necessary at all times to maintain a full ordering of multiple objects across insertions and deletions? (a) a queue (b) a hashtable (c) a heap (d) a binary search tree 9. A student has proposed the following method as a faster way of counting the number of elements in a doubly-linked list with a sentinel head node. def fast_count(self): n = 0 h = self.head.next while h is not self.head: if h.next is not self.head: n, h = n+2, h.next.next n, h = n+1, h.next return n What is the time complexity of fast_count when run on a list with N elements? (a) O(1) (c) O(N) (d) O(N log N) Page 3 of 8

10. Which completes the following implementation of insert, which inserts a new value val at (positive) position idx within an array-backed list? def insert(self, idx, val): self.data.append(none) self.data[idx] = val (a) for i in range(idx, len(self.data)): self.data[i+1] = self.data[i-1] (b) for i in range(idx, len(self.data)): self.data[i+1] = self.data[len(self.data)-i] (c) for i in range(len(self.data)-1, idx, -1): self.data[len(self.data)-i] = self.data[i] (d) for i in range(len(self.data)-1, idx, -1): self.data[i] = self.data[i-1] 11. Which removes the first element from a circular doubly-linked list with a sentinel head node? (a) self.head = self.head.next self.head.prior = self.head.prior (b) self.head.next.prior = self.head self.head.next = self.head.next.next (c) self.head.next.next.prior = self.head self.head.next = self.head.next.next (d) self.head.next.prior = self.head.next self.head.next.next = self.head.next.prior 12. Consider the following function: def last(a): if not a: return None elif len(a) == 1: return a[0] return Which completes the implementation so that when passed a non-empty list as the initial argument, the last element in the list is returned? (a) last(a[0]) (b) last(a[1:]) (c) last(a[:-1]) (d) last(a[1:-1]) Page 4 of 8

13. Consider the following function: def permutations(lst, p=()): if not lst: print(p) Which completes the implementation so that when passed a list, all permutations of the list elements are printed out? E.g., when called with the list [1, 2, 3], the output would consist of (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1) though not necessarily in that order. (a) permutations(lst[1:], (lst[0],) + p) (b) permutations(lst[1:], p + (lst[0],)) permutations(lst[1:], p) (c) for i in range(len(lst)): permutations(lst[:i] + lst[i+1:], p + (lst[i],)) (d) for i in range(1, len(lst)): permutations(lst[i:], p + (lst[i],)) 14. For this and the next question, consider the following recursive function, devised by a student as an implementation of an alternative sort algorithm: def swap_sort(lst): if not lst or len(lst) == 1: return lst elif lst[0] > lst[-1]: return [ lst[-1] ] + swap_sort(lst[1:-1]) + [ lst[0] ] return [ lst[0] ] + swap_sort(lst[1:-1]) + [ lst[-1] ] The algorithm is flawed, however. What does it return given the input list [10, 3, 4, 1, 8, 16, 2]? (a) [1, 3, 2, 4, 8, 10, 16] (b) [2, 3, 4, 1, 8, 16, 10] (c) [3, 10, 1, 4, 8, 16, 2] (d) [10, 16, 8, 4, 3, 2, 1] 15. We can approximate the runtime complexity of swap_sort by estimating the number of recursive calls (i.e., the depth of recursion). What is the runtime complexity of swap_sort given an input list of N elements? (a) O(N) (c) O(N log N) (d) O(N 2 ) Page 5 of 8

Stacks (6 points): Consider the following linked implementation of the Stack ADT: class Stack: class Node: def init (self, val, next): self.val = val self.next = next def init (self): self.top = None def push(self, val): self.top = Stack.Node(val, self.top) def pop(self): val = self.top.val self.top = self.top.next return val WP1 Implement the method roll as part of the linked stack data structure. With an argument n 2, the method moves the top of the stack to the n th position, shifting all intervening elements towards the top. Examples: invoking roll(3) on a stack containing the values A, B, C, D, E (where A is the topmost value) would result in the stack B, C, A, D, E invoking roll(5) on a stack containing the values A, B, C, D, E would result in the stack B, C, D, E, A Restrictions/Assumptions: Your implementation should not change the value contained in any of the nodes nor create any new nodes. Instead, it should work by re-linking nodes. You may assume the argument, n, to roll is at least 2, and that the stack contains at least n values. Page 6 of 8

Heaps (8 points): WP2 (a) Consider the following ordered sequence of values to be added to a max-heap: 4, 8, 6, 5, 7, 9 Sketch the heap after adding each value and re-heapifying. WP2 (b) Consider the following implementation of third_largest, which is intended to return the third largest value from a max-heap (assuming the heap contains at least three values): class Heap: def init (self): self.data = [] def third_largest(self): if self.data[1] > self.data[2]: return self.data[2] return self.data[1] The method does not work reliably, however. Sketch a valid max-heap for which the above implementation fails. Page 7 of 8

Binary Search Trees (12 points): WP3 (a) Consider the following ordered sequence of values to be added to a (unbalanced) binary search tree: 8, 4, 9, 5, 7, 6 Sketch the tree after adding each value. WP3 (b) Consider the following binary search tree: 20 15 28 12 18 23 30 16 25 Sketch the resulting tree after removing the value 20 and replacing it with a suitable candidate (as in the deletion algorithm presented in class). WP3 (c) Implement the binary search tree method predecessor, which returns the largest value in the tree that is smaller than the argument. If there is no such value, the method returns None. You should not assume that the argument value itself is contained within the tree. E.g., calling predecessor(17) on the tree above should return the value 16. Note that a recursive helper function is already defined and called for you. Page 8 of 8