CS 223: Data Structures and Programming Techniques. Exam 2

Similar documents
CS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CSE373 Fall 2013, Midterm Examination October 18, 2013

CS 315 Data Structures mid-term 2

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

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

Fundamental of Programming (C)

Exam Data structures DAT036/DAT037/DIT960

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

Computer Science Foundation Exam

Computer Science Foundation Exam

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Second Examination Solution

This exam is worth 24 points, or 24% of your total course grade. The exam contains six

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

CSE 303 Midterm Exam

Computer Science Foundation Exam

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

CS 0449 Sample Midterm

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CS 1713 Introduction to Programming II

Test 1: CPS 100. Owen Astrachan. February 23, 2000

CSE351 Winter 2016, Final Examination March 16, 2016

Pointers. Pointers. Pointers (cont) CS 217

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

Algorithm Design and Analysis. What is an Algorithm? Do Algorithms Matter?

ECE264 Fall 2013 Exam 1, September 24, 2013

ECE264 Spring 2013 Final Exam, April 30, 2013

COMP 250 Midterm #2 March 11 th 2013

Pointers. Pointer References

Section 1: True / False (1 point each, 15 pts total)

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

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

CS 1713 Introduction to Programming II

Q1 Q2 Q3 Q4 Q5 Q6 Total

Trees. A tree is a directed graph with the property

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Question 1. [15 marks]

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Worksheet 11 Multi Dimensional Array and String

CSE332 Summer 2010: Midterm Exam Sample Solutions

Section 4 SOLUTION: AVL Trees & B-Trees

1. AVL Trees (10 Points)

CSE 333 Midterm Exam 2/14/14

Balanced Search Trees. CS 3110 Fall 2010

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

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

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

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

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

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Computer Science Foundation Exam

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

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

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

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Exam Sample Questions CS3212: Algorithms and Data Structures

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

9. Heap : Priority Queue

CSE 333 Midterm Exam 5/10/13

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)

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

Computer Science Foundation Exam

Sorting and Searching

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 27

Today s Learning Objectives

CS 2301 Exam 3 B-Term 2011

CPSC 35 Midterm Exam

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Exercise Session 2 Simon Gerber

COS 226 Midterm Exam, Spring 2009

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

Computer Science Foundation Exam

CS 216 Exam 1 Fall SOLUTION

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

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 261 Data Structures. AVL Trees

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

[4] 1 cycle takes 1/(3x10 9 ) seconds. One access to memory takes 50/(3x10 9 ) seconds. =16ns. Performance = 4 FLOPS / (2x50/(3x10 9 )) = 120 MFLOPS.

Module Contact: Dr Tony Bagnall, CMP Copyright of the University of East Anglia Version 1

CP2 Revision. theme: dynamic datatypes & data structures

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Computer Science Foundation Exam

Sorting and Searching

ESC101N: Fundamentals of Computing End-sem st semester

CMSC 341 Lecture 14: Priority Queues, Heaps

CSE373 Winter 2014, Midterm Examination January 29, 2014

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Final Examination CSE 100 UCSD (Practice)

Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3

Transcription:

CS 223: Data Structures and Programming Techniques. Exam 2 Instructor: Jim Aspnes Work alone. Do not use any notes or books. You have approximately 75 minutes to complete this exam. Please write your answers on the exam. More paper is available if you need it. Please put your name at the top of the first page. There are four questions on this exam, for a total of 80 points. 1

1 The answer is always hash tables (20 points) Suppose you want to build a grade database that supports the operations INSERT and FIND- MEDIAN, where INSERT adds a new grade to the database and FIND-MEDIAN reports a median grade, defined as a grade x such that the number of grades less than x in the database and the number of grades greater than x in the database differ by at most one. Suppose also that we expect to call FIND-MEDIAN at least as often as we call INSERT. 1. Briefly explain why a hash table is a bad choice for this database. 2. Give an alternative data structure that supports these operations efficiently. You do not need to write any code for this. Describing what data structure you would use, how you would use it, and how much it would cost, is enough. 1. Because hash tables do not preserve the order of their contents, there is no way to compute the median other than looking at all of the elements, which will take at least Ω(n) time. 2. There are several options here. We were generally looking for a solution that runs in O(log n) time (or better) for both INSERT and FIND-MEDIAN. (a) An augmented balanced binary search tree, where each node includes a count of the number of nodes in its subtree. This allows ranking and unranking elements at cost O(log n), and maintains (at the root) a count of all elements, so we can just search for the element with rank n/2. This gives a cost of O(log n) for both INSERT and FIND-MEDIAN. (b) Two heaps, with the invariant that every element in the left heap (a max-heap) is smaller than every element in the right heap (a min-heap), and that the sizes of the heaps differ by at most one. To perform an insert, put the new element in whichever heap will not violate this invariant. If one heap is too big, perform a DELETE-MIN or DELETE- MAX as appropriate on it and insert the element into the other heap (we will need to do this at most once for every INSERT). The cost of INSERT is thus O(log n). For FIND-MEDIAN, just return the top element in the bigger heap, or in either heap if they are the same size. This takes O(1) time. A popular idea was to use an AVL tree without keeping around size fields. Unfortunately AVL trees are height-balanced but only very approximately size-balanced, so there is no way to find the median without extra information. 2

2 Transposing a two-dimensional array (20 points) Write a function that takes an argument that is an n-long array of pointers to n-long arrays of ints, and transposes it, so that for each i and j, the value of a[i][j] after calling the function is the value of a[j][i] before calling the function. We have provided the function header for you: void transpose(int n, int **a) { int i; int j; int tmp; /* for swapping */ for(i = 0; i < n; i++) { /* only do j < i so we don t swap same pair twice */ for(j = 0; j < i; j++) { tmp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = tmp; 3

3 Summing a tree (20 points) Suppose that you have a balanced binary search tree data structure declared as follows: struct tree { int key; int value; struct tree *child[2]; ; Write a function sumtree that, given a pointer to the root of the tree, computes the sum of the value fields in all nodes in the tree (do not worry about overflow). We have provided the function header for you: int sumtree(const struct tree *root) int sumtree(const struct tree *root) { int sum; int i; if(root) { sum = root->value; for(i = 0; i < 2; i++) { sum += sumtree(root->child[i]); else { sum = 0; return sum; 4

4 A twisty program (20 points) What output does the following program produce? Please draw a rectangle around your solution to distinguish it from any notes you used to produce it. #include <stdio.h> void f(int n, int ***a, int **b) { for(int i = 0; i < n; i++, a++) { *a = &b[(i+1) % n]; void g(int n, int **a, int *b) { for(int i = 0; i < n; i++) { a[i] = b + ((i+1) % n); int main(int argc, char **argv) { int **x[3]; int *y[3]; int z[3]; f(3,x,y); g(3,y,z); for(int i = 0; i < 3; i++) { z[i] = i; for(int i = 0; i < 3; i++) { printf("%d ", **(x[i])); for(int i = 0; i < 3; i++) { printf("%d ", *(y[i])); putchar( \n ); return 0; 2 0 1 1 2 0 5