DSA 1 Quiz 5b Retake

¡Supera tus tareas y exámenes ahora con Quizwiz!

Implementing a hash table using chaining for collision resolution, array of 512, what is the maximum number of entries that can be placed in the hashtable?

Limited only by memory

What is the worst-case time for mergesort to sort an array of n elements?

O(n logn)

Suppose we implement a priority queue using an array of queues where the number of priorities is fixed. What is the worst case Big O for finding an element?

O(n)

Suppose we implement a priorityqueue but instead of a Heap, we use an unsorted vector. Which of the following best describes the worst-case run times for peek(), push(), and poll()?

O(n), O(1), O(n)

What is the worst-case time for heapsort to sort an array of n elements?

O(n^2)

Select all of the following TRUE statements about QuickSort. QuickSort is a stable sort. QuickSort easily lends itself to be a hybrid sort. QuickSort is an adjancy comparison sort. QuickSort is usually implemented as a recursive algorithm. QuickSort is generally not an in-place sort. QuickSort is considered the fastest average time sort.

QuickSort easily lends itself to be a hybrid sort QuickSort is usually implemented as a recursive algorithm QuickSort is considered the fastest average time sort.

Which of the following statements about the BinarySearch algorithm is NOT true? The algorithm runs in O(log n) time. The algorithm can be implemented iteratively or recursively. The best case run time is O(1). Correct! The worst case run time is O(n).

The worst case run time is O(n).

Consider a complete binary tree with exactly 10,000 nodes, implemented with an array. Suppose that a node has its value stored in location 4999. What can you say about its right child?

There is no right child

In BinarySearch, the best case occurs when:

The item is located in the middle of the collection.

Several different implementations of QuickSort were discussed in class. What is considered to be the main difference between these implementations?

The number of swaps required

Assume that a hash function has the following characteristics: h(203) = h(426) = h(561) = 5 h(987) = h(316) = 3 h(736) = h(605) = 16 h(124) = 0 Array size of 7. The keys are inserted in this order: 987, 203, 736, 316, 426, 561, 124, 605 Collision resolution is chaining. Which element can be accessed with a single probe?

124

Trace the action of HeapSort on the following collection of integers. What is the contents of the collection after 3rd iteration of pop()? Note that the vector is already a heap. [50, 21, 17, 10, 18, 11] 11, 18, 17, 10, 21, 50 21, 18, 17, 10, 11, 50 17, 11, 10, 18, 21, 50 18, 11, 17, 10, 21, 50

17, 11, 10, 18, 21, 50

Assume that a hash function has the following characteristics: h(203) = h(426) = h(561) = 5 h(987) = h(316) = 3 h(736) = h(605) = 16 h(124) = 0 Array size of 10. The keys are inserted in this order: 987, 203, 736, 316, 426, 561, 124, 605 If the collosion resolution is quadratic hashing, where does 561 end up?

2

A vector contains the numbers <7, 2, 9, 5, 4, 1> and Insertion Sort is run on this list. Which of the following list orderings will never occur during any step of the sorting process? We are only concerned with the state of the list at the beginning of each iteration of the outer loop (after each element is fully "inserted"). 2, 7, 9, 5, 4, 1 2, 5, 7, 9, 4, 1 2, 4, 5, 7, 9, 1 2, 7, 9, 4, 5, 1 1, 2, 4, 5, 7, 9

2, 7, 9, 4, 5, 1

< 6, 3, 9, 5, 4, 1> and insertion sort is run on the list. Which of the following will never occur during any step of the sorting process?

3, 6, 9, 4, 5, 1

Which statements are true about the time complexities of heap operations on a properly constructed heap stored as a tree in an array? A. The poll() (or remove) operation will have worst case time complexity theta(log n) B. The push() (or insert) operation will have best case time complexity theta(1). In other words, its possible that push will only have to do constant operations for some values. C. The push() (or insert) will have expected time complexity theta(log n)

A and B

