CS 216 Exam 1 Fall SOLUTION

Similar documents
CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

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

UNIVERSITY REGULATIONS

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Dynamic Data Structures

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

Data Structures Question Bank Multiple Choice

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

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

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

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

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CSCE 2014 Final Exam Spring Version A

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

COS 226 Algorithms and Data Structures Fall Midterm

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Largest Online Community of VU Students

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Draw a diagram of an empty circular queue and describe it to the reader.

ECE250: Algorithms and Data Structures Midterm Review

Matriculation number:

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

Section 1: True / False (2 points each, 30 pts total)

CSL 201 Data Structures Mid-Semester Exam minutes

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

Summer Final Exam Review Session August 5, 2009

CS171 Midterm Exam. October 29, Name:

Data Structure (CS301)

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

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Q1 Q2 Q3 Q4 Q5 Q6 Total

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

(the bubble footer is automatically inserted into this space)

Computer Science Foundation Exam

Suppose we want to create a data structure called DataStore that has the following operations:

Computer Science Foundation Exam

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

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Course Review for Finals. Cpt S 223 Fall 2008

Computer Science E-22 Practice Final Exam

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

COMP 250 Midterm #2 March 11 th 2013

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Solution to CSE 250 Final Exam

Basic Data Structures (Version 7) Name:

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSE373 Fall 2013, Midterm Examination October 18, 2013

CMSC351 - Fall 2014, Homework #2

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

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

CS301 - Data Structures Glossary By

Answers. 1. (A) Attempt any five : 20 Marks

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

List, Stack, and Queues

Midterm Exam (REGULAR SECTION)

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

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

CS350 - Exam 1 (100 Points)

Stack and Queue. Stack:

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

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

Largest Online Community of VU Students

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

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

CS 315 Data Structures mid-term 2

CMSC 341 Lecture 10 Binary Search Trees

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Analysis of Algorithms. CS 1037a Topic 13

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Midterm Exam 2 CS 455, Spring 2011

Introduction to the C programming language

Higher National Diploma in Information Technology. First Year Second Semester Examination IT 2003-Data Structure and Algorithm.

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Basic Data Structures

This examination has 11 pages. Check that you have a complete paper.

ADTs Stack and Queue. Outline

Introduction to the C programming language

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Transcription:

CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT use calculators. Good Luck!! TOTAL 104 MAX SCORE Write and sign pledge after taking the exam: Exam 1 CS216 Anderson Fall 2004 Page 1 of 13

CS 216 Exam 1 Define each term and give an example that explains it: 1. (2 points) excess B Also known as biased notation. Method of representing integers where integer value K is represented as K + B, where B is the bias. In single precision IEEE floating point excess 127 is used to represent the exponent. Thus the exponent -120 is represented as 7 or: 00000111 2. (2 points) height of an AVL tree The length of the longest path from the root to a leaf node. The height of the tree in problem #16 is four. The height of a tree of a single node is zero. 3. (2 points) FIFO data structure A first-in-first-out data structure is one in which the first item inserted will be the first item removed. An example is a queue. After the sequence: enqueue(1), enqueue (2), enqueue (3), a call to dequeue() will return the value 1. 4. (2 points) abstract data type A set of values and the set of operations allowable on those values. Examples: stack, queue, list. Exam 1 CS216 Anderson Fall 2004 Page 2 of 13

5. (2 points) Order the following rates of growth from slowest rate of growth to fastest rate of growth: n 8, n log n, n, 2 n, 300, log n 300 log n n n log n n 8 2 n 6. (3 points) Name 3 factors that are ignored by big-oh notation. Hardware, Operating system, programming language used, assumes that all operations take the same amount of time, ignores constant factors, throws out lower ordered terms. 7. (4 points) Fill in the blanks in the definition of big-oh notation: T(N) = O(f(N)) if: there are positive constants c and n 0 such that: T(N) <= c * f(n) when: N >= n 0 Exam 1 CS216 Anderson Fall 2004 Page 3 of 13

8. (10 points total) Describe the running time of the following pseudocode in Big-Oh notation in terms of the variable n. Assume all variables used have been declared. Show your work for partial credit. int vote_count(int k) { int sum; for (int i = 0; i < 500; ++i) sum = sum + (i * k); return sum; a) answ = vote_count(n); O(1) b) int sum; O(n) if (n < 100) cout << whoa! ; else for (int i = 0; i < n; ++i) sum += vote_count(n); c) for (int j = 4; j < n; ++j) { O(n 3 ) cin >> val; for (int i = 0; i < j; ++i) { b = b * val; for (int k = 0; k < n; ++k) c = b + c; d) for (int i = 0; i < n * n; ++i) { O(n 4 ) sum = sum/n; for (int j = 0; j < i; ++j) j >> cout; e) for (int i = 0; i < n; ++i) { O(n 3 ) for (int j = 0; j < i * n ; ++j) sum = sum + i; for (int k = 0; k < n + n; ++k) a[k] = a[k] + sum; Exam 1 CS216 Anderson Fall 2004 Page 4 of 13

9. (6 points total) What is the representation of each of the following in the indicated radix? Be sure to show your work. a) 12 7 in decimal = 1 * 7 1 + 2 * 7 0 = 7 + 2 = 9 10 b) 1132 8 in hex = 1 * 8 3 + 1 * 8 2 + 3 * 8 1 + 2 * 8 0 = 1 * 512 + 1 * 64 + 3 * 8 + 2 * 1= 512 + 64 + 24 + 2 = 602 10 602/16 = 37 rem 10 etc. = 25A 16 001 001 011 010 0010 0101 1010 (group the digits to get: 25A 16 ) c) 2E 15 in radix 10 = 2 * 15 1 + 14 * 15 0 = 2 * 15 + 14 * 1 = 30 + 14 = 44 10 10. (6 points total) Consider the positive binary integer represented in two s complement: 0110010110000111 2. a) Express this binary number in octal 0 110 010 110 000 111 2 = 62607 8 b. Express this binary number in hexadecimal 0110 0101 1000 0111 2 = 0x 6587 c. Negate the number (i.e. give the two s complement representation of a negative version of the same number) Use the same number of bits. 1001101001111001 2 Exam 1 CS216 Anderson Fall 2004 Page 5 of 13

11. (3 points) Draw the binary search tree created by inserting these values in this order: 4 1 9 6 7 2 5 0 8 4 1 9 0 2 6 5 7 8 12. (2 points) Give a pre-order traversal of your tree shown above: 4 1 0 2 9 6 5 7 8 13. (2 points) Give a post-order traversal of your tree shown above: 0 2 1 5 8 7 6 9 4 14. (3 points) Delete the root of the tree shown above using one of the methods described in class. Draw the new tree here: 1 2 9 1 5 9 0 6 5 7 8 0 2 6 7 8 Exam 1 CS216 Anderson Fall 2004 Page 6 of 13

15. For each operation below give: 1) How you would most efficiently implement the operation, 2) Describe the worst case scenario (e.g. The worst case occurs when the value you are looking for is not in the list ) and 3) What is the worst case Big-Oh running time of this scenario. State any assumptions you make. a) (3 points) Find the maximum value stored in an AVL tree. - Starting at the root, traverse to the right until hit a NULL right pointer (this is the largest value in the tree). - All cases are roughly the same as the tree is balanced, but in the worst case the longest path in the tree is from the root to the largest value. - O(log N) as this is the worst case height of the tree (length of longest path from the root to a leaf) b) (3 points) Pop a value from a stack implemented as an array. - return the value currently contained in the top of stack location, increment (or decrement) the top of stack location so this value is no longer pointed to. - All cases are the same. - O(1) as updating the top of stack location and returning the value are just constant time operations that do not depend on how many elements are currently in the stack. c) (3 points) Find the total number of values stored in a queue implemented as a doubly linked list. Two options: Keep a size field updated: - assume that there is a field that keeps track of how many elements are currently stored in the queue. This field is updated (in constant time) on enqueue and dequeue operations. - All cases are equal - O(1) to return the value stored in the field Count the number of elements at the time it is requested: - starting at either the head or tail, traverse to the end of the list, counting the number of elements as you go. - All cases are equal - O(N) as you must traverse the entire list, doing a constant time operation at each node (incrementing a count). Exam 1 CS216 Anderson Fall 2004 Page 7 of 13

16. (3 points) Given the following tree: 40 22 77 17 12 27 30 65 96 90 24 Is it an AVL tree? If not, circle the node(s) where the AVL property is violated. Why or why not (must answer for any credit)? No, at node 30, the height of the left subtree is 1 and the height of the right subtree is -1, so their difference is > 1 thus violating the AVL structure property. 17. (3 points) Given the following tree: 67 20 70 23 42 78 89 98 Is it an AVL tree? If not, circle the node(s) where the AVL property is violated. Why or why not (must answer for any credit)? No, at nodes 20 and 70, the height of the left subtree is -1 and the height of the right subtree is 1, so their difference is > 1 thus violating the AVL structure property. Exam 1 CS216 Anderson Fall 2004 Page 8 of 13

18. (7 points) Assume we are using the 32-bit IEEE single precision floating point format as described in class and used in lab. The mantissa has 24 bits including the hidden bit. There is one sign bit and there are eight exponent bits. The exponent is stored in excess 127. What decimal floating point number is represented by the following 32 bits? SHOW YOUR WORK! 1000 1010 1111 0000 0000 0000 0000 0000 a) Is this a positive or negative number? Negative (sign bit is 1) b) What is the exponent (in base 10)? 00010101 2 = 21 10 21-127 = -106 c) What is the value of the mantissa (in base 10) 1.111 2 1 + 1/2 + 1/4 + 1/8 = 1 + 0.5 + 0.25 + 0.125 =1.875 d) What is the total value? Note: you may leave your answer in the form: value 10 * base exponent Where you specify value, base and exponent. -1.875 * 2-106 Exam 1 CS216 Anderson Fall 2004 Page 9 of 13

