Sample Questions Csci 1112 A. Bellaachia



Similar documents
Binary Heap Algorithms

Data Structure [Question Bank]

PES Institute of Technology-BSC QUESTION BANK

DATA STRUCTURES USING C

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

Exam study sheet for CS2711. List of topics

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

10CS35: Data Structures Using C

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

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

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

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

Converting a Number from Decimal to Binary

Binary Search Trees (BST)

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:

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

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

CompSci-61B, Data Structures Final Exam

Analysis of Algorithms I: Optimal Binary Search Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Pseudo code Tutorial and Exercises Teacher s Version

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

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

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

Ordered Lists and Binary Trees

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

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Lecture Notes on Binary Search Trees

International Journal of Software and Web Sciences (IJSWS)

Data Structures in Java. Session 15 Instructor: Bert Huang

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

Binary Search Trees CMPSC 122

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Data Structures and Algorithms

Lab Manual. Data Structures (Pr): COT-213 Data Structures (P): IT-215

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

Analysis of a Search Algorithm

Data Structures and Algorithms Written Examination

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.

Data Structures Fibonacci Heaps, Amortized Analysis

6 March Array Implementation of Binary Trees

Review of Hashing: Integer Keys

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures. Level 6 C Module Descriptor

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Algorithms and Data Structures

Section IV.1: Recursive Algorithms and Recursion Trees

Java Software Structures

CS/COE

Lecture 1: Course overview, circuits, and formulas

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Binary Heaps. CSE 373 Data Structures

Cpt S 223. School of EECS, WSU

Algorithms. Margaret M. Fleck. 18 October 2010

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

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

COMPUTER SCIENCE. Paper 1 (THEORY)

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

FINAL EXAM SECTIONS AND OBJECTIVES FOR COLLEGE ALGEBRA

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Abstraction and Abstract Data Types

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Math Common Core Sampler Test

DATABASE DESIGN - 1DL400

Introduction to Data Structures and Algorithms

Zeros of Polynomial Functions

Introduction to data structures

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)

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

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Heaps & Priority Queues in the C++ STL 2-3 Trees

The ADT Binary Search Tree

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

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

Algorithms and Data Structures

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

Lecture 1: Data Storage & Index

Fast Sequential Summation Algorithms Using Augmented Data Structures

OPTIMAL BINARY SEARCH TREES

COMPUTER SCIENCE 1999 (Delhi Board)

Algebra Unpacked Content For the new Common Core standards that will be effective in all North Carolina schools in the school year.

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Common Data Structures

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 14 The Binary Search Tree

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Binary search algorithm

Binary Trees and Huffman Encoding Binary Search Trees

AP Computer Science AB Syllabus 1

Mathematics Review for MS Finance Students

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

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Chapter 13: Query Processing. Basic Steps in Query Processing

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

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

Transcription:

Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k -1 i 1 k 1 o Geometric series: N A N 1 1 A i i 0 A 1 Special case when A = 2 2 0 + 2 1 + 2 2 + + 2 N = 2 N+1 1 o Some Math Formulas Properties of logarithms: log b (xy) = log b x + log b y log b (x/y) = log b x - log b y log b xa = alog b x log b a= log x a/log x b Properties of exponentials: a (b+c) = a b a c a bc = (a b ) c a b /a c = a (b-c) b = a log a b b c = a c*log a b A. Bellaachia, Algorithms & Data Structures Page 1

