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

Similar documents
CS 223: Data Structures and Programming Techniques. Exam 2

Heaps / Priority Queues

CS 2301 Exam 3 B-Term 2011

Introduction to Computer Science Midterm 3 Fall, Points

Suppose that the following is from a correct C++ program:

ECE264 Spring 2013 Final Exam, April 30, 2013

Algorithms and Data Structures

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Dictionaries. Priority Queues

Procedures, Parameters, Values and Variables. Steven R. Bagley

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

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

CS 315 Data Structures mid-term 2

CS 0449 Sample Midterm

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

Arrays and Pointers in C. Alan L. Cox

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

Algorithms and Data Structures

CS 261 Spring 2013 Midterm

ECE264 Fall 2013 Exam 3, November 20, 2013

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

Lecture Notes on Tries

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

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

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

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

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

Solution for Data Structure

8. Binary Search Tree

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Computer Science Foundation Exam

Understanding Pointers

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

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

Linear Data Structure

Binary Heaps. CSE 373 Data Structures Lecture 11

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

CS171 Final Practice Exam

Computer Science Foundation Exam

Priority queues. Priority queues. Priority queue operations

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

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

Lecture No.04. Data Structures

CS 2412 Data Structures. Chapter 7 Heaps

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

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

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

9. Heap : Priority Queue

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

Biostatistics 615 / 815

CS171 Final Practice Exam

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

CS 237 Meeting 19 10/24/12

POINTER AND ARRAY SUNU WIBIRAMA

Computer Science Foundation Exam

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Final Examination CSE 100 UCSD (Practice)

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

ESC101N: Fundamentals of Computing End-sem st semester

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

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

Route planning / Search Movement Group behavior Decision making

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

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

COMP 524 Spring 2018 Midterm Thursday, March 1

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

COMP1917 Computing 1 Written Exam Sample Questions

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

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

Computer Science E-22 Practice Final Exam

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Department of Electrical Engineering and Computer Sciences Spring 2013 Instructor: Dr. Dan Garcia CS61C Midterm

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CSCE 2014 Final Exam Spring Version A

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Garbage Collection: recycling unused memory

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Trees. A tree is a directed graph with the property

CS 241 Analysis of Algorithms

Sorting and Searching

Spring 2014, CS288 Test 2, 2:30-3:45 pm, Fri, 4/4/2014, GITC 1400

Transcription:

CS 223: Data Structures and Programming Techniques. Exam 2 April 19th, 2012 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 Pointer juggling (20 points) On which line will the following program fail with a segmentation fault, and why? 1 #define N (3) 2 3 int 4 main(int argc, char **argv) 5 6 int i; 7 struct pointy 8 struct pointy *p[2]; 9 a[n]; 10 11 for(i = 0; i < N; i++) 12 a[i].p[0] = &a[i]; 13 a[i].p[1] = a + (i + 1) % N; 14 15 16 a[0].p[0] = 0; 17 18 a[2].p[0]->p[1]->p[1] = a[0].p[0]; 19 a[1].p[0]->p[0]->p[1] = a[0].p[0]; 20 a[0].p[1]->p[1]->p[0] = a[2].p[0]; 21 a[0].p[0]->p[1]->p[1] = a[2].p[1]; 22 a[0].p[0]->p[1]->p[1] = a[2].p[0]; 23 24 return 0; 25 We get a segmentation fault on line 20, when the program attempts to dereference a[0].p[1]. The reason is that a[0].p[1] is assigned a null pointer in line 18, since a[2].p[0]->p[1] is &a0. This is not affected by line 19, which puts a null pointer in a[1].p[1]. 2

