EXAMINATIONS 2016 TRIMESTER 2

Similar documents
EXAMINATIONS 2017 TRIMESTER 2

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Model Solutions. COMP 103: Test May, 2013

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

****** NOTE!! THIS CONTAINS SOLUTIONS ***** TRIMESTER 2

WITH SOLUTIONS!!! WARNING: THIS COPY CONTAINS SOLUTIONS!!! COMP103 Introduction to Data Structures and Algorithms

Model Solutions. COMP 103: Test April, 2013

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

COMP 103 : Test. 2018, Sept 12//(Corrected)

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

COMP 103 : Test. 2019, Jan 9 ** WITH SOLUTIONS **

COMP 103 Introduction to Data Structures and Algorithms

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

EXAMINATIONS 2010 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2010 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2013 COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

CONTAINS SOLUTIONS!!!

COMP103 Test. 5 Sept, 2007

COMP 103 : Test. 2018, Sept 12//(Corrected) ** WITH SOLUTIONS

EXAMINATIONS 2011 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2006 SUMMER TRIMESTER. COMP103 Introduction to Data Structures and Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

CMSC 206: Data Structures Final Exam Reference May 2018

EXAMINATIONS 2012 END-OF-YEAR COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS61BL: Data Structures & Programming Methodology Summer 2014

DM550 Introduction to Programming part 2. Jan Baumbach.

COMP 103. Data Structures and Algorithms

Summer Final Exam Review Session August 5, 2009

Class 32: The Java Collections Framework

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

COMP 250 Midterm #2 March 11 th 2013

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS61BL: Data Structures & Programming Methodology Summer 2014

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Lecture 13: AVL Trees and Binary Heaps

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

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

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

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

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

HEAPS & PRIORITY QUEUES

CSE 143 Au03 Final Exam Page 1 of 15

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

182 review 1. Course Goals

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

AP Computer Science 4325

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

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

Programming II (CS300)

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

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

Te Whare Wananga o te Upoko o te Ika a Maui EXAMINATIONS 2003 END-YEAR COMP 103. Data Structures and Algorithms

CSE 2123: Collections: Priority Queues. Jeremy Morris

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

Computer Science E-22 Practice Final Exam

COMP 103. Mid Trimester Test 17 August 2006

Programming II (CS300)

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Topics for CSCI 151 Final Exam Wednesday, May 10

PRIORITY QUEUES AND HEAPS

CS 310: Maps and Sets and Trees

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2017 TRIMESTER 1 *** WITH SOLUTIONS *** COMP 261 ALGORITHMS and DATA STRUCTURES

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

Java Collection Framework

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

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

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

O T E Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 6. The Master Theorem. Some More Examples...

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2014 TRIMESTER 1 *** WITH SOLUTIONS ***

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

CONTAİNERS COLLECTİONS

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Collections Questions

Recitation 9. Prelim Review

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

CS 307 Final Spring 2008

Transcription:

T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2016 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Time Allowed: TWO HOURS CLOSED BOOK Permitted materials: Only silent non-programmable calculators or silent programmable calculators with their memories cleared are permitted in this examination. Non-electronic foreign language to English dictionaries are permitted. Other materials are not allowed. Instructions: Attempt all questions. Answer in the appropriate boxes or as instructed in the questions if you write your answer elsewhere, make it clear where your answer can be found. The exam will be marked out of 120 marks. Documentation on some relevant Java classes and interfaces can be found at the end of the paper. You may tear that page off if it helps. There are spare pages for your working and your answers in this exam, but you may ask for additional paper if you need it. Questions Marks 1. Stacks and Queues [10] 2. Complexity [10] 3. Linked Lists [10] 4. Binary Search Trees [15] 5. Heaps [15] 6. Hashing [15] 7. General Questions [20] 8. Iterators [10] 9. Recursion [15] COMP103 Page 1 of 24

