CSE 310 Quizes

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the struct definition and declaration: struct site{ char name[16]; int pages; }; struct site *ptr; How to assign "Quiz" to the field "name" in the structure?

(*ptr).name = "Quiz"; or ptr->name = "Quiz"; "->", "*", and "." are dereference operators. "->" and "." can be used interchangeably to access an attribute of an object while "*" is to access an object stored at an address. These operators also follow the order of operations, so parenthesis are used to tell the program to access the object first, then its attribute.

What is the output of function f assuming that parameter start points to first node of a singly linked list of the first 6 integers in order? 1->2->3->4->5->6 void f(struct node* start){ if(start == NULL) return; printf("%d ", start-data); if(start->next != NULL) f(start->next->next); printf("%d ", start->data); }

1 3 5 5 3 1

Consider a decision tree for Mergesort on 6 elements, At least how many leaf nodes does the decision tree have?

720

What is separate chaining?

A collision resolution technique for hash tables.

What does each internal node in a decision tree represent?

A comparison made in the algorithm.

What is a hash table?

A generalization of an ordinary array where a function of key is used as the index instead of using key as index directly.

Consider the hashing function h(k) = k mod m. What is a good value for m?

A prime number.

What does each leaf node represent?

An outcome of the problem.

Which of the traversal methods is sufficient to reconstruct the BST from the traversal. 1. in-order 2. pre-order 3. post-order

Either 2 or 3 in-order traversal will only return a sorted array of the elements of the BST, therefore unable to reconstruct the BST from the traversal.

In the case of the max-priority queue, the minimum element is the priority element.

False - name of the queue is self explanatory, the prioritized element is the max element.

Dynamic programming is strictly for optimization problems. It cannot be applied to a problem such as computing the n-th Fibonacci number, or computing the factorial of n.

False.

When solving a specific subproblem in the bottom-up approach, we might not have solved all the smaller subproblems its solution depends upon and might not have its result saved.

False.

What is true about heaps?

Heapsort is an in place sorting algorithm. In a max-heap, the largest element is at the root. The space complexity of heapsort is O(n).

Which traversal method of a BST produces the keys in sorted order?

In-order duh.

How do you insert a node into a heap?

Insert the node into the first available spot, then sort the heap.

Post-orderr

Left, right, root

In-order

Left, root, right

What is the height of any binary search tree with n nodes?

O(log n)

What is the run time for Increase-Key() in a max-heap of n size?

O(log n)

What is the upper bound on the running time of binary search for a key k in a sorted array of size n?

O(log n)

What is the run time to delete the maximum element in a max-heap?

O(log n) After deleting, Max-Heapify() is called to restore the max-heap property.

What is the run time for Quicksort's best case scenario?

O(n log n)

What is the run time for Quicksort's worst case scenario when pivot is the median?

O(n log n)

In a (binary) max-heap containing n keys, what is the upper bound running time to find the smallest key?

O(n)

What is the run time of Select(A, i)?

O(n)

Given two heaps with n elements each, what is the run time to construct a single heap comprising all 2n elements?

O(n) time

BottomUpCutRod(p, n) Array r[0 ... n] r[0] = 0 for j = 1 to n{ q = -infinity for i = 1 to j q = max(q, p[i] + r[j-i]) r[j] = q } return r[n] What is the running time of the algorithm?

O(n^2)

Are recursive problems efficient?

Recursive problems are not efficient because they solve subproblems again and again.

Pre-order

Root, left, right

What is true about the Select algorithm?

Select(A, i) returns the i-th order statistic. Select(A, 1) returns the minimum element in A. Calling Select(A, n/2) returns the median element if n is odd in O(n) time.

What is true about tabulation and memoization in dynamic programming?

Tabulation is used in a bottom-up algorithm whereas memoization is used in a top-down algorithm. Both tabulation and memoization are used to avoid recomputation of subproblems.

What is the height of a heap?

The amount of rows after the root.

Why can we use a max-heap as a priority queue?

The root can be identified as the highest priority node.