1. Problem For i=1 to n m=i, j=1; While (j<=m) do O(1); j=j+1; endwhile; j=1; While (j<=n) do for i=1 to j do O(1); endfor; j = j*; endwhile; Show that the time complexity of the following code is: O(n 2 logn) i = 1; while( i < n ){ doit(); i = i*; 2. Problem The following M[n][n] matrix is considered a space matrix. Example: (1) Characterize the non zero elements of M. (2) Give a mapping function required to store M in a one-dimensional array M'.. Problem (1) Given two sorted singly linked list L1 and L2, write a pseudo-code that merges the two list in a new list L (2) Repeat (1) without using a new list L; merge L1 and L2 in L1. A. Bellaachia, Algorithms & Data Structures Page 2

. Problem Write a non recursive pseudo-code that prints the elements of a singly linked list in reverse, using only constant extra space. What is the time complexity of your algorithm? 5. Problem Write a pseudo-code to swap two adjacent nodes by adjusting only the links(and not the data) using: a. Singly linked list b. Doubly linked list 6. Problem In this problem we would like to delete the first inserted k elements of a stack. The following example shows the state of the stack after we delete the first inserted three elements in a stack: 10 0-1 -20 10 100 0 200-1 -5-20 -19 100 a) Write an algorithm that deletes the first k inserted elements of a stack S. b) What is the time complexity of your algorithm? 7. Problem Write a function to test if a string is a palindrome using a stack. What is the time complexity of your algorithm? 8. Problem Given an expression string, write a program using a stack that checks whether the pairs and the orders of {,, (, ), [","] are correct in the string. For example, the program should print true for the following expression: [(a+b)]-{a-b-c+ {[(e+f)+(t+h)]-(a-g) And false for this expression: [(a+b]-c) 9. Problem Write a non recursive pseudo-code that prints the elements of a singly linked list using a stack data structure. What is the time complexity of your algorithm? A. Bellaachia, Algorithms & Data Structures Page

10. Problem Given the following code: Stack Mysterystack = new Stack(10); int N = 50; while (N >1 ) { Mysterystack.push(N % 2); N = N / 2; System.out.println("=============================="); while (!Mysterystack.isEmpty()){ System.out.println(Mysterystack.top()); Mysterystack.pop(); (a) (b) Explain in general what does this code return? What is the output of this code for the following? - N = 0 [Ans: 1110] - N = 0 [Ans: 01000] - N = 60 [Ans: 11100] 11. Problem Write a pseudo-code that determine whether a string is in the language L, where L { s { a, b * suchthat s isthe forma n b n 12. Problem Given two queues Q1 and Q2, write a pseudo-code that counts how many elements in the same position of the two queues are equal. The queues may not be the same length. Do not modify the queues. 1. Problem: Consider the following recursion function: void function(int n) { cout << n << endl; if (n>1) function(n-1); (1) What is the output of this function when n = 5? (2) What is the output of this function when n = 8? () Explain what does this function do? 1. Problem A. Bellaachia, Algorithms & Data Structures Page

Give a recursive method for removing all the elements from a stack. 15. Problem The nth power of a non-zero number can be recursively defined as follows: Write a recursive function that computes the nth power of a number using the recurrence relation above. 16. Problem The nth power of a non zero number can also be recursively defined as follows: Write a recursive function that computes the nth power of a number using the recurrence relation above. A. Bellaachia, Algorithms & Data Structures Page 5

17. Problem Write an algorithm that returns the minimum value in a binary search tree. (Hint: Go to the left child of the root and keep going to the left child until you come to a leaf node. This node is the minimum.) What is time complexity of your algorithm? 18. Problem Write an algorithm that returns the maximum value in a binary search tree. (Hint: Go to the right child of the root and keep going to the right child until you come to a leaf node. This node is the maximum.) What is time complexity of your algorithm? 19. Problem Write a recursive algorithm that returns the total number of nodes in a binary tree. What is time complexity of your algorithm? 20. Problem Given the following graph 1 A B C D E F 1 9 8 G H I 5 (1) Give two different DFS traversals for G and their corresponding DFS spanning trees (2) Give two different BFS traversals for G and their corresponding DFS spanning trees 1 21. Problem Write a program that returns the maximum degree of an undirected graph G. What is the time complexity of your program? Explain. A. Bellaachia, Algorithms & Data Structures Page 6

22. Problem Write an algorithm that takes a binary tree and return a new tree. The new tree swaps the left and right child of every node. For example: R 7 10 2 6 9 11 8 R 7 10 The swapped tree is: 11 9 6 2 8 2. Problem Write an algorithm that copies a binary tree T1 into a new tree T2. 2. Problem (1) Show the result of inserting, 1,, 6, 9, 2, 5, 7, 0 into an initially empty binary search tree B. Draw you tree. (2) Delete the root and draw your BST B. () Delete the node that contains 6 in B and draw your BST B. A. Bellaachia, Algorithms & Data Structures Page 7

25. Problem We would like to change Breadth First search (BFS) traversal to a new traversal called SBFS. Instead of using a queue, SBFS uses a stack. Give a DFS, BFS and SBFS spanning trees for the following graph: 1 A B C D E 9 F 1 8 G H I 5 1 26. Problem (1) Show the result of inserting, 1,, 6, 9, 2, 5, 7, 0 into an initially empty min-heap B. Draw your heap. (2) Delete the root of B and draw the new heap B. () Delete the root of B and draw the new heap B. 27. Problem Every character in a given computer is decoded as two digits, according to the following code: 0-9 00 through 09 A-I 11 through 19 J-R 21 through 29 S-Z 2 through 9 Now consider this hashing-coding problem. There is a hash table of 100 entries, indexed by two digits decimal subscripts. We would like to hash words of letters. A hashing method is proposed in which h(w) is computed by dividing the word by 10 (integer division), then taking the rightmost two digits of the result. Find h(w) for each of the keys MARY, JACK, WELL, MACK, BOLD. 28. Problem Consider inserting the keys 10, 22, 1,, 15, 28, 17, 50, 51 into a hash table of length m = 1 using open addressing with the auxiliary hash function F(x) = x mod 1. Show the hash table of the given list using the following: - Linear probing, - Quadratic probing A. Bellaachia, Algorithms & Data Structures Page 8

- Double hashing with F1(x) = x and F2(x) = 1 + (x mod(m-1)). 29. Problem Give an algorithm that checks whether an undirected graph G is a complete graph. State the data structure you are using to store your graph. 0. Problem An example of an undirected star graph is: Give a recursive strategy to check whether a graph S is a star graph. A. Bellaachia, Algorithms & Data Structures Page 9