CS 62 Practice Final SOLUTIONS

Similar documents
CS302 Data Structures using C++

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

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

CS24 Week 8 Lecture 1

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CSCE 2014 Final Exam Spring Version A

CS301 - Data Structures Glossary By

Data Structures Question Bank Multiple Choice

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

1. O(log n), because at worst you will only need to downheap the height of the heap.

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

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CSE332 Summer 2010: Final Exam

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

CS61C : Machine Structures

CS 241 Data Organization Binary Trees

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

CP2 Revision. theme: dynamic datatypes & data structures

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

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Table of Contents. Chapter 1: Introduction to Data Structures... 1

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Data Structure. IBPS SO (IT- Officer) Exam 2017

Summer Final Exam Review Session August 5, 2009

Optimizing Dynamic Memory Management

Pointers II. Class 31

Fundamental Algorithms

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Common Misunderstandings from Exam 1 Material

Fundamentals of Data Structure

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Dynamic Data Structures

COMP 250 Midterm #2 March 11 th 2013

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

CS171 Midterm Exam. October 29, Name:

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?

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 19 November 2014

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

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

CS171 Final Practice Exam

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Computer Science E-22 Practice Final Exam

Largest Online Community of VU Students

ECE 250 Algorithms and Data Structures

) $ f ( n) " %( g( n)

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

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Cpt S 122 Data Structures. Data Structures

DATA STRUCTURE AND ALGORITHM USING PYTHON

DATA STRUCTURES AND ALGORITHMS

CS DATA STRUCTURES AND ALGORITHMS

Events, Memory Management

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

Dynamic Storage Allocation

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

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

CS8391-DATA STRUCTURES

Course Name: B.Tech. 3 th Sem. No of hours allotted to complete the syllabi: 44 Hours No of hours allotted per week: 3 Hours. Planned.

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

Run-time Environments - 3

( ) n 3. n 2 ( ) D. Ο

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

DATA STRUCTURES AND ALGORITHMS

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

CS140 Final Project. Nathan Crandall, Dane Pitkin, Introduction:

LECTURE 11 TREE TRAVERSALS

! 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)

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

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)

VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur

Graphs & Digraphs Tuesday, November 06, 2007

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

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

Department of Computer Science and Technology

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Programming and Data Structure Solved. MCQs- Part 2

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

Algorithms and programs, basic idea of pseudo-code.algorithm efficiency and analysis, time and space analysis of algorithms order notations.

Dynamic Data Structures. CSCI 112: Programming in C

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

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

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Basic Data Structures (Version 7) Name:

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

CS61C : Machine Structures

Data Structure Advanced

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

Computer Science 136 Exam 2

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

Transcription:

CS 62 Practice Final SOLUTIONS 2017-5-2 Please put your name on the back of the last page of the test. Note: This practice test may be a bit shorter than the actual exam. Part 1: Short Answer [32 points] 1. [4 points] In an array-based representation of a tree, what s the formula for the index of the left child of the node at index i? Are the left and right children of a node always adjacent to each other in the array, or not? left = i*2+1 Yes, they are always adjacent. 2. [4 points] Explain the difference between weak and strong connectivity in a directed graph. A strongly-connected directed graph is one in which for any pair of nodes A and B, there is both a path from A to B and a path from B to A. In a weakly-connected graph, this would be true if edges could be traversed either direction, but it isn t necessarily true with the edges as-is. A weakly-connected graph is simply any graph which cannot be cut into two pieces without cutting through at least one edge. 3. [4 points] Explain the practical purpose of a sequential cutoff in code that uses the map-reduce paradigm (like the ParallelSum example we saw in class). A sequential cutoff exists to eliminate excessive overhead from starting threads. Because starting a thread (or even just pulling one out of a pool) takes some time, having each thread process just a single element or a pair of elements is ineffcient relative to having each thread process a couple hundred or thousand elements. 4. [4 points] What s the difference between a callback and a normal function in terms of when they are called? A normal function is called when the programmer says so. A callback is called when some external event triggers it, so the programmer isn t in full control.