In a binary search tree, either the left or the right sub-tree can have a key equal to the root's key.

True - Inserted to the left is called left bias, and right is right bias.

The bottom up and memoized algorithms have the same asymptotic running time.

True.

Is the insertion operation faster in a sorted array or unsorted array?

Unsorted array - We do not have to care about the position at which the element is to be inserted.

When should you use dynamic programming?

When there is a repeated computation of subproblems Optimal substructure where the optimal solutions of subproblems are used to find the optimal solutions of larger subproblems. Recursive formulation of the problem.

What is a collision?

When two keys hash to the same value?

Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true? i. 9679, 1989, 4199 hash to the same value ii. 1471, 6171 hash to the same value iii. All elements hash to the same value iv. Each element hashes to a different value

i and ii only Because the hash function is x mod 10, the only part of the element that mattters is the ones place. So all the numbers with the same value in the ones place will hash to the same index.

What is the average number of comparisons made in both successful and unsuccessful searches? bool Search(int k, int *A, int n){ for(int i = 0; i < n; i++) if(A[i] == k) return true; return false; } key == A[0] T: return true F: key == A[1] T: return true F: key == A[2] T: return true F: key == A[3] T: return true F: return false

2.8 Possible # of comparisons when successful: 1, 2, 3, 4 Possible # of comparisons when unsuccessful: 4 Take the average: (1 + 2 + 3 + 4 + 4)/5 = 2.8

The following numbers are inserted into an empty binary search tree in the given order: 11, 2, 4, 6, 16, 13, 17. What is the height of the resulting binary search tree?

3

Given a rod of length n that we decide to cut at length i. How many subproblems are left after we make this cut?

1 subproblem of size n-i solved recursively. After cutting at length i, that cut becomes part of the solution and n-i is recursively solved.

How many children can the interior node of a decision tree have?

2 children

What is the average number of comparisons made in successful searches? bool Search(int k, int *A, int n){ for(int i = 0; i < n; i++) if(A[i] == k) return true; return false; } key == A[0] T: return true F: key == A[1] T: return true F: key == A[2] T: return true F: key == A[3] T: return true F: return false

2.5 Posssible # of comparisons when successful: 1, 2, 3, 4 Take average: (1 + 2 + 3 + 4)/4 = 2.5

What is the maximum number of comparisons made by the linear search: bool Search(int k, int *A, int n){ for(int i = 0; i < n; i++) if(A[i] == k) return true; return false; } key == A[0] T: return true F: key == A[1] T: return true F: key == A[2] T: return true F: key == A[3] T: return true F: return false

4, there are only four internal nodes

Given a hash table T with 25 slots that stores 1000 elements, what is the load factor a for T?

40 Load factor = # of elements / # of slots

Assume that a hash table is implemented using open addressing with linear probing with hash function h(k) = k mod 7, for a hash table of size 7. Insert the keys 44, 45, 79, 55, 91, 18, 63 (in that order) into the table. At what position in the table is key 18 inserted?

5 - Because the elements are hashed with linear probing, order is important, therefore we must know where elements 44, 45, 79, 55, then 91, are hashed. 44 is hashed to 2, 45 to 3, 79 to 2 and placed in 4, 55 to 6, 91 to 0, and 18 to 4 and placed in 5.

Keys 5, 4, 6, 1, 2 are mapped to a hash table of size 3 with hash function h(k) = k mod 3. If collisions are resolved by separate chaining, what are the values of a, b, c, d, e respectively as shown below? T 0 -> a -> / 1 -> b -> c -> / 2 -> d -> e -> /

6, 4, 1, 5, 2

A binary search tree is generated by inserting in order the following integers: 51, 16, 63, 6, 21, 59, 92, 4, 9, 38, 61, 25 The number of nodes in the left subtree and right subtree of the root, respectively, is:

7, 4

Which of the following statements are true regarding direct address tables? Statement 1: If the universe U is large, storing a table of size |U| may be impractical. Statement 2: Often, most of the space allocated to table T is wasted as the set K of keys actually stored is small.

