You are allowed nothing except a writing utensil and a 3 x 5 index card, which MUST BE TURNED IN WITH YOUR EXAM.

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

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures and Algorithms Written Examination

6. Standard Algorithms

DATA STRUCTURES USING C

Data Structures. Level 6 C Module Descriptor

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:

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Zabin Visram Room CS115 CS126 Searching. Binary Search

Change Impact Analysis

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

CompSci-61B, Data Structures Final Exam

Ordered Lists and Binary Trees

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

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

Analysis of a Search Algorithm

Binary Heap Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

Specimen 2015 am/pm Time allowed: 1hr 30mins

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

What Is Recursion? Recursion. Binary search example postponed to end of lecture

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

Data Structures and Algorithms

10CS35: Data Structures Using C

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

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structures. Algorithm Performance and Big O Analysis

LINKED DATA STRUCTURES

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

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

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

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Node-Based Structures Linked Lists: Implementation

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

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

Number Systems and Radix Conversion

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.

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Loop Invariants and Binary Search

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

AP Computer Science AB Syllabus 1

Lecture Notes on Binary Search Trees

Symbol Tables. Introduction

Python for Rookies. Example Examination Paper

Stack Allocation. Run-Time Data Structures. Static Structures

PES Institute of Technology-BSC QUESTION BANK

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

COMPUTER SCIENCE A. Course Description

Persistent Binary Search Trees

Binary Search Trees CMPSC 122

Searching Algorithms

Efficient Data Structures for Decision Diagrams

Introduction to Data Structures

Chapter 3. if 2 a i then location: = i. Page 40

Introduction to Programming System Design. CSCI 455x (4 Units)

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

D06 PROGRAMMING with JAVA

Binary search algorithm

CS 103X: Discrete Structures Homework Assignment 3 Solutions

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

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Chapter 8: Bags and Sets

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

Unit 6. Loop statements

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Heaps. CSE 373 Data Structures

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

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)

Lecture Notes on Linear Search

Data Structures and Data Manipulation

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Pseudo code Tutorial and Exercises Teacher s Version

Data Structures and Algorithms Lists

Introduction to Object-Oriented Programming

Sample Questions Csci 1112 A. Bellaachia

Practical Session 4 Java Collections

7.1 Our Current Model

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 13: Query Processing. Basic Steps in Query Processing

Sequences in the C++ STL

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

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

02-201: Programming for Scientists

CmpSci 187: Programming with Data Structures Spring 2015

Sequential Data Structures

Linked Lists: Implementation Sequences in the C++ STL

Programming and Software Development CTAG Alignments

C++FA 5.1 PRACTICE MID-TERM EXAM

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Data Structure [Question Bank]

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Transcription:

CS 36 Data Structures Summer 2011 Exam 1: Lists, Stacks, Queues, Recursion, Searching You have 75 minutes to complete this exam. You are allowed nothing except a writing utensil and a 3 x 5 index card, which MUST BE TURNED IN WITH YOUR EXAM. Be sure to show your work and explain all your answers. Partial credit may be awarded for incorrect answers with thorough explanations, and a correct answer with no work or explanation may be awarded little or no credit. If you are asked to write code but do not know the exact Java syntax required, approximate as best you can. If you feel your code is not enough to demonstrate your understanding of the problem, describe your algorithm in English as well. Don t make me guess at what you re thinking. BE SURE TO WRITE YOUR NAME ON THIS PAGE. 1

1. Perform a radix sort on the following list of numbers. Be sure to show the bins (queues) at each step of the algorithm: [312, 8, 21, 16, 201, 1007, 9, 2851, 1] One s digit: 0: 1: 21, 201, 2851, 1 2: 312 3: 5: 6: 16 8: 8 9: 9 [ 21, 201, 2851, 1, 312, 16, 1007, 8, 9 ] Ten s digit: 0: 201, 1, 1007, 8, 9 1: 312, 16 2: 21 3: 5: 2851 6: 8: 9: [ 201, 1, 1007, 8, 9, 312, 16, 21, 2851 ] Hundred s digit: 0: 1, 1007, 8, 9, 16, 21 1: 2: 201 3: 312 5: 6: 8: 2851 9: [ 1, 1007, 8, 9, 16, 21, 201, 312, 2851 ] Continued on next page... 2

Thousand s digit: 0: 1, 8, 9, 16, 21, 201, 312 1: 1007 2: 2851 3: 5: 6: 8: 9: [ 1, 8, 9, 16, 21, 201, 312, 1007, 2851 ] After this, if we kept track of the largest integer in our array, we know we can stop. Otherwise, if we perform one more round, we will see that all of the numbers go into the 0th bin. 3

2. Consider the following implementation of a list. My class contains three fields: a head variable, which anchors the linked list; a count variable which keeps track of the length n of the list; and a shortcuts variable which anchors a second linked list of length n. (For simplicity, you may assume that n is a perfect square, so n is an integer.) Each node in this second linked list contains as its data a pointer to a node in the linked list anchored at head. These shortcut pointers are evenly spaced throughout the big linked list. For example, if n = 100, then the shortcuts list will have 10 nodes, pointing to nodes 0, 10, 20,..., 80, 90 of the big linked list. Using whatever combination of words, pictures, and code that you need to get your point accross, describe how you would implement the get() method of this new data structure. Also explain what this method s runtime would be. Here is a rough implementation of the get() method. You could also have shown this with pictures, pseudocode, etc. public E get(int pos) { Node current = shortcuts; int count = 0; while (count + Math.sqrt(n) < pos) { current = current.getnext(); count += Math.sqrt(n); current = current.getdata(); while (count < pos) { current = current.getnext(); count++; return current.getdata(); This will have a runtime of O( n), where n is the length of the bigger linked list. This is because each loop can run a maximum of n times. The first loop can at most traverse through each of its n elements. The second loop will only ever have a subsection of the big list of length n to traverse through as well we would never need to traverse any further, because otherwise, we d have skipped one more ahead in the shortcuts list. All of the other operations are constant time, so we have O( n + n) = O( n). 4

3. In our implementation of the GCD function, we used the modular operator % which computes the remainder when dividing two integers. For this problem, you will write this function yourself. Your method will take two integers, a and b, and return their remainder. One way to do this would be to repeatedly subtract b until you get to the remainder though of course, it is important to know when to stop! The method signature looks like this: int remainder(int a, int b) {... Write a recursive version of this method: int remainder(int a, int b) { if (a < b) return a; return remainder(a-b, b); Write an iterative version of this method: int remainder(int a, int b) { while (a >= b) { a -= b; return a; Describe the runtime of either of your methods. (Hint: it will depend on a and b.) The recursive method will have the same number of recursive calls as the iterative method has iterations of the while-loop: namely, a/b. There are nothing but constant-time operations within each recursive call or loop iteration, so this makes our run-time O(a/b). 5

4. In this problem, you will implement a method that takes two pieces of input: a sorted array of integers arr and an integer v. It returns a boolean value true if v is contained in arr and false otherwise. Your implementation should have a runtime of O(log n), where n is the length of arr. Explain why your method has this runtime. You may assume that arr and v are not null, that arr contains no null values, and that arr is sorted. public static boolean arraycontains(int[] arr, int v) { int min = 0; int max = arr.length - 1; int mid = max + min / 2; while (max - min > 1) { if (arr[mid] == v) { return true; else if (arr[mid] < v) { min = mid; mid = (min + max) / 2; else { max = mid; mid = (min + max) / 2; return false; Runtime justification: This method just implements binary search. It has a runtime of O(log n) because we are halving the size of our search space with each iteration of the loop. We can only halve the space log 2 n times. The rest of the operations are all O(1), so we are looking at O(log 2 n) = O(log n). 6