1. Stacks and Queues (10 marks) (a) (5 marks) Postfix order (also known as Reverse Polish Notation ) rewrites the expressions to have the operations following the arguments. For example, 6 + 4 becomes 6 4 + and 9 becomes 9. Rewrite the following arithmetic expression in postfix order: ((5 2)/(4 6 + 3)) TO DO (b) (5 marks) What does the following code print? Queue<String> q = new ArrayDeque<String>(); q.offer("z"); q.offer("y"); q.offer("x"); q.poll(); q.offer("a"); q.offer("b"); q.poll(); UI.println(q); q.poll(); q.offer("c"); q.offer("d"); UI.println(q); q.offer("e"); q.offer("f"); q.poll(); UI.println(q); [X, A, B] [A, B, C, D] [B, C, D, E, F] COMP103 Page 2 of 24

2. Complexity (10 marks) (a) (3 marks) For the following functions, give the complexity class they fall into. 1 2 n4 + 200n 3 + 99 O( ) n + n + n O( ) 2 n + 1 O( ) (b) (3 marks) For the following operations, give the complexity class they fall into. Adding an element to the front of a LinkedList of length n O( ) Method contains in ArrayList of length n O( ) Iterating through all n elements in a set of m bags O( ) (c) (4 marks) List the following sorting algorithms from lowest to highest in terms of their worst case complexity: Bubble Sort, Quick Sort, Merge Sort, Heap Sort, Insertion Sort, Selection Sort. In cases where they have the same complexity list them in alphabetical order. From lectures. COMP103 Page 3 of 24

3. Linked Lists (10 marks) (a) (5 marks) Suppose you are working on a program that uses a List of episodes (ie. objects of class Episode), implemented as a list of LinkedNodes. You are to write a method middleepisode for this program. The method is passed the head of the list as an argument, and should return a reference to the middle Episode in the list if the size is odd, and either of the middle nodes if the size is even. If no such item exists because the list is empty, it should return null. Your implementation of this method needs to be iterative. /* Returns the value in the middle node of a list starting at a given node TODO */ public Episode middleepisode (LinkedNode<Episode> list){ } COMP103 Page 4 of 24

(b) (5 marks) Give pseudocode for the algorithm for processing the nodes in a general tree in an iterative, breadth-first traversal. make a queue, put root on the queue, while queue not empty: dequeue put its children on the queue process node. COMP103 Page 5 of 24

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP103 Page 6 of 24

4. Binary Search Trees (15 marks) (a) (5 marks) Consider the following Binary Search Tree, that is being used to implement a Set. By drawing on the diagram below, show what happens as you add the following items (using the algorithm described in lectures) in the following order: 50, 36, 21, 17 COMP103 Page 7 of 24

(b) (5 marks) The binary search tree below is the same as the one before. Add nodes of your choice to change it into a complete binary search tree which represents a Set of items. If it helps to make your answer clearer, redraw the entire tree in the space below. COMP103 Page 8 of 24

(c) (3 marks) Again using the same binary search tree as before (re-drawn below) that represents a Set, show what the tree would look like if the values 2 and 37 were removed from the Set. (d) (2 marks) Now remove the value 20 from the resulting tree above (after deleting 2 and 37) and redraw the final tree below: COMP103 Page 9 of 24

5. Heaps (15 marks) (a) (10 marks) Show the heap that results from adding the following values to a heap that starts off empty: 15, 0, 25, 26, 27, 1, 2 (in that order). This is a max heap, with the largest value at the root (not the smallest). Draw both the partially-ordered tree representation, and the array representation of the heap. draw the partially ordered tree and the array: COMP103 Page 10 of 24

(b) (5 marks) Show that turning an unsorted array into a heap is O(n). You can follow the same argument as was presented in the lectures. TO DO COMP103 Page 11 of 24

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP103 Page 12 of 24

6. Hashing (15 marks) (a) (5 marks) Explain the difference between open addressing and closed addressing in hashing: TO DO COMP103 Page 13 of 24

(b) (10 marks) Describe two different collision resolution techniques for open addressing, and state advantages and disadvantages of each: 1st Technique: TO DO 2nd Technique: TO DO COMP103 Page 14 of 24

7. General Questions (20 marks) (a) (5 marks) Give an example of a unit test and describe what they are used for in software development. TO DO (b) (5 marks) Explain what the purpose is of Comparable interface in the Java Collections, and give a realistic example of where you might use it in a software project. TO DO COMP103 Page 15 of 24

