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

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

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

COMP103 Test. 5 Sept, 2007

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

EXAMINATIONS 2016 TRIMESTER 2

EXAMINATIONS 2017 TRIMESTER 2

SOLUTIONS. COMP103 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

Model Solutions. COMP 103: Test April, 2013

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

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

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

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

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

Model Solutions. COMP 103: Test May, 2013

CONTAINS SOLUTIONS!!!

COMP 103 Introduction to Data Structures and Algorithms

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

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

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

EXAMINATIONS 2013 COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

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

COMP 103 : Test. 2019, Jan 9 ** WITH 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 PRACTICE EXAM

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

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

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

CMSC 206: Data Structures Final Exam Reference May 2018

Class 32: The Java Collections Framework

COMP103 Summer: Midterm

COMP 103. Data Structures and Algorithms

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Java Collection Framework

DM550 Introduction to Programming part 2. Jan Baumbach.

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

CS61BL: Data Structures & Programming Methodology Summer 2014

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

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

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

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

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

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

Collections and Maps

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

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

Collections and Iterators. Collections

PIC 20A Collections and Data Structures

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

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

More Data Structures (Part 1) Stacks

COMP 103. Mid Trimester Test 17 August 2006

DM550 Introduction to Programming part 2. Jan Baumbach.

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

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

CSC 1214: Object-Oriented Programming

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

CS 307 Final Spring 2011

Programming Languages and Techniques (CIS120)

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

Topic 10: The Java Collections Framework (and Iterators)

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

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

Computer Science 62. Midterm Examination

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

An Interface with Generics

CSE 143 Au03 Final Exam Page 1 of 15

Basic Data Structures

Notes on access restrictions

recursive algorithms 1

JCF: user defined collections

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

CSE 143 Au03 Midterm 2 Page 1 of 7

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

CS171 Midterm Exam. October 29, Name:

HEAPS & PRIORITY QUEUES

Basic Data Structures 1 / 24

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

COMP 103 RECAP-TODAY. Hashing: collisions. Collisions: open hashing/buckets/chaining. Dealing with Collisions: Two approaches

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Practical Session 3 Java Collections

THE UNIVERSITY OF WESTERN AUSTRALIA

182 review 1. Course Goals

csci 210: Data Structures Stacks and Queues

CS11 Java. Winter Lecture 8

CS 307 Midterm 2 Fall 2008

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

Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Outline. iterator review iterator implementation the Java foreach statement testing

Building Java Programs

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Lecture 6: ArrayList Implementation

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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 V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 Trimester 1, MID-TERM TEST COMP103 Introduction to Data Structures and Algorithms SOLUTIONS Time Allowed: 45 minutes Instructions: 1. Attempt all of the questions. 2. Read each question carefully before attempting it. 3. This test will be marked out of 45 marks (i.e. allocate one minute per mark). 4. Write your answers in the boxes in this test paper and hand in all sheets. 5. Documentation on some relevant Java classes, interfaces, and exceptions can be found at the end of the paper. Questions Marks 1. Collections [6] 2. Implementing a Stack [19] 3. Sorting [10] 4. Recursion [10] COMP103 continued...

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 2 continued...

Student ID:....................... Question 1. Collections [6 marks] (a) [6 marks] In the boxes below give a short (e.g. one sentence) description for each of the following collections identifying their key feature(s). Bag Can have duplicates. No restrictions. Set Cannot have duplicates. Map Maps keys to values. Cannot have duplicate keys Can have duplicate values. Stack Respects the Last In First Out order of access for values. Queue Respects the First In First Out order of access for values. List Assigns a position for each value stored. COMP103 3 continued...

