Some CPSC 259 Sample Midterm and Final Exam Questions (Part 1) Sample Solutions

Similar documents
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Analysis of Binary Search algorithm and Selection Sort algorithm

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Lecture 12 Doubly Linked Lists (with Recursion)

Data Structure [Question Bank]

DATA STRUCTURES USING C

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Data Structures and Data Manipulation

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Linked Lists Linked Lists, Queues, and Stacks

Analysis of a Search Algorithm

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

10CS35: Data Structures Using C

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Stacks. Linear data structures

PES Institute of Technology-BSC QUESTION BANK

ADTs,, Arrays, Linked Lists

Unordered Linked Lists

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Sequential Data Structures

CompSci-61B, Data Structures Final Exam

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

St S a t ck a ck nd Qu Q eue 1

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Data Structures and Algorithms Written Examination

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Binary Heap Algorithms

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures. Level 6 C Module Descriptor

Ordered Lists and Binary Trees

Common Data Structures

LINKED DATA STRUCTURES

Introduction to Data Structures

Data Structures and Algorithms

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Cpt S 223. School of EECS, WSU

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sample Questions Csci 1112 A. Bellaachia

TREE BASIC TERMINOLOGIES

CS211 Spring 2005 Final Exam May 17, Solutions. Instructions

Programming with Data Structures

Node-Based Structures Linked Lists: Implementation

STACKS,QUEUES, AND LINKED LISTS

Basic Data Structures and Algorithms

arrays C Programming Language - Arrays

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Converting a Number from Decimal to Binary

CSE 211: Data Structures Lecture Notes VII

Data Structures and Algorithms Lists

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Data Structure with C

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture Notes on Binary Search Trees

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Introduction to Data Structures and Algorithms

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

recursion, O(n), linked lists 6/14

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures and Algorithms C++ Implementation

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

CmpSci 187: Programming with Data Structures Spring 2015

Module 2 Stacks and Queues: Abstract Data Types

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Algorithms and Data Structures

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

CS 241 Data Organization Coding Standards

Universidad Carlos III de Madrid

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Data Structures Fibonacci Heaps, Amortized Analysis

CS 2412 Data Structures. Chapter 2 Stacks and recursion

D06 PROGRAMMING with JAVA

Algorithms. Margaret M. Fleck. 18 October 2010

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Algorithms and Data Structures Written Exam Proposed SOLUTION

Why? A central concept in Computer Science. Algorithms are ubiquitous.

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Big Data and Scripting. Part 4: Memory Hierarchies

Chapter 3: Restricted Structures Page 1

DATA STRUCTURE - STACK

Transcription:

Some CPSC 259 Sample Midterm and Final Exam Questions (Part 1) Sample Solutions DON T LOOK AT THESE SOLUTIONS UNTIL YOU VE MADE AN HONEST ATTEMPT AT ANSWERING THE QUESTIONS YOURSELF. 1. 4 marks You will get 3 marks if you get one wrong, 2 marks if you get 2 wrong, and 0 marks if you get 3 or more wrong. Circle ALL correct answers because there may be more than 1 correct answer! Which of the following statements about stacks, queues, and general lists are true? a) Stacks and queues can be implemented using arrays. b) A queue allows a user to retrieve nodes using the LIFO principle, in O(1) time. c) A stack allows a user to retrieve nodes using the FIFO principle, in O(1) time. d) Suppose we have a circular array holding only integers, and having a capacity (maximum size) of n. If the array is a circular array, then we can store more than n entries in it at any given time because we can make use of the modulus operator. e) Using a doubly-linked list, it is not possible to keep the nodes sorted. ANSWER: Circle (a) only 2. 2 marks Put these Big-O sets of functions into ascending order of complexity: O(n 1/2 ), O(lg n), O(n 1/3 ), O(n), O(1/n). ANSWER: O(1/n), O(lg n), O(n 1/3 ), O(n 1/2 ), O(n) 3. 6 marks Compute the complexity of the following function, in terms of number of dollar signs ($) printed, as a function of N. Note that N is distinct from n. Justify your answer by briefly explaining your work (i.e., by showing your calculations). It is not necessary to compute witnesses. void mycalc(int N) int w, k; j = 0; n = N; while (n > 1) n = n / 32; j++;

for (w = 0; w < (N/3 + 32); w++) for (k = 1; k <= j*n; k++) printf( $ ); ANSWER: Assignment statements have O(1) complexity WHILE loop has O(log_32 n) = O(lg N) complexity Outer FOR loop has O(N) complexity Inner FOR loop has O(lg N) * O(N) complexity... because we looped j times N, and j came from the O(lg N) calculation O(N lg N) complexity printf has O(1) complexity Overall: O(1) + O(lg N) + O(N)*O(N lg N)*O(1) = O(N 2 lg N) complexity 4. 10 marks The following C function traverses a doubly-linked list consisting of 0 or more nodes from the current node, in the direction indicated by the input parameter (going left, or going right). For example, if you are at node Susan and you re supposed to go left, then, on the screen, print out all the names found in the nodes from the one before Susan to the start of the list, in that order (but do not include Susan (in the current node)). The same idea applies when going to the right. Assume that the following declaration is correct: struct Node char * name; struct Node * prev; struct Node * next; ; Note that if curr is a struct Node pointer, then: printf( %s\n, curr->name); prints out the name. Finish the following function. There is no dummy node at the start or the end of the list (i.e., it s just an ordinary list of nodes, possibly empty); so, you ll have to test for where the list ends. Note also that the function returns the number of names that were printed. int printnodesinonedirection(struct Node * curr, char LeftOrRight) // POST-CONDITION: This function starts at Node curr and then prints out // all the names in the nodes to the left ( L ) or right ( R ) of curr (but

// excluding the name in curr. The function returns a count of the number // of names printed on the screen. // PRE-CONDITION: curr is either NULL or points to a valid Node shown above // LeftOrRight is either L or R /* Declare any needed variables here. Follow this with your code. (Some code is provided further below.) */ int count = 0; if (curr == NULL) /* check for empty list */ return 0; if (LeftOrRight == L ) /* be sure to do the R part, too */ /* print out all the names of the nodes to the left of curr s node */ curr = curr->prev; while (curr!= NULL) printf( %s\n, curr->name); count++; curr = curr->prev; return count; /* otherwise we must be going to the right */ curr = curr->next; while (curr!= NULL) printf( %s\n, curr->name); count++; curr = curr->next; return count; 5. 2 marks If an algorithm runs in O( n lg n ) time, it is not possible to find another algorithm that performs worse on the same task: a) True b) False ANSWER: Circle (b) 6. 2 marks What is the most appropriate Big-O complexity for the following code fragment: for (k=1; k < 10*n; k++) print k; print n*n;

a) O(n). b O(10n). c) O(1). d) O(k). e) O(n 2 ) f) None of the above. ANSWER: Circle (a) because the code fragment loops O(n) times, and the two statements inside the FOR loop are both O(1) operations. 7. 2 marks What is the most appropriate Big-O complexity of the task of finding and deleting an element in a singly linked list (with n nodes) in the worst case? You can assume that you have a head pointer. a) O(1) b) O(n) c) O(n+1) d) O(n 2 ) e) O(n + n) f) None of the above. ANSWER: Circle (b) 8. 6 marks Consider the following C code. After each line, write down the contents of x and y. If at any point you cannot determine the contents, write unknown. int main(void) int x = 5, y = 15; int * p1, * p2; p1 = &x; // x contains 5 ; y contains 15 p2 = &y; // x contains 5 ; y contains 15 *p1 = 10; // x contains 10 ; y contains 15 *p2 = *p1; // x contains 10 ; y contains 10 p1 = p2; // x contains 10 ; y contains 10 *p1 = *p2+10; // x contains 10 ; y contains 20 system( pause ); return 0;

9. 10 points Write a function merge to merge two sorted arrays a and b into one, so that merged version of the array contains all elements of the two arrays combined into one list and they re in proper numeric order from smallest to largest). Use the following specification: void merge( int a[], int n1, int b[], int n2, int c[] ) Pre-conditions: a is a sorted array of integers, indexed from 0 to n1-1; b is a sorted array of integers, indexed from 0 to n2-1; c is a previously allocated array (in the caller) with size n1+n2 Post-conditions: c contains all the items from a, plus all the items from b, in sorted order, indexed from 0 to n1+n2-1; that is, the array c contains the merged result of the items in arrays a and b. Use the space given to plan your algorithm before you start coding. Hint: There are two steps: comparing the next unused element from each of a and b, and pick the smaller of the two to add to c. Be sure to correctly handling the remaining data once a or b is empty. /* Note: Comments are optional, but might help with part marks. */ int ax = 0; /* a s index */ int bx = 0; /* b s index */ int cx = 0; /* c s index */ /* Compare the next entry of each list to see which is smaller. Write the smaller of the two into the new array. */ while ( ax < n1 && bx < n2 ) if ( a[ax] < b[bx] ) /* a s next value was smaller */ c[cx] = a[ax]; ax++; else /* b s next value was smaller */ c[cx] = b[bx]; bx++;... continued on next page

/* we must have run out of either a s entries or b s entries */ while ( ax < n1 ) /* write the rest of a s entries */ c[cx] = a[ax]; ax++; while ( bx < n2 ) /* write the rest of b s entries */ c[cx] = b[bx]; bx++; /* end of function */