2 Choosing a data structure (20 points) Suppose you are asked to write a program for displaying a leaderboard for a large on-line multiplayer game. The leaderboard consists of the names of the 10 players with the highest scores and their scores. Each player of the game has a name (a string) and a score, equal to the number of games they have won. Your program should provide routines incrementscore(const char *player) and returnwinners(char *winners[10], int scores[10]) where incrementscore increases the score of the named player by 1 (or sets the score to 1 if the player has never won a game before) and returnwinners fills in the winners and scores arrays with the names and scores of the players with the 10 highest scores (with ties broken arbitrarily). Without writing actual code, describe as succinctly as you can what data structure would be a good choice for this task and why. The simplest solution would probably be to keep track of the scores for all players using a hash table and keep track of the top 10 players in a sorted array. The reason this works is that a new player can only enter the top 10 by displacing the lowest existing player. So when a score is updated, we can (a) update the score in the hash table; (b) check using a linear scan if the player is already on the leaderboard, and float them up if their score has increased enough to warrant it; or (c) if the player is not on the leaderboard but has a score greater than the lowest leader s score, replace the lowest leader and float up if needed. Since the size of the leaderboard is small and constant, it would also work to have an unsorted leaderboard, but keeping it sorted saves time in returnwinners and in finding the smallest score. Another reasonable option would be a priority queue for the leaderboard indexed by a hash table or some other plausible dictionary data structure for the player names. The downside of this approach compared to the simple array is that it adds complexity, increases the cost from O(1) to O(log n), and requires more space to store the non-leaders in the priority queue. 3

3 A digital search tree (20 points) Here is an implementation of the creation and insertion routines for a binary digital search tree. The idea of the tree is that a key of type unsigned int is represented by a path through the tree, where each edge in the path is determined by the next bit in the key. Write a routine DSTpredecessor that returns the largest key in the tree less than or equal to the given key, or 0 if there is no such key. We have provided a declaration for this function for you on the next page. #include <stdlib.h> #include <limits.h> #define BITS (sizeof(unsigned int) * CHAR_BIT) /* CHAR_BIT == 8 */ struct node struct node *kids[2]; ; struct node *DSTcreate(void) struct node *root; root = malloc(sizeof(struct node)); root->kids[0] = root->kids[1] = 0; return root; void DSTinsert(struct node *root, unsigned int key) unsigned int mask; int bit; for(mask = 1 << (BITS-1); mask!= 0; mask >>= 1) bit = (key & mask)!= 0; if(root->kids[bit] == 0) root->kids[bit] = malloc(sizeof(struct node)); root = root->kids[bit]; 4

/* return largest key <= key or 0 if there is none */ unsigned int DSTpredecessor(struct node *root, unsigned int key); Here s one possible solution, using an auxiliary function that finds the smallest value in a subtree: /* return smallest key in subtree, not including bits above root */ static unsigned int smallestkey(struct node *root) int ret = 0; for(;;) if(root->kids[0]) root = root->kids[0]; ret = ret << 1; else if(root->kids[1]) root = root->kids[1]; ret = (ret << 1) + 1; else return ret; /* return largest key <= key or 0 if there is none */ unsigned int DSTpredecessor(struct node *root, unsigned int key) unsigned int mask; unsigned int bit; unsigned int prefix; /* bits we ve accumulated so far */ prefix = 0; for(mask = 1 << (BITS-1); mask!= 0; mask >>= 1) bit = (key & mask); if(root->kids[1] && bit!= 0 && (prefix + mask + smallestkey(root->kids[1]) <= key)) /* it s in the right subtree */ prefix += mask; root = root->kids[1]; else if(root->kids[0]) if(bit == 0) /* it s in the left subtree */ 5

root = root->kids[0]; else /* it s the biggest value in the left subtree */ key = ~0; root = root->kids[0]; else /* nowhere to go */ return 0; return prefix; 6

4 Change this program (20 points) Here is a rather dangerous implementation of a queue. Turn it into a stack by changing exactly one line of code. 1 #include <stdlib.h> 2 3 struct thing 4 int size; /* size of contents */ 5 int bot; /* first used location */ 6 int top; /* first unused location */ 7 int *contents; /* array of elements */ 8 ; 9 10 struct thing *thingcreate(int size) 11 12 struct thing *t; 13 14 t = malloc(sizeof(*t)); 15 16 t->size = size; 17 t->bot = t->top = 0; 18 t->contents = malloc(sizeof(int) * size); 19 20 return t; 21 22 23 void thingpush(struct thing *t, int value) 24 25 t->contents[t->top++] = value; 26 27 28 int thingpop(struct thing *t) 29 30 return t->contents[t->bot++]; 31 Change line 30 to 30 return t->contents[--(t->top)]; 7