PRACTICE FINAL EXAM 3

Similar documents
PRACTICE FINAL EXAM 2

PRACTICE FINAL EXAM #4 (DRESS REHEARSAL)

PRACTICE MIDTERM EXAM #2

PRACTICE MIDTERM EXAM #1

CS106X Handout 25 Autumn 2012 October 23 rd, 2012 CS106X Midterm Examination

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

Computer Science 302 Spring 2007 Practice Final Examination: Part I

1) Holiday Lights 30 Points

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Practice Final Examination #2

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

Final Examination CSE 100 UCSD (Practice)

12 Abstract Data Types

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS 104 (Spring 2014) Final Exam 05/09/2014

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Summer Final Exam Review Session August 5, 2009

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

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

Binary Search Trees Treesort

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

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

Largest Online Community of VU Students

Part I: Short Answer (12 questions, 65 points total)

CS 216 Exam 1 Fall SOLUTION

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

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

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

Practice Final Examination #2

COS 226 Algorithms and Data Structures Fall Midterm

INF2220: algorithms and data structures Series 4

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

UNIVERSITY REGULATIONS

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Programming Abstractions

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE 333 Midterm Exam 2/14/14

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

COS 226 Midterm Exam, Spring 2009

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CS106X Final Review. Rachel Gardner and Jared Bitz. slides adapted from Ashley Taylor, Anton Apostolatos, Nolan Handali, and Zachary Birnholz

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

EXAMINATIONS 2016 TRIMESTER 2

Postfix (and prefix) notation

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

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

Final Exam. Name: Student ID: Section: Signature:

Computer Science Foundation Exam

Computer Science Foundation Exam

Tutorial Letter 102/1/2012 Programming: Data Structures

CS 314 Exam 2 Spring

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS171 Final Practice Exam

Introduction to Computing II (ITI 1121) Final Examination

CSCE 2014 Final Exam Spring Version A

Programming Abstractions

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

COMP 103 Introduction to Data Structures and Algorithms

An Introduction to Trees

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

Lecture Notes on Tries

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Computer Science Foundation Exam

(the bubble footer is automatically inserted into this space)

Programming Abstractions

CSE332 Summer 2010: Final Exam

Lecture Notes on Tries

CSE 143 Sp04 Final Exam Sample Solution Page 1 of 12

CSE 12 Basic Data Structures. Dr. Cynthia Bailey Lee

3137 Data Structures and Algorithms in C++

Programming Abstractions

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Computer Science Spring 2005 Final Examination, May 12, 2005

CS61BL: Data Structures & Programming Methodology Summer 2014

CS302 Data Structures using C++

Instructor: Ashley Taylor Spring 2018 May 10, Midterm Exam NAME (LAST, FIRST): Problem TOTAL

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

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE 143 Au03 Final Exam Page 1 of 15

Lecture 18 Heaps and Priority Queues

ECE264 Spring 2013 Final Exam, April 30, 2013

Topic Binary Trees (Non-Linear Data Structures)

COS 226 Algorithms and Data Structures Spring Midterm Exam

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Algorithm and Data Structures, Supplemental Solution 2010

Transcription:

This practice exam is based on an actual final exam from CS106X (same topics coverage as CS106B, but somewhat higher expectations for mastery). The question types and mix of topics of our CS106B exam will be the same, but this practice may over-prepare you to some degree in terms of the difficulty of a couple of the individual problems. As my basketball coach would always say when we were conditioning a practice that is harder than the game is a good thing! --Cynthia CS106B Autumn 2017 PRACTICE FINAL EXAM 3 Instructor: Cynthia Lee Practice Exam NAME (LAST, FIRST): SUNET ID: @stanford.edu Problem Score Possible Heap BST Trees Graphs, Sorting Classes 1 2 3 4 6 TOTAL Instructions: The time for this exam is 3 hours. Use of anything other than a pencil, eraser, pen, one 8.5x11 page (one side) of notes, and the official textbook is prohibited. In particular, no computers or digital devices of any kind are permitted. Blank scratch paper may be provided by proctors and does not need to be turned in. PLEASE rip off the pages of library reference in the back of the exam and do not turn them in. Please do NOT insert new pages into the exam. Changing the number of pages in the exam confuses our automatic scanning system. You may use the back sides of the exam pages for more space. Thanks. SCPD and OAE: Please call or text 760-845-7489 if you have a question. Please sign before you begin: I agree to abide by the spirit and letter of the Honor Code, and to follow the instructions above. (Signature) (Date) (Start time - HH:MM zone)

1. Sorting. Show each step of insertion sort on the vector of integers shown below. The pseudocode for insertion sort is given as a reminder. print(vector) // Initial state means the values at this point in the code for (i = 1; i<vector size; i++) { j = i while (j > 0 and vector[j-1] > vector[j]) { temp = vector[j] vector[j] and vector[j-1] vector[j-1] = temp j } print(vector) // You write vector s values at this point in the code } Below is the initial state of the vector of values to sort. Fill in the rest of the steps for each time the print executes. You may not need all the provided blank vectors to complete your solution. 9 4 8 5 1 2 3 7 6

