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

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

COMP 103 : Test. 2018, Sept 12//(Corrected) ** 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 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 PRACTICE EXAM

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

Model Solutions. COMP 103: Test May, 2013

Model Solutions. COMP 103: Test April, 2013

EXAMINATIONS 2016 TRIMESTER 2

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

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 2017 TRIMESTER 2

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

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

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

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

COMP103 Test. 5 Sept, 2007

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

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

CONTAINS SOLUTIONS!!!

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

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

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 2012 END-OF-YEAR COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS61BL: Data Structures & Programming Methodology Summer 2014

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

COMP 103 Introduction to Data Structures and Algorithms

DM550 Introduction to Programming part 2. Jan Baumbach.

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

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

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

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

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

Collections Framework: Part 2

Class 32: The Java Collections Framework

COMP 102: Test August, 2017

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

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

CS2110: Software Development Methods. Maps and Sets in Java

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

Fall 2017 Mentoring 7: October 9, Abstract Data Types

Java Collections Framework. 24 April 2013 OSU CSE 1

Programming Languages and Techniques (CIS120)

Csci 102: Sample Exam

CS61BL: Data Structures & Programming Methodology Summer 2014

Model Solutions. COMP 102: Test 1. 6 April, 2016

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

Topic 10: The Java Collections Framework (and Iterators)

Programming Languages and Techniques (CIS120)

Collections (Java) Collections Framework

CSC148H Week 3. Sadia Sharmin. May 24, /20

CONTAİNERS COLLECTİONS

COMP 102: Test August, 2017

University of Maryland College Park Dept of Computer Science

Generics Collection Framework

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

CSE 143 Au03 Final Exam Page 1 of 15

CMSC 206: Data Structures Final Exam Reference May 2018

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

CS 314 Midterm 2 Fall 2012

PIC 20A Collections and Data Structures

COMP 102/112 : Test. 2019, Apr 1 ** WITH SOLUTIONS **

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS2110: Software Development Methods. Maps and Sets in Java

DM550 Introduction to Programming part 2. Jan Baumbach.

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

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

Practical Session 3 Java Collections

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

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc.

Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps

Java Magistère BFA

182 review 1. Course Goals

Programming Languages and Techniques (CIS120)

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

Model Solutions. COMP 102: Test. 18 August, 2016

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

CS 314 Exam 2 Spring 2018

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Programming Languages and Techniques (CIS120)

More Data Structures (Part 1) Stacks

An Interface with Generics

Chapter 5 Java Collection. Wang Yang

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

Java Collections Framework

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

CS 302 ALGORITHMS AND DATA STRUCTURES

Collections, Maps and Generics

Model Solutions. COMP 102: Test May, 2015

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

Model Solutions. COMP 102: Test March, 2014

ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011

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

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Transcription:

Family Name:.............................. Other Names:...................................... Signature.................................. COMP 103 : Test 2019, Jan 9 ** WITH SOLUTIONS ** Instructions Time allowed: 50 minutes Attempt all questions. There are 50 marks in total. Write your answers in this test paper and hand in all sheets. If you think some question is unclear, ask for clarification. Brief Java documentation is provided with the test This test contributes 18% of your final grade (But your mark will be increased to your exam mark if that is higher.) You may use dictionaries. You may write notes and working on this paper, but make sure your answers are clear. Questions Marks 1. Properties of Collections [12] 2. Using a Stack for Bracket Matching [13] 3. Using Collections [15] 4. Cost of Algorithms [10] TOTAL:

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. COMP 103 (Test) Page 2 of 12

Question 1. Properties of Collections [12 marks] The properties of four different collections are given below. For each list of properties, please identify which type of collection is being described. (a) [3 marks] The order of items in the collection is important Items can only be removed from one end. Items are added and removed from the same end. The last item added is the first item to be removed A stack (b) [3 marks] The order of items in the collection is important Items can only be removed from one end. Items are added and removed from opposite ends. The first item added is the first item to be removed A queue (c) [3 marks] The time an item is added does not affect when it is removed Items must be added with a unique key The order of items in the collection is unimportant Items are stored as key-value pairs A map (d) [3 marks] The time an item is added does not affect when it is removed The collection cannot contain any duplicate items The order of items in the collection is unimportant A set COMP 103 (Test) Page 3 of 12

Question 2. Using a Stack for Bracket Matching [13 marks] An integral part of any good code editor is being able to tell if your { s and s match up, or to catch that missing ). For this question, you are to implement an algorithm that can be used to check if all of the brackets in a segment of code match up correctly, and in the right order. You must use a Stack<Character>. There are fours kinds of bracket: { and - Curly brackets ( and ) - Round brackets [ and ] - Square brackets < and > - Angle brackets Some notes: The code that your method is checking is represented using a CharSequence, which is a more general way of representing a String-like object. Documentation has been provided in the appendix. You may ignore any character in the input that is not an opening or closing bracket. Brackets can appear inside each other, but they must be closed in the correct order. E.g: ( [ ] ) is valid, but ( [ ) ] is not. All open brackets must be closed. E.g: ( [ ] is not valid because the first ( is never closed. A closing bracket without an opening bracket is invalid. If the input is valid, the method should return true. Otherwise it should return false. Use a Stack<Character> to hold all the opening brackets. Some useful methods have been provided / Returns true if the argument is a valid open bracket / public boolean isopenbracket(char c) { return ( c == { c == ( c == [ c == < ); / Returns true if the argument is a valid close bracket / public boolean isclosebracket (char c) { return ( c == c == ) c == ] c == > ); / Returns true if the closing bracket matches the opening bracket / public boolean matches(char opening, char closing ) { return ( ( opening == { && closing == ) ( opening == ( && closing == ) ) ( opening == [ && closing == ] ) ( opening == < && closing == > ) );. (Question 2 continued on next page) COMP 103 (Test) Page 4 of 12

(Question 2 continued) / Check for matching brackets. Returns true if all of the brackets match, or false if there is any sort of mismatch The input is sequence of characters You need to use a Stack<Character> if a character is not an open or close bracket, skip it / public boolean checkbrackets(charsequence input) { Stack<Character> stack = new Stack<>(); for( int i =0; i<input.length (); i ++) { char c = input.charat(i ); if (isopenbracket(c)) { stack.push(c); else if ( isclosebracket (c)) { if (stack.isempty()) return false ; if (!matches(c, stack.pop())) return false ; if (! stack.isempty()) return false ; return true; COMP 103 (Test) Page 5 of 12

Question 3. Using Collections [15 marks] This question involves part of a program for managing the sales representatives of a company that need to make visits to the clients of the company. The program has a field to store a Map of Queues of Clients, one Queue for each sales representative: private Map<String, Queue<Client>> clientqueues = new HashMap<String, Queue<Client>>(); The Client class includes the following method: / Returns the name of the Sales Representative for this client / public String getsalesrep (){... (a) [7 marks] Complete the following assignvisits(...) method which is given a list of Clients that need to be visited in the next week, and must put each Client on the correct queue in the clientqueues map. The getsalesrep method of Client will return the name of the sales representative who is responsible for the client. If a sales representative is not already a key in the clientqueues Map, assignvisits should add the sales representative to the Map, along with a new Queue. public void assignclients ( List <Client> clients ){ for ( Client client : clients ){ String name = client.getsalesrep(); if (! clientqueues. containskey(name)){ clientqueues. put(name, new ArrayQueue<Client>()); clientqueues. get(name).offer( client ); (Question 3 continued on next page) COMP 103 (Test) Page 6 of 12

(Question 3 continued) (b) [8 marks] Complete the following listdailyvisits() method which will print out the name of each sales representative and the clients that they must visit today. Each representative can visit at most four clients in one day. For each sales representative in the clientqueues Map, the first four clients on their queue should be removed from the queue and printed. If there are fewer than four clients on the queue, a should be printed in place of the missing clients. The Client class has a getname method that will return a String that is the name of the client. For example, if clientqueues had just two sales representatives, and the second had only two clients in their queue, the method might print: Jamie McCarty: Sun Enterprises Smith Booster Engines Planetary expeditions Space Robotics Lindsay Young: Launch Pad Logistics Hydrogen Fuel Systems -- -- public void listdailyvisits (){ for ( String name : clientqueues. keyset()){ UI. println (name+": "); for( int i=0; i<4; i++){ if (clientqueues. get(name).isempty()){ UI. println (" --"); else { Client client = clientqueues.get(name).poll (); UI. println ( client.getname()); COMP 103 (Test) Page 7 of 12

Question 4. Cost of Algorithms [10 marks] The two fragments of code below are very similar, and both of them reverse the input list. Assume the size of the list is n. For each fragment, work out the cost (in Big-O notation) by working out the cost of performing each line once working out the number of times each line will be performed computing the total cost Pay close attention to the differences between the two fragments. (a) [3 marks] public void reverse1 ( List <String> input){ Stack<String> stack = new Stack<String>(); while (! input.isempty()) { String s = input.remove(0); // cost= O( n ) times= n stack.push(s); // cost= O( 1 ) times= n while(!stack.isempty()) { String s = stack.pop(); // cost= O( 1 ) times= n input.add(s); // cost= O( 1 ) times= n Total Cost = O( n 2 ). (b) [3 marks] public void reverse2 ( List <String> input){ Queue<String> queue = new ArrayDeque<String>(); while (! input.isempty()) { String s = input.remove(input. size () 1); // cost = O( 1 ) times= n queue. offer (s ); // cost= O( 1 ) times= n while(!queue.isempty()) { String s = queue.poll (); // cost= O( 1 ) times= n input.add(s); // cost= O( 1 ) times= n Total Cost = O( n ). (Question 4 continued on next page) COMP 103 (Test) Page 8 of 12

(Question 4 continued) (c) [4 marks] A program uses a HashMap to store all the tax details for citizens. When the Map has 100,000 citizens, the program takes 7 microseconds to retrieve the tax details for a given citizen. If there were 100,000,000 citizens, how long would you expect the program to take to retrieve the tax details for a citizen? Briefly explain why. It would still take 7 microseconds, as a HashMap is O(1) for retrieval * * * * * * * * * * * * * * * COMP 103 (Test) Page 9 of 12

Documentation Brief, simplified specifications of some relevant Java collection types and classes. Note: E stands for the type of the item in the collection. interface Collection <E> public boolean isempty() // cost : O(1) for all standard collection classes public int size () // cost : O(1) for all standard collection classes public void clear () public boolean add(e item) public boolean contains(object item) public boolean remove(object element) interface List <E> extends Collection<E> // Implementations: ArrayList public boolean isempty() public int size () public void clear () public E get( int index) public E set( int index, E element) public boolean contains(object item) public void add(int index, E element) public E remove(int index) public boolean remove(object element) // cost : O(1) // cost : O(1) // cost : O(n) // cost : O(n) ( unless index is close to the end.) // cost : O(n) ( unless index is close to the end.) // cost : O(n) interface Set extends Collection<E> // Implementations: HashSet, TreeSet public boolean isempty() public int size () public void clear () public boolean add(e item) // cost : O(1) for HashSet, O(log(n)) for TreeSet public boolean contains(object item) // cost : O(1) for HashSet, O(log(n)) for TreeSet public boolean remove(object element) // cost : O(1) for HashSet, O(log(n)) for TreeSet interface Map<K, V> // Implementations: HashMap, TreeMap public V get(k key) // cost : O(1) for HashMap, O(log(n)) for TreeMap public V put(k key, V value) // cost : O(1) for HashMap, O(log(n)) for TreeMap public V remove(k key) // cost : O(1) for HashMap, O(log(n)) for TreeMap public boolean containskey(k key) // cost : O(1) for HashMap, O(log(n)) for TreeMap public Set<K> keyset() // cost : O(1) public Collection <V> values() // cost : O(1) public Set<Map.Entry<K,V>> entryset() // cost : O(1) // ( get returns null if the key is not present ) // ( get put and remove return the old value, if any)

interface Queue<E> extends Collection<E> // Implementations: ArrayDeque, LinkedList, PriorityQueue public boolean isempty() public int size () public void clear () public E peek () // cost : O(1) for ArrayDeque, LinkedList, O(1) for PriorityQueue public E poll () // cost : O(1) for ArrayDeque, LinkedList, O(log(n)) for PriorityQueue public boolean offer (E element) // cost : O(1) for ArrayDeque, LinkedList, O(log(n)) for PriorityQueue // ( peek and poll return null if the queue is empty) class Stack<E> implements Collection<E> public boolean isempty() public int size () pubic void clear () public E peek () // cost : O(1) public E pop () // cost : O(1) public E push (E element) // cost : O(1) // ( peek and pop return null if the queue is empty) class Collections public void sort ( List <E> list); public void sort ( List <E> list, (E e1, E e2) >{...); public void swap(list<e> list, int i, int j ); public void reverse ( List <E> list); public void shuffle ( List <E> list); // cost = O(n log(n )), but O(n) if almost sorted // cost = O(n log(n )), but O(n) if almost sorted // cost = O(1) // cost = O(n) // cost = O(n) interface Comparable<E> // All Comparable objects have a compareto method: public int compareto(e other); // returns // ve if this comes before other ; // +ve if this comes after other, // 0 if this and other are the same interface CharSequence public char charat( int index ); // Returns the char value at the specified index public int length (); // Returns the length of this CharSequence