(c) (5 marks) Consider the Java Map implementation. For some maps, the number of items in the set of values and the set of keys is different. Explain why it is the case and give an example of a map to illustrate your answer. TO DO (d) (5 marks) Why is it important to override both the equals method and the hashcode method in a class? What can go wrong if you override one but not the other? TO DO COMP103 Page 16 of 24

8. Iterators (10 marks) (a) (10 marks) In the box below write code for the three missing methods of an iterator for ArrayList: See lecture slides on Iterators and Iterable for the answers. private class ArrayListIterator<E> implements Iterator<E> { private ArrayList<E> list; private int nextindex; private boolean removeable = false; private ArrayListIterator(ArrayList<E> list) { } private boolean hasnext() { } private E next() { } } COMP103 Page 17 of 24

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP103 Page 18 of 24

9. Recursion (15 marks) Consider the following triple tree data structure (from the test): class TripleTreeNode { public TripleTreeNode left, middle, right; public String value; public TripleTreeNode(String value) { this.value = value; } } (a) (7 marks) Implement recursively a printall() method that will go inside TripleTreeNode class that prints all the nodes in the tree by doing a depth first traversal. Each node should be indented by the number of spaces that is equal to the depth of the node being printed. COMP103 Page 19 of 24

(b) (8 marks) (Hard) Now implement recursively the drawall() method that will also go inside TripleTreeNode class that draws all the nodes in the tree, while ensuring that all the nodes of each subtree do not draw over each other. Assume that you just need to draw circles (using UI) with a fixed size of 10 pixels diameter. Use UI.drawOval(x, y, width, height) where x,y is the top left corner and the width and height are both 10. You don t need to print any values in these circles - you just need to ensure that the children will have enough space to draw a subtree without interfering with the other nodes. * * * * * * * * * * * * * * * COMP103 Page 20 of 24

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP103 Page 21 of 24

SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP103 Page 22 of 24

Appendix (may be removed) Brief (and simplified) specifications of some relevant interfaces and classes. interface Collection<E> public boolean isempty() public int size() public boolean add(e item) public boolean contains(object item) public boolean remove(object element) public Iterator<E> iterator() interface List<E> extends Collection<E> // Implementations: ArrayList, LinkedList public E get(int index) public E set(int index, E element) public void add(int index, E element) public E remove(int index) // plus methods inherited from Collection interface Set extends Collection<E> // Implementations: ArraySet, HashSet, TreeSet // methods inherited from Collection interface Queue<E> extends Collection<E> // Implementations: ArrayDeque, LinkedList public E peek () // returns null if queue is empty public E poll () // returns null if queue is empty public boolean offer (E element) // returns false if fails to add // plus methods inherited from Collection class Stack<E> implements Collection<E> public E peek () // returns null if stack is empty public E pop () // returns null if stack is empty public E push (E element) // returns element being pushed // plus methods inherited from Collection interface Map<K, V> // Implementations: HashMap, TreeMap, ArrayMap public V get(k key) // returns null if no such key public V put(k key, V value) // returns old value, or null public V remove(k key) // returns old value, or null public boolean containskey(k key) public Set<K> keyset() // returns a Set of all the keys COMP103 Page 23 of 24

interface Iterator<E> public boolean hasnext(); public E next(); public void remove(); interface Iterable<E> public Iterator<E> iterator(); interface Comparable<E> public int compareto(e o); interface Comparator<E> public int compare(e o1, E o2); // Can use in the "for each" loop // Can compare this to another E // -ve if this less than o; +ve if greater // Can use this to compare two E s // -ve if o1 less than o2; +ve if greater class Collections public static void sort(list<e>) public static void sort(list<e>, Comparator<E>) public static void shuffle(list<e>, Comparator<E>) class Arrays public static <E> void sort(e[ ] ar, Comparator<E> comp); class Random public int nextint(int n); // return a random integer between 0 and n-1 public double nextdouble(); // return a random double between 0.0 and 1.0 class String public int length() public String substring(int beginindex, int endindex) COMP103 Page 24 of 24