2. Heaps. Draw the tree structure that results from interpreting the data in the array below as a binary heap (using 0-based indexing where the 0 th index of the array is the leftmost box below). Then answer the question about your drawing. 30 20 12 9 13 10 2 7 5 1 DRAW: CIRCLE ONE: Is this a valid max-heap? YES NO

3. BSTs. We have implemented the Map ADT using a plain Binary Search Tree (no special balancing measures). Our node structure is defined as struct node { int key; int value; node* left; node* right; Draw a diagram of the BST that results from inserting the following (key, value) pairs in the order given. Please label each BST node with both the key and value. (5,2), (5,5), (9,3), (6,12), (7,5), (8,1) Diagram after inserting (5,2): Diagram after inserting (5,5): (5,2) This one is completed for you as a node formatting example. Diagram after inserting (9,3): Diagram after inserting (6,12): Diagram after inserting (7,5): Diagram after inserting (8,1)

4. Trees. Recall that one application of a Stack is to process an arithmetic expression written in postfix notation. We can also represent arithmetic expressions as a tree. Your job is to write a function that will convert a Stack of singleton expression nodes (where all the left and right pointers are initially set to NULL) into the correct tree structure. Note that you do not need to actually calculate the answer to the arithmetic expression. Here is an example: Input: Output: The struct for our expression tree nodes is as follows: struct Node { char operator; int value; Node * left; Node * right; The operator field can have three values: '+', '*', and 'N', where '+' and '*' represent the addition and multiplication operators, and 'N' means that this node represents a number in the expression, not an operator. When the operator is 'N', the value field contains the value of the number. The left and right fields point to the left and right children in the expression tree. Your function should have the following signature: Node* maketree(stack<node*>& expression); Return a pointer to the root of your newly constructed tree (if expression is empty, return NULL). When your function returns, expression should be empty (all contents popped). You may assume that the provided expression generates a valid tree (so the Stack should always have something remaining to pop when you need it to). Algorithm hints: Pop a Node* from the Stack. o If the popped Node is an operator, you ll need to go build its left subtree, and then, when that is complete, go build its right subtree. (There is a reason you are given the nodes in a Stack: if your algorithm is structured correctly, you will get them in just the right order.) o If the popped Node is a number, then it will have no children (it will be a leaf node). So you should stop building children along this branch of the tree when you get to a number.

Node* maketree(stack<node*>& expression) {

5. Hashing. Imagine we want to be able to store binary trees in a hash table. Recall that this means that we need a hash function that takes as input a binary tree and outputs an integer hash key in the range 0 to number-ofbuckets. We have designed the following recursive formula as the pseudocode for our hash function (it is parametrized to a simple integer hash function h 1, k buckets, and constant primes P and Q): hash(t) = (P * hash(t->left) * h(t->val) + Q * hash(t->right)) (mod k) Write a function simulatetreehash that, given a Vector<node*> of binary trees (each vector entry is a pointer to a root), returns a Map<int, Vector<node*>> showing, for each integer bucket index, which trees would fall into that bucket when using the above formula. Assume you are provided the function h with this signature: int h(int val); but you will write all the rest of the necessary code (helper/wrapper functions are ok). struct node { int val; node* left; node* right; const int P = 715827883; const int Q = 2796203; Map<int, Vector<node*>> simulatetreehash(vector<node*> trees, int k) { 1 In other words, h is a hash function that goes from integer input to integer output. We will use it as a helper for the hash function that we are writing that goes from binary tree input to integer output. Assume it is already defined and you can just call it.

// more space for Hashing problem (you should not need this much space)

6. Graphs and Classes. For this problem, you will implement a variant of the Game of Life 2 that is sometimes given as the first assignment in CS106B/X. The Game of Life represents cells in a petri dish, where the number of cells is constant, but in each generation, a given cell can be alive or dead. The cells start in some initial configuration of alive or dead, and then simulate a new generation by doing a pass over all of them to update their alive/dead status. When doing this pass over each cell, we determine if a given cell will be alive in the next generation by examining its friends and seeing how many are alive or dead a friend is like a customized definition of neighbor in a graph. Once you ve counted the number of alive friends (for our special definition of friend ), the rules are as follows: If 0-1 friends of a cell are alive, the cell dies. If 2 friends of a cell are alive, the cell is stable (if alive, stays alive; if dead, stays dead). If 3 friends are alive, the cell will be alive. If 4 or more friends are alive, the cell dies of overcrowding. All the changes happen simultaneously, so if a previously dead cell becomes alive, do not then count it as alive when you count up the number of alive friends of one of its friends (at least not until the next round). Our definition of a cell s friends are defined by weighted, undirected graph edges, so a cell can have any (non-negative) number of friends. Note that the nodes and their edge relationships do not change during the course of the game the only change is whether the cell represented by a given node is alive or dead. The friends of a cell c are as follows: If a path of total weight no more than 20 exists between c and d, then d is c s friend. c is not its own friend. Example: In the graph below, cell 1 has 3 neighbors (2, 3, and 4). 0 2 7 4 30 7 5 3 2 1 15 5 Write a class that computes this new version of the Game of Life. Your class should have exactly three public member functions, as follows: void initializecells(string filename) o Randomly initializes all cells to be alive or dead (with equal probability). Suggestion: bool alive = getrandominteger(0,1); o Reads the configuration of the graph edges from a file. The file format is as follows: (1) the first line of the file is the number of cells, (2) then follows one line per cell (starting with cell 0, cell 1, etc), with space-separated integers giving the weights of the edges for that cell to every other cell. A weight of 0 means there is no edge between those two 2 This problem was originally given in a quarter when the Game of Life was given as an assignment, which of course was not the case for you this quarter. That may make this problem somewhat more challenging for you than it was for those students. On the other hand, some of those students may have had a hard time wrapping their mind around this strange version of the Game of Life, having grown accustomed to the assignment version, which could actually make it harder than seeing this version with fresh eyes. In any case, this should be solvable for you as a Practice Final question.

cells. All actual edge weights are > 0. Example file contents for the graph above (comments are not part of the file): 6 // total number of cells 0 30 0 0 0 0 // adjacent to cell 0 30 0 7 0 0 0 // adjacent to cell 1 0 7 0 5 7 0 // etc. 0 0 5 0 2 15 0 0 7 2 0 0 0 0 0 15 0 0 void updatecells() o Updates the board according to one generation of the game. void printcells() o Prints the dead/alive state of each cell to cout in the format [cell number]: [0/1]. Here is an example with 3 cells: 0: 1 1: 0 2: 1 Your class needs to have private data for representing the state of the board; the design is up to you. You might find that using your own Graph representation (using other container classes) rather than the full BasicGraph class is simpler. You may also wish to define a cell struct to hold the state of a cell. If you want to add any helper functions, they should be private. Below is life.h. Fill in the private section according to your design. On the following pages, fill in life.cpp with the required public functions and any private ones you may have added. class LifeGame { public: void initializecells(string filename); void updatecells(); void printcells(); private: //this may be more space than you need

// life.cpp #include <iostream> void LifeGame::initializeCells(string filename){

//lifegame.cpp continued void LifeGame::updateCells() {

//lifegame.cpp continued void LifeGame::printCells() {

Summary of Relevant Data Types We tried to include the most relevant member functions for the exam, but not all member functions are listed. You are free to use ones not listed here that you know exist. You do not need #include. class string { bool empty() const; int size() const; int find(char ch) const; int find(char ch, int start) const; string substr(int start) const; string substr(int start, int length) const; char& operator[](int index); const char& operator[](int index) const; class Vector { bool isempty() const; int size() const; void add(const Type& elem); // operator+= used similarly void insert(int pos, const Type& elem); void remove(int pos); Type& operator[](int pos); class Grid { int numrows() const; int numcols() const; bool inbounds(int row, int col) const; Type get(int row, int col) const; // cascade of operator[] also works void set(int row, int col, const Type& elem); class Stack { bool isempty() const; void push(const Type& elem); Type pop(); class Queue { bool isempty() const; void enqueue(const Type& elem); Type dequeue();

class Map { bool isempty() const; int size() const; void put(const Key& key, const Value& value); bool containskey(const Key& key) const; Value get(const Key& key) const; Value& operator[](const Key& key); Example range-based for: for (Key key : mymap){ } class Set { bool isempty() const; int size() const; void add(const Type& elem); bool contains(const Type& elem) const; Operators: set + value // Returns the union of set set1 and individual value value set += value // Adds the individual value value to the set set set1 += set2 // Adds all the elements from set2 to set1 Example range-based for: for (Type elem : mymap){ } class Lexicon { int size() const; bool isempty() const; void clear(); void add(std::string word); bool contains(std::string word) const; bool containsprefix(std::string prefix) const; Example range-based for: for (string str : english){ } struct Edge { Vertex* start; Vertex* finish; double cost; bool visited; struct Vertex { std::string name; Set<Edge*> arcs; Set<Edge*>& edges; bool visited; Vertex* previous;

class BasicGraph : public Graph<Vertex, Edge> { public: BasicGraph(); Vertex* addvertex(vertex* v); const Set<Edge*>& getedgeset() const; const Set<Edge*>& getedgeset(vertex* v) const; const Set<Edge*>& getedgeset(std::string v) const; Vertex* getvertex(std::string name) const; const Set<Vertex*>& getvertexset() const; void removeedge(std::string v1, std::string v2, bool directed = true); void removeedge(vertex* v1, Vertex* v2, bool directed = true); void removeedge(edge* e, bool directed = true); void removevertex(std::string name); void removevertex(vertex* v); }