19. (20 points) This question tests your understanding of stacks and pointer manipulation. You must implement a stack ADT in C++. The underlying representation of the stack should be the Node class as described below used in a singly linked list. Your stack should store integers and should handle errors (printing an error message is fine). You will be graded mostly on the correctness of the ideas of your solution rather than exact C++ syntax, but your solution should be clear. Correct C++ code is the best way to ensure we understand your solution. You may NOT use the STL in any way for this question. You should use the header file provided below. You do not need to implement copy constructors, destructors, or operator= for these classes, but otherwise the routines you implement should handle memory management appropriately You should implement all the functions with in front of them. class Node { public: Node(int value): val(value), next(null) {; int val; Node *next; ; class Stack { public: Stack(); // constructor void push(int value); // pushes value onto the stack. int pop(); // returns and removes the value on // the top of the stack. int top(); // returns the value on the top of // the stack without removing it. bool isempty(); // returns true if the stack contains // no elements. private: // Add data members here. ; Node *topofstack; Exam 1 CS216 Anderson Fall 2004 Page 10 of 13

Stack::Stack(){ topofstack = NULL; void Stack::push(int val){ Node *temp = new Node(val); if (isempty()) { // Add the first item to the stack. topofstack = temp; else { temp->next = topofstack; topofstack = temp; // returns -1 if empty int Stack::pop() { Node *temp; int return_val; if (!isempty()) { // At least one item is on the stack temp = topofstack; topofstack = topofstack->next; return_val = temp->val; delete temp; else { // empty stack cout << Empty list ; return_val= -1; return return_val; // returns -1 if empty int Stack::top() { if (!isempty()) { // At least one item is on the stack return topofstack->val; else { // empty stack cout << Empty list ; return -1; bool Stack::isEmpty() { return!topofstack; Exam 1 CS216 Anderson Fall 2004 Page 11 of 13

20. (8 points) Implement a non-member function: void print_stack_inorder(stack my_stack) that takes a Stack as input, and prints out all the values in my_stack in ascending numerical order, e.g. from smallest value to largest value. To answer this question the only data structure you may use is a Stack (or temporary stacks). You may not use lists, arrays, vectors, queues. For the purposes of this question you can assume that a copy constructor that implements a deep copy exists for the Stack class. Assume you have a working version of the stack from the previous question, and a definition of the interface. void print_stack_inorder(stack my_stack) { Stack tempstack; int temp_val; int current_min; while(!my_stack.isempty()) { // Do nothing w. empty stack current_min = my_stack.top(); // Pop all values off of my_stack and push onto tempstack. while(!my_stack.isempty()) { temp_val = my_stack.pop(); tempstack.push(temp_val); // Keep track of the minimum value. if (temp_val < current_min) current_min = temp_val; // Pop all values off of tempstack and push onto my_stack. while(!tempstack.isempty()) { temp_val = tempstack.pop(); // Print out the current min and don't push back on. if (temp_val == current_min) cout << current_min << " " ; else my_stack.push(temp_val); cout << endl; OR for recursive solution, see next page. Exam 1 CS216 Anderson Fall 2004 Page 12 of 13

void print_stack_inorder(stack my_stack) { Stack tempstack; int temp_val; int current_min; if (my_stack.isempty()) return; current_min = my_stack.pop(); while (!my_stack.isempty()){ temp_val = my_stack.pop(); if (temp_val < current_min) { tempstack.push(current_min); current_min = temp_val; else tempstack.push(temp_val); cout << current_min; print_stack_inorder(tempstack); 21. (2 points) What is the worst case big-oh running time of your pop method and why? O(1) pop only involves constant time operations updating pointers, deleting a single node. No operation depends on the number of elements in the stack (N). 22. (3 points) What is the worst case big-oh running time of your print_stack_inorder function and why? O(n 2 ) The function consists of an outer while loop that has n iterations one iteration for each value printed out. Inside of that outer loop are two other loops (not nested) that each take O(n) iterations (first time through the outer loop they each take n iterations, then n-1, n-2, n-3, then finally only one iteration the last time through the outer loop). Summing these iterations we get (n * (n+1))/2 or O(n 2 ). Similarly multiplying the number of iterations of the outer loop (n) times the amount of work per iteration: n + n = O(n), we get O(n 2 ). For recursive solution, there are n calls to a routine that takes time n on each call (maximum of n pops and pushes per call). So also O(n 2 ). Exam 1 CS216 Anderson Fall 2004 Page 13 of 13