5. [4 points] Explain the problem with the following code, and suggest how to fix it: public void printeven(iterator<integer> iter) { while (iter.hasnext()) { if (iter.next() % 2 == 0) { System.out.println(iter.next()); Hint: the code isn t using the next function correctly. This code calls next once ni the condition of the while loop, and then again in the body, getting two different values each time. It needs to call next only once, and store the result. 6. [4 points] If a program takes 30 seconds to set up some parallelizable work and then another 30 seconds to do that work on a single processor, what is the maximum achievable speedup for this program using multiple processors according to Amdahl s law? The best possible speedup is 2x. This is because 1/2 the time is nonparalellizeable setup time, so even if the parallelizable work were done in a fraction of a nanosecond on billions of processors, the total run time would be 30 seconds, which is 1/2 of the sequential time. 7. [4 points] Explain what the malloc function does in C. Also explain what its argument does. Malloc allocates memory on the heap. number of bytes to allocate. The argument specifies the 8. [4 points] In a graph where the shortest path from A to B is 3 edges, while the shortest path from A to C is 7 edges, will breadth-first search starting at A explore B or C first, or does it depend on the structure of the graph? Justify your answer. It will explore B first, because breadth-first search always explores closer nodes (in terms of path length) before farther ones.

Problem 2: Linked Lists [12 points] 1. Diagram [4 points] Draw a diagram of a doubly-linked list with both head and tail pointers that contains the elements 17, 23, and 31. Use arrows to indicate pointers, including the head and tail pointers, and the next and previous pointers of each node. For null pointers, draw an arrow pointing to the word NULL. head tail /-------\ /-------\ v v v v /------------\ /- ----------\ /- ----------\ o 17 o o 23 o o 31 o + \- -------- -/ \---------- -/ \---------- -/ v ^ ^ v NULL \-------/ \-------/ NULL 2. Operations [4 points] Assuming we wanted to insert the value 47 between the 23 and 31 in our list, write a list of which pointers would have to be changed. Include pointers that are part of the new node. 23.next 31.previous 47.previous 47.next 3. Effciency [4 points] If you use this doubly-linked list to implement a queue, what would the big-o run times of the push and pop methods be? Also, what would be the run time of an insertsorted method that inserted an item such that the whole list remained in sorted order? Both push and pop would be O(1). They just have to modify the first/last pointers and the head/tail pointers. insertsorted would be O(n) (it has to scan through the list to find the correct location).

Problem 3: Malloc and Free [12 points] 1. Malloc [6 points] For the code fragment below, how many different heap regions does it allocate, and how many bytes does it allocate on the heap. Also, when the setup function is called, how many bytes are allocated on the stack for its local variable(s)? Assume the size of an int is 4 bytes, and the size of a pointer of any type is 8 bytes. int** setup() { int** arrays = malloc(2 * sizeof(int*)); arrays[0] = malloc(4 * sizeof(int)); arrays[1] = malloc(4 * sizeof(int)); return arrays; It allocates 3 heap regions (there are three calls to malloc ), and it allocates a total of 2*8 + 4*4 + 4*4 = 16 + 16 + 16 = 48 bytes on the heap. When called, it just needs space to store the pointer arrays which will be 8 bytes on the stack. 2. Free [6 points] Fill in the cleanup function below so that when given a pointer returned by the setup function from part 1, it will properly free all of the allocated memory. void cleanup(int** arrays) { free(arrays[0]); free(arrays[1]); free(arrays);

Problem 4: Graphs [12 points] 1. Acyclic Non-Trees [4 points] Draw a directed, acyclic graph with 5 nodes which is not a tree, but which has at least one spanning tree. Label your nodes with the letters A through E. A / \ v v B C \ / v D->E 2. Spanning Trees [4 points] How many spanning trees does the graph that you drew in part 1 have? List the root nodes of each spanning tree (list the same node multiple times if it s the root of multiple spanning trees). It has two spanning trees. The root of each is A (that s the only node that can reach every other node in the graph). 3. Paths [4 points] Is the graph you drew in part one strongly connected, weakly connected, both, or neither? Write down a sequence of nodes which is the longest (or a tied-forlongest) path in that graph. It s weakly connected. A tied-for-longest path is: A > B > D > E.

Name: