CompSci-61B, Data Structures Final Exam



Similar documents
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structures and Algorithms Written Examination

DATA STRUCTURES USING C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Sample Questions Csci 1112 A. Bellaachia

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Heap Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Chapter 3: Restricted Structures Page 1

Data Structure [Question Bank]

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Exam study sheet for CS2711. List of topics

Social Media Mining. Graph Essentials

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Java Software Structures

Binary Search Trees CMPSC 122

Algorithms and Data Structures Written Exam Proposed SOLUTION

Analysis of Binary Search algorithm and Selection Sort algorithm

CmpSci 187: Programming with Data Structures Spring 2015

Ordered Lists and Binary Trees

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

HW3: Programming with stacks

Data Structures and Algorithms

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

PES Institute of Technology-BSC QUESTION BANK

Converting a Number from Decimal to Binary

Analysis of a Search Algorithm

Data Structures in Java. Session 15 Instructor: Bert Huang

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Lecture Notes on Binary Search Trees

Data Structures in the Java API

Lecture Notes on Binary Search Trees

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Review of Hashing: Integer Keys

Lecture 9. Semantic Analysis Scoping and Symbol Table

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Algorithms and Data Structures

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

The ADT Binary Search Tree

Introduction to Programming System Design. CSCI 455x (4 Units)

Data Structures. Level 6 C Module Descriptor

International Journal of Software and Web Sciences (IJSWS)

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

DATA STRUCTURE - STACK

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Pseudo code Tutorial and Exercises Teacher s Version

The Goldberg Rao Algorithm for the Maximum Flow Problem

Algorithms and Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

10CS35: Data Structures Using C

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Lecture 2: Data Structures Steven Skiena. skiena

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Data Structures and Data Manipulation

Shortest Path Algorithms

Binary Trees and Huffman Encoding Binary Search Trees

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Lecture Notes on Linear Search

DATA STRUCTURE - QUEUE

Zabin Visram Room CS115 CS126 Searching. Binary Search

Chapter 8: Bags and Sets

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Class 32: The Java Collections Framework

Common Data Structures

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Introduction to Data Structures and Algorithms

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Binary Search Trees (BST)

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Stacks. Linear data structures

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Cpt S 223. School of EECS, WSU

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

TREE BASIC TERMINOLOGIES

Intrusion Detection via Static Analysis

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Binary search algorithm

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Algorithm Design and Analysis

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Programming with Data Structures

Transcription:

Your Name: CompSci-61B, Data Structures Final Exam Your 8-digit Student ID: Your CS61B Class Account Login: This is a final test for mastery of the material covered in our labs, lectures, and readings. It consists of 11 questions worth a total of 100 points. Circle the correct answer for the multiple-choice questions, and write in the space provided for the essay questions. This exam is open book and open note. Partial credit is possible. 1. Write a class file for compsci61b.mystack, extending your compsci61b.myarraylist class, with the following changes: [12 points] add O(1) versions of public boolean push(t value), public T pop(), and public T peek(). iterate from most recently added value to oldest value pop order (that is, FILO) throw UnsupportedOperationExceptions (with any text you want) for the 2 add methods, remove, and the iterator s remove. Here is a list of the methods in compsci61b.myarraylist: public MyArrayList() public boolean add(t value) public boolean add(int, T value) public T remove(int index) public void clear() public boolean replace(int index, T value) public T getentry(int index) public boolean contains(t value) public int size() public boolean isempty() public boolean isfull() public String tostring() public Iterator<T> iterator() -- should be in FILO order package compsci61b; public class MyStack<T> extends MyArrayList<T> Write your code here: CompSci-61B Midterm Exam, page 1 continued on next page

Write your code here (continued): CompSci-61B, Data Structures Final Exam CompSci-61B Midterm Exam, page 2

2. Write a class file for compsci61b.myprorityqueue, extending your compsci61b.mysortedlinkedlist class, with the following changes: [10 points] add public T poll() and public T peek(). ( poll should return the smallest value in the queue, or null if the queue is empty) throw UnsupportedOperationExceptions (with any text you want) for the add methods, remove, and the iterator s remove. Here is a list of the methods in compsci61b. MySortedLinkedList: public boolean remove(t value) public boolean add(t value) public boolean contains(t value) public boolean add(int index, T value) unsupported operation throws an exception public boolean replace(int index, T value) unsupported operation throws an exception public T getentry(int index) public boolean contains(t value) public int size() public boolean isempty() public boolean isfull() public String tostring() public void clear() public Iterator<T> iterator() should be in order smallest to largest package compsci61b; public class MyStack<T> extends MyArrayList<T> Write your code here: CompSci-61B Midterm Exam, page 3

3. Rewrite MySortedArrayList.add so that it has a best case of O(1): [6 points] public boolean add(t value) // the original version, O(n) efficiency if (value == null) return false; if (nvalues == data.length) doublethearraysize(); // find the insertion location int index = 0; for (; index < nvalues; index++) if (value < data[index]) break; // shift (if necessary) and insert for (int i = nvalues; i > index; i--) data[i] = data[i - 1]; // move right data[index] = value; // copy into array ++nvalues; return true; public boolean add(t value) // the rewritten version with an O(1) best case Write your code here: CompSci-61B Midterm Exam, page 4