Both of the statements are true. Storing a large table is impractical because one will be allocating a large table, where here may be collisions or often times, unused space. If for example, the set is [1 8 10 12 20], one may allocate memory of size 20 and store each at value - 1, there will be 20 spaces allocated for only 5 elements, leaving 15 spaces unused.

Which of the following statements are true? Statement 1: With a different interpretation, hash functions can be used even when the keys are strings. Statement 2: Hash functions should be fast, i.e, O(1) to compute.

Both statements are true.

Which of the following statements are true? Statement 1: Hash functions can be defined for keys that are strings. Statement 2: Linear probing suffers from primary clustering.

Both statements are true. Hash functions are used to determine the key for the item that is to be hashed, items include strings. These functions usually includes the sum of the letters. Linear probing is a method used when there is a collision. This places in the first open space in the hash linearly, which can cause another collision and more probing.

Considering the case of separate chaining in hashing. Which of the following are true? Statement 1: Performance of the dictionary operations is not good because of the use of a linked list. Statement 2: The chain is not maintained in sorted order.

Both statements are true. The use of a linked list has little effect towards the performance of dictionary operations. The element is simply inserted at the end of the chain, regardless of order.

What is true about hash tables?

Collisions can happen even when |K| <= m where K is the number of keys and m is the size of hash table. Deleting a key from hash table is O(1) if collisions are resolved by chaining and if the lists are doubly linked.

What is the output of the program fragment? struct site{ char name[] = "Quiz"; int pages = 200; }; struct site *ptr; printf("%d ", ptr->pages); printf("%s", ptr->name);

Compiler error - a new structure had not been allocated, only the ptr is declared, therefore when printing the program tries to access non existent values.

CutRod(p, n) if n==0 return 0 q = -infinity for i = 1 to n q = max(q, p[i] + CutRod(p, n - i)) return q What is true about the recursive rod cutting algorithm?

To solve the original problem of size n, we solve smaller problems of the same type. The rod cutting problem has the optimal substructure property, because optimal solutions to a problem incorporates optimal solutions to smaller subproblems.

In double hashing, the hash function h for probe i uses two hash functions h1 and h2: h1(k) = k h2(k) = 1 + (k mod (m - 1)) h(k, i) = [h1(k) + i*h2(k)] mod m Insert keys 10, 31, 11 into a hash table of size m = 5 using open addressing with double hashing as defined above. What is the final table?

[10, 31, EMPTY EMPTY, 11] i = 0 h1(10) = 10 h2(10) = 3 h(10, 0) = 0 i = 0 h1(31) = 31 h2(31) = 4 h(31, 0) = 1 i = 0 h1(11) = 11 h2(11) = 4 h(11, 0) = 1 i++ h(11, 1) = 0 i++ h(11, 2) = 4

Insert keys 7, 10, 25, 12, 17, 34 into a hash table of size 7 using open addressing, with the hash function h(k, i) = (k + i) mod 7 and linear probing to resolve collisions. What does the final array look like?

[7, 34, EMPTY, 10, 25, 12, 17]

BottomUpCutRod(p, n) Array r[0 ... n] r[0] = 0 for j = 1 to n{ q = -infinity for i = 1 to j q = max(q, p[i] + r[j-i]) r[j] = q } return r[n] What is true about the algorithm?

r[0] initialized to 0 indicates that there is no revenue for a rod of length 0. r[j] stores the solution to the rod of length j in line 7. The exponential time recursive algorithm Cut-Rod is transformed into an iterative algorithm that runs in polynomial time. -infinity is really -MAXINT in a finite precision arithmetic of a computer.


Ensembles d'études connexes

Exam 2 Econ, Econ 2305 Midterm #2, Econ Test

View Set

Chapter 16: Financial Management and Securities Markets

View Set

Chapter 6 - Communication and Guest Services

View Set

Social Scientists and Early Hominids

View Set

Chapter 10 Carrier Wide Area Networks (WAN)

View Set

Chapter 8 - Software Development Security

View Set