We used an argument that involved a decision tree to prove that any comparison sort (like insertion sort or quicksort) has worst-case time complexity that is Ω(n log n). Which of the following statements are true about this lower-bounds argument? A. The internal nodes (i.e. non-leaf nodes) of the decision tree represent a comparison between two items in the list. B. The decision tree must have n(n-1)/2 leaves because that is the maximum number of inversions for a list of size n. C. The worst-case number of comparisons the sort must do is the length of the longest possible path from the root to a leaf.

A and C

Which of the following are true about selection sort? A. If the list is already sorted, selection sort will do theta(n^2) comparisons. B. If the list is in reverse sorted order, selection sort will do fewer comparisons than if the list was in sorted order.

A only

Which of the following statements A - C are true statements about storing a heap in an array? A. When an element is removed, the last element stored in the array replace the first value and then percolate_down() is called on that element. B. With each value we also store the location of the values parent in the tree (by storing the value and location in a node in the heap) C. The heap order property lets us find if a value is in the heap in logarithmic time.

A only

Which of the following statements about merge or Mergesort are true? A. Mergesort is a stable sort because, if there are two identical values in the first and second sublist being merged, it is simple for merge to make sure the value in the first sublist is copied to the merged list before the second value is copied. B. Mergesort will always use merge to combine two sublists that have equal sizes or sublists where one sublist has one more item than the other. C. When merge finds there is not another element in the first sublist to compare to an element in the second sublist, it will always need to copy the remaining elements from the second sublist into the merged array.

A, B, and C

Which of the following statements about merge or Mergesort are true? A. Mergesort is a stable sort because, if there are two identical values in the first and second sublist being merged. It is simple for merge to make sure that the value in the first sublist is copied to the merge list before the second value is copied. B. Mergesort will always use merge to combine the two sublist that have equal sizes or where one sublist has one more item than the other. C. When merge finds there is not another element in the first sublist to compare to an element in the second sublist. It will always need to copy the remaining elements from the second sublist into the merged array.

A, B, and C

list.add(someItem) Collections.sort(list) Which of the following must be true? A. The variable list bust be of a type that implements the Comparable interface B. The definition of hte class that devines object someItem must pmplement the Comparable interface C. The class definitions for the class that defines object someItem must have a method compareTo()

B and C

Which of the following are TRUE statements about the time-complexity of QuickSort? A. If the input is sorted in random order, this will always lead to the worst-case for quicksort. B. If the input is sorted in ascending order, this may lead to the worst-case for quicksort depending on how partition is implemented. C. We'll get a Θ(n2) time complexity for quicksort if partition always uses a partition value that is the maximum value element in the sublist being processed.

B and C are true

Which of the following data structures would be most appropriate (i.e., most efficient) for an application that is required to: 1. Add numbers. 2. Remove the minimum number. 3. Remove the maximum number.

BinarySearch Tree

To utilize the built-in sorting functions of Java supplied frameworks, a class must implement

Comparable

If you call the interrupt() method on a running thread, the thread will terminate immediately.

False

Which of the following data structures would be most appropriate (i.e., most efficient) for an application that is required to: Add words. Return a list of all the unique words in alphabetical order. Return the number of times a given word has been added.

Hash Table

Hash table of capacity 10. You are inserting integers into this table. The hash function and second hash function will be used are: hash(int key) = key %10 hash2(int key) = 1 + (key%3) Elements inserted: 5, 15, 8, 18, 11, 35 Where does 35 end up? For collision resulution you should use double hashing where hash(k)+n*hash2(k) as n increments by 1, starting at 0.

Index 4

Select all of the following TRUE statements about MergeSort. MergeSort easily lends itself to be a hybrid sort. MergeSort is usually implemented as a recursive algorithm. MergeSort is generally not an in-place sort. MergeSort is an adjancy comparison sort. MergeSort is a stable sort.

MergeSort easily lends itself to be a hybrid sort. MergeSort is usually implemented as a recursive algorithm. MergeSort is generally not an in-place sort. MergeSort is a stable sort.

Which of the following data structures would be most appropriate (i.e., most efficient) for an application that is required to: 1. Add "observed" words. 2. Add "known" words. 3. Return how many "observed" words are present in "known".

Priority Queue

Which of the following statements is TRUE about the biggest difference between the two different implementations of HeapSort we saw in class? **Solution 1 involved push() to the heap n times and then poll() from the heap n times. Solution 2 used a two-step process to transform the collection into a heap and then removing from the heap n-1 times. Solution 1 is not an in-place sort but Solution 2 is. Solution 2 requires one less call to poll(). Solution 1 is a stable sort but Solution 2 is not. There is essentially no difference between the 2 solutions.

Solution 1 is not an in-place sort but Solution 2 is.

The following multiple choice items explain the lower bounds argument covered in class that shows adjacent sorts must have a worst-case complexity of Ω(n2), but one of the steps contains an error. Select the item containing the error. 1. .An inversion is two items in a list that are out of relative order 2. The maximum number of inversions in a list is n + (n-1)*2 3. Each swap of two adjacent elements can fix at most one inversion 4. Therefore, if only one inversion is removed per operation, any algorithm is Θ(n2) or higher

Step 2

Which of the following is not true about the second, more difficult implementation of HeapSort we saw in class? Talking about solution in which the list you want to sort is used as the heap itself. Not talking about the version in which you push() to the heap n times and then poll() from the heap n times.

This implementation is stable because we always make sure to percolate items with the same key up past its parent only if the child element is a left child

If two threads are not sharing data, a race condition cannot exist

True

When using a Map data structure, all of the key objects must be unique

True

If a Bubble sort algorithm is implemented using a variable to count the number of swaps per iteration, it has a best case performance time of O(n). Under what conditions does this happen?

When the data is already sorted

Which of the following is NOT true about using the compareTo() method in Java to sort things? If x.compareTo(y) returns zero, then bubble sort may not know how to sort the list correctly. Objects in Java cannot be sorted because the Object class does not implement the Comparable interface. If x < y, then the x.compareTo(y) method must return any negative integer. You could rewrite the body of your compareTo() method in order to sort a list in descending order instead of ascending order. If x.compareTo(y) returns a negative integer, then y.compareTo(x) will definitely return a positive integer.

You could rewrite the body of your compareTo() method in order to sort a list in descending order instead of ascending order.

Given the following heap, poll(remove) two values. 100 / \ 50 30 / \ 10 20

[30, 20, 10]

insert() the following elements into a Max-Heap: 5, 6, 2, 4, 1, 3. call poll on heap exactly once. Which of the following is the array representation of the heap after these operations?

[5, 4, 3, 2, 1]

Given the following collection of sorted integers named arr: [5, 17, 22, 30, 34, 39, 40, 43, 51, 55] and a correctly implemented recursive BinarySearch, what will the values of the variable low, mid, and hi be in the third recursive call if the first call is binarySearch(arr, 50, 0, arr.size()-1) ?

low=8, mid=8, hi=9

Consider the following code to find the index for the appropriate bucket in a hash table using hash_function as the hashtag function: int index = Math.abs(hash_function(key)); index = index%table_size; There is a value for index that can cause problems. What is it?

minimum value of an integer

Supposed you are implementing a hash table using linear probing for collision resultion. Your logic for remove is to find the key in the hashtable, set that bucket to null, and decrement the size of the hashtable. Which of the following is the likely result of implementing this logic for the remove function?

retrieve function might be wrong

What course of action should be followed when a thread has been interrupted (due to an exception)?

the thread should execute the code in the appropriate catch block

The Runnable interface has a(n) _______() method stub, which needs to be implemented when a class implements Runnable. This method contains the code that a thread should execute

threadRun()


Conjuntos de estudio relacionados

ATI Learning System RN: Fundamentals 1, 2, and Final

View Set

5.9 Security Fundamentals - Describe wireless security protocols (WPA, WPA2, and WPA3)

View Set

2 - The Firm and Market Structures

View Set

Life Insurance Underwriting and Policy Issue

View Set