4. Imagine that you are grading the programming exercise on binary search trees for a future CS61B class and to make your job easier, you decided program a method "isbst" that would tell you whether the binary trees constructed by your students are actually Binary Search Trees. That is, isbst should return true if the input Binary Tree satisfies the Binary Search Tree property and false if not. (Assume that the input is a valid binary tree we just want to see if it is also a binary search tree.) These trees do not implement generics they store Integer values. Assume that you would copy/paste this method into the students submitted source code, and place calls to the method in the students main test code. All students name the private inner node class the same, per the lab specifications. Your program must run in time linear to the size of the input tree. [15 points]...package and import statements... public class MyBinaryTree // per the specification private class Node // per the specification private Integer value; // per the specification private Node left; // per the specification private Node right; // per the specification...possibly methods here......student s code (not you!)... public boolean isbst(node tree) // copy/paste this into student s code Write your code here: CompSci-61B Midterm Exam, page 5 continued on next page

Write your code here (continued): CompSci-61B, Data Structures Final Exam...student s code (not you!), including main test method... CompSci-61B Midterm Exam, page 6

5. Explain the big oh efficiency for "contains" for each of the following (Circle one each) [6 points total] unsorted array: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] sorted linked list: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] balanced binary tree: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] chained hash table: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] 6. Regarding hash tables: [15 points total] a. While using a hash table with chaining and no maximum load factor, you found out that you inserted too many elements into the table. To be more precise, you actually inserted n log n elements into the hash table whose array size is n. (a) What would be the average lookup time for your hash table now? (b) What if you doubled the size of the Hash Table and rehashed the same elements, what would be the average lookup time now? Assume that you can insert however many elements you want in whatever order you want using whatever hash function you want. Express the answers in big oh notation. [4 points] Answer here: a. b. b. You have two hash tables of the same size, and they are hashing the same set of keys using the same hash function. However, one hash table uses linear probing and one uses chaining. Suppose you can insert however many elements you want in whatever order you want, is it possible for one hash table to have, on average, linear time lookup speed while the other has, on average, constant time lookup speed? Explain. [7 points] Answer here: c. In our implementation of linear probing in hash tables, our array elements could be entry references, null, or blank. What is the purpose of using blanks instead of nulls? [4 points] Answer here: CompSci-61B Midterm Exam, page 7

7. Specify which data structure you would use in each of these scenarios. Choose from one of the following. If multiple answers are possible, choose the most efficient one. Briefly explain your choices. [9 points total] Queue Stack Priority Queue Binary Search Tree Graph Hash Table a. You're simulating a keyboard and would like to process the keystrokes and then print them on the screen. [1.5 points] b. You need to process the service events for lab 8 but now, the events all take the same amount of time to complete. [1.5 points] c. You're writing a graph traversal program and you would like to use a separate additional data structure to keep track of whether a vertex has been visited or not. [1.5 points] d. You're writing expression parsing program for an early HP calculator that uses reverse polish notation. [1.5 points] e. You want to be able to input the prices of many different cars, and then find cars that are neither too expensive nor too cheap for your customers. [1.5 points] f. You would like to store all possible board configurations for the game of chess and you also want to store what moves you can make to reach one configuration from another. [1.5 points] CompSci-61B Midterm Exam, page 8

8. Suppose we modify the depth-first traversal algorithm for a graph to push the node into a stack before traversing its neighbors and then pop the stack at the end, in accordance with the following pseudocode: boolean traverse(vertex v) stack.push(v);... pre-process v... for-all Vertex u where (v, u) is an edge if (!stack.contains(v)) traverse(u); stack.pop();... post-process v... Answer the following 2 questions Provide examples: [7 points total] a. Will this modified depth-first search algorithm work on a directed graph (digraph), ie. have the same behavior as the normal depth-first search? [5 points] b. Will it work on an undirected graph? [2 points] CompSci-61B Midterm Exam, page 9

9. Write a static method which is supposed to take an array of ints between 0 and m inclusive, and count the number of repetitions in the array. The array and m are arguments in the method s parameter list. For example, an array of 1, 3, 1, 4, 3, 3 has 3 repetitions, because the number 1 repeats once and the number 3 repeats twice. Furthermore, your program needs to run in time O(n+m) where n is the size of the array. The variable count is a hint, you don't need to use it you may cross it out -- and you can declare any variables of your own. [10 points] public static int countrepetitions(int[] arr, int m) int[] count = new int[m + 1]; Write your code here: CompSci-61B Midterm Exam, page 10

10. In a graph representation of a maze, the entry point is called A and the exit is Z. There is only one direct path through the maze (without doubling back or circling). Which traversal method(s) will find the direct route from A to Z? Assume that the maze is represented by a series of nodes at the decision points, and the edges include the distance between nodes. (Circle all that apply, and explain) [4 points] (a) depth first (b) breadth first (c) shortest route (d) cheapest route 11. In the BST shown to the right, the value I inside in the node marked 1 is to be removed. This may involve the swapping of values among nodes, and will involve the removal of one node. There are two possible solutions to this problem -- identify the changes, in order, for both: [6 points] First possible solution (fill in the blanks): Put value into node # Remove node # Second possible solution (fill in the blanks): Put value into node # Put value into node # Put value into node # Put value into node # Remove node # use as many of these blank as you need CompSci-61B Midterm Exam, page 11