Question 2. Implementing a Stack [19 marks] (a) [8 marks] Complete the following class that implements a Stack using an array. Note that pop() should through an EmptyStackException if the stack is empty. public class Stack<E> { static int INITIAL CAPACITY = 2; E[] data; public Stack() { public void push(e item) { public E pop() { (Question 2 continued on next page) COMP103 4 continued...

Student ID:....................... (Question 2 continued) ANSWER CODE (2 marks for each of the first 3 methods and 2 marks for ensure- Capacity): public class Stack<E> { static int INITIAL CAPACITY = 2; E[] data; int count; public Stack() { data = (E[]) new Object[INITIAL CAPACITY]; count = 0; public void push(e item) { ensurecapacity(); data[count] = item; count++; public E pop() { if (count <= 0) throw new EmptyStackException(); E item = data[count 1]; count ; return item; public void ensurecapacity() { if (count >= data.length) { E[] newdata = (E[]) new Object[data.length 2]; for ( int i = 0; i < count; i++) { newdata[i] = data[ i ]; data = newdata; (Question 2 continued on next page) COMP103 5 continued...

(Question 2 continued) (b) [6 marks] Write an iterator for your Stack class above that is defined as a private inner class and goes through the items in the stack from the top to the bottom - in other words, the first item should be the one that would have come off first if a pop method were to be called. For simplicity, assume that you don t need to implement the remove method and that you don t need to worry about anyone modifying the Stack while you are iterating through it. import java. util. Iterator ; public class Stack<E> implements Iterable<E> { // CODE FROM PREVIOUS SUBQUESTION IMPLEMENTING STACK public Iterator <E> iterator() { return new StackIterator<E>(this); private class StackIterator<E> implements Iterator<E> { Stack<E> stack; int position ; public StackIterator (Stack<E> stack) { public boolean hasnext() { public E next() { public void remove() { throw new UnsupportedOperationException(); (Question 2 continued on next page) COMP103 6 continued...

Student ID:....................... (Question 2 continued) ANSWER CODE (2 marks each of the first 2 methods and 2 marks for the next()) import java. util. Iterator ; public class Stack<E> implements Iterable<E> { // CODE FROM PREVIOUS SUBQUESTION IMPLEMENTING STACK public Iterator <E> iterator() { return new StackIterator<E>(this); private class StackIterator<E> implements Iterator<E> { Stack<E> stack; int position ; public StackIterator (Stack<E> stack) { this. stack = stack; this. position = stack.count; public boolean hasnext() { return this. position > 0; public E next() { this. position ; return this. stack.data[this. position ]; public void remove() { throw new UnsupportedOperationException(); (Question 2 continued on next page) COMP103 7 continued...

(Question 2 continued) (c) [3 marks] What will the following code print when using the Stack you have just implemented? public static void main(string[] a) { Stack<String> ss = new Stack<String>(); ss.push("a"); ss.push("b"); ss.push("c"); System.out.println(ss.pop() + ss.pop() + ss.pop()); for (String s : ss) System.out.println(s); System.out.println("------"); CBA (d) [2 marks] What is the best order of items when iterating through a Stack? Justify your answer. The order is depends on the user: both from last to first and from first to last can make sense in different situations and as stack processes items in first in first out order, neither is a perfect fit. COMP103 8 continued...

Student ID:....................... Question 3. Sorting [10 marks] (a) [3 marks] Consider the insertion sort as discussed in the lectures. Give an example of an array of 10 elements that would produce the best cost behaviour for insertion sort and explain briefly why. A, B, C, D, E, F, G, H, I, J Because insertion step will always pick the best letter that will be greater than the end of the sorted part and thus there will be no need to shift items. (b) [3 marks] Consider the insertion sort as discussed in the lectures. Give an example of an array of 10 elements that would produce the worst cost behaviour for insertion sort and explain briefly why. J, I, H, G, F, E, D, C, B, A Because insertion step will always have to take the next item all the way down to the start of the sorted part thus maximising the number of shifts required. (c) [2 marks] What does it mean for a sorting algorithm to be stable? If two items are the same (equal) their order will be the both before and after the sort is performed. (d) [2 marks] Imagine you are given two sorting algorithms: one that has worst cost O(nlog(n)) and one that has worst cost O(n 2 ). Which of these two algorithms is going to sort 1 trillion integers faster? Justify your answer. We do not know as the cost is given for the almost all values of n in the long term. There can be exceptions (e.g. when a specific case of n = 1000000000000 is given) as to which one is faster, rather the important part is that overall the O(nlog(n)) algorithm will be faster. COMP103 9 continued...

Question 4. Recursion [10 marks] (a) [4 marks] Fix the recursive code below that tries to calculate a factorial. A reminder: n! = 1 2... (n 1) n. public int factorial ( int n) { if (n > 1) { return n factorial (n); else { return 1; ANSWER TO ABOVE: Replace recursive call to factorial(n) with factorial(n-1)! (b) [4 marks] Write a recursive function that returns the nth Fibonacci number. A reminder that Fibonacci sequence is as follows: 1, 1, 2, 3, 5, 8,..., f n 2, f n 1, f n,... where f n = f n 1 + f n 2. public int fib ( int n) { ANSWER: public int fib ( int n) { if (n < 1) return 0; if (n == 1) return 1; if (n == 2) return 1; return fib (n 2) + fib(n 1); (c) [2 marks] Explain why this recursive version of fib is particularly inefficient. Justify your answer. The two calls mean that the same number is recalculated at least twice. If one could cache these intermediate results somehow, the calculation would work much faster! ******************************** COMP103 10

Appendix (may be removed) Brief (and simplified) specifications of some relevant interfaces and classes. public class Scanner public boolean hasnext(); // there is more to read public String next (); // return the next token (word) public String nextline (); // return the next line public int nextint (); // return the next integer public 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 public interface Iterator <E> public boolean hasnext(); public E next(); public void remove(); public interface Iterable <E> public Iterator <E> iterator(); public interface Comparable<E> public int compareto(e o); public interface Comparator<E> public int compare(e o1, E o2); // Can use in the for each loop // Can compare this to another E // Can use this to compare two E s

public class Collections public void sort( List <E>, Comparator<E>) public interface Collection<E> public boolean isempty(); public int size (); public boolean add(); public Iterator <E> iterator(); public interface List <E> extends Collection<E> // Implementations: ArrayList public E get( int index); public void set( int index, E element); public void add(e element); public void add(int index, E element); public void remove(int index); public void remove(object element); public interface Set extends Collection<E> // Implementations: ArraySet, SortedArraySet, HashSet public boolean contains(object element); public boolean add(e element); public boolean remove(object element); public interface Queue<E> extends Collection<E> // Implementations: ArrayQueue, LinkedList public E peek (); // returns null if queue is empty public E poll (); // returns null if queue is empty public boolean offer (E element); public 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); public interface Map<K, V> // Implementations: HashMap, TreeMap, ArrayMap public V get(k key); // returns null if no such key public void put(k key, V value); public void remove(k key); public Set<Map.Entry<K, V>> entryset();