Data Structures Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

False

(T/F) Assuming that the sorting algorithm sorts the input sequence in ascending order. If the input is already in ascending order, then insertion sort runs in O(n2) time.

False

(T/F)A binary tree is said to be a heap data structure if it is a complete binary tree.

True

(T/F)A recursive definition can have one or more base cases

True

(T/F)A stack data structure can be implemented using an array or a linked list.

True

(T/F)An initially empty queue Q has performed a total of 32 enqueue operations, 10 first operations which checks the element at the head of the queue without taking it out, and 15 dequeue operations, 5 of which returned null to indicate an empty queue. The current size of the queue is 22

False

(T/F)Any iterative procedure ca be converted into a recursive procedure

False

(T/F)Binary recursion is an example of tail recursion

false

(T/F)For a queue, "enqueue" and "dequeue" cancel each other out: an enqueue of any value followed by a dequeue leaves the queue in exactly the state it was in initially.

false

(T/F)Given an arbitrary binary tree, a postorder traversal of that tree will visit the nodes in the exact reverse order of a preorder traversal.

False

(T/F)If a code has O(1) time complexity, then the wall clock time to execute the code is exactly the same for all possible inputs.

True

(T/F)If you insert 16 elements in an empty BST, the possible heights of the resulting BST is anywhere between 4 and 15.

False

(T/F)In Java, String class implementation is faster than that of StringBuilder since it grows the array by constant size when full.

true

(T/F)In a binary tree with two nodes, the root is not a leaf node.

False

(T/F)In a linked list implementation of stack, in push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.

True

(T/F)In a min-heap, parent node can have value equal to one of its children.

False

(T/F)In the implementation of linked list data structure, the "head" variable is an instance of the "Node" class.

True

(T/F)The depths of any two leaves in a max heap differ by at most 1.

False

(T/F)The number of operations executed by algorithms A and B is 8nlogn and 2n^2 respectively. Algorithm A is faster than Algorithm B, for any possible input size

False

(T/F)The runtime of inserting an element into a BST is guaranteed to be O(log n).

True

(T/F)The space complexity of the recursive code to compute a sum of a list is O(n) int sum(int sum){ if (n<= 0) return 0; return n +sum(n-1); }

True

(T/F)Time complexity of algorithm A and B are as follows: A: O(logn) B: O(n) For a maximum input size of 8, A is faster than B.

true

(T/F)To be a heap data structure, one of the conditions is that the binary tree must be a complete tree.

False

(T/F)When the runtime of a algorithm X is asymptotically more efficient than that of another algorithm Y, it implies that X will always be a better choice for any input size

True

(T/F)You are given a set of n distinct elements, and an unlabeled binary tree with n nodes. You can populate the tree in only 1 way with the given set such that the tree becomes a binary search tree.

46, 34, 42, 23, 52, 33

A hash table of length 10 uses linear probing to insert elements into the hash table using hash function h(k)=k mod 10. After inserting 6 values into an empty hash table, the table looks as shown in the picture. Which one of the following choices gives a possible order in which the key values could have been inserted in the table? | 0 | | | 1 | | |2 |42| |3 |23| |4 |34| |5 |52 | |6 |46| |7 |33| |8 | | |9 | | o 46, 42, 34, 52, 23, 33 o 34, 42, 23, 52, 33, 46 o 46, 34, 42, 23, 52, 33 o 42, 46, 33, 23, 34, 52

O(nlogn)

Algorithm A executes an O(logn) time computation for each entry of an array storing n elements. What is the worst case run time?

O(2^n)

Algorithm: TowersOfHanoiMoves Input: sequence of numbers [1 ... n] denoting number of discs Output: sequence of numbers [ a1, a2, ..., an] corresponding to disc count (1, 2, ..., n) denoting total moves for each disc count. MoveCount() for (i = 1 to n) a1 = solveTowersOfHanoi( i ); end What is the time complexity of the MoveCount algorithm? o O(n) o O(n2^n) o O(2^n)

log n, log n

Assume that you are implementing a priority queue PQ that returns the min element on dequeue operation.If we use a min-heap to implement the PQ, enqueue is O(____) operation, and dequeue is O(_____) operation

O(1)

Assuming that the hash function for a table works well, and the size of the hash table isreasonably large compared to the number of items in the table, the expected (average) time needed to find an item in a hash table containing n items is o O(1) o O(log n) o O(nlog n) o O(√n)

All of these

For the Abstract Data Type "List", which of the following implementations are possible ? o Singly Linked List o Doubly Linked List o Array o ArrayList

O(n^2)

Given a n-elemetn array X, algorithm D calls algorithm E on each element X[i]. Algorithm E runs in O(i) time when it is called on element X[i]. What is the worst case running time of algorithm D?

45

If G is a simple undirected graph with 12 vertices ad 3 connected component, what is the largest number of edges it might have? 3 12 36 45

O(n)

In a binary min-heap containing n numbers, the largest element can be found in time o O(n) o O(log n) o O(n * log n) o O(1)

all of these

In an array-based implementation of a doubly linked list, identify which are correct statements. o Implementation of dequeue operation has time complexity O(1). o Implementation of "size" operation has time complexity O(1) o Implementation of enqueue operation has time complexity O(1). o Implementation of "first" operation to check the value of the first element has time complexity O(1).

Inorder successor is always either a leaf node or a node with empty left child.

In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation? o Inorder Successor is always a leaf node. o Inorder successor is always either a leaf node or a node with empty left child. o Inorder successor may be an ancestor of the node. o Inorder successor is always either a leaf node or a node with empty right child.

O(n^2)

List<Integer> lst = new LinkedList<Integer>(); for (int i=0; i< N; i++) lst.add(lst.size(), i); What is the time complexity of this code snippet ? Assume that LinkedList class is implemented as singly linked list, and contains variables "head" and "size". NOTE: Method signature from Javadocs for class LinkedList: add(int index, E element) Inserts the specified element at the specified position in this list. size()Returns the number of elements in this list. O(1) O(n) O(n^2) It may vary depending on the value of N

Smaller instances of the same problem

Recursion is a method in which the solution of a problem depends on ______ O Larger instances of different problems O Larger instances of the same problem O Smaller instances of the same problems o Smaller instances of different problems

0 1 2 3 4 5 6 7 8 9

Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree? o 7 5 1 0 3 2 4 6 8 9 o 0 2 4 3 1 6 5 9 8 7 o 0 1 2 3 4 5 6 7 8 9 o 9 8 6 4 2 3 0 1 5 7

O(nlogn)

Suppose we have a O(n) time algorithm that finds median of an unsorted array. Now consider a QuickSort implementation where we first find median using the above algorithm, then use median as pivot. What will be the worst case time complexity of this modified QuickSort ? o O(n^2logn) o O(n^2) o O(n^2logn) o O(nlogn)

False

Suppose we represent a graph G having n vertices ad m edges with the edge list structure. The, the insertVertex method and the removeVertex method both run in O(1) time

The number of elements to the right of the pivot is approximately equivalent to the number of elements to the left of the pivot.

The best case, in terms of the asymptotic running time, for QuickSort happens when: o The array is already sorted. o The array is sorted in reverse. o All elements of the array are negative. o The number of elements to the right of the pivot is approximately equivalent to the number of elements to the left of the pivot.

True

The depth first search traversal of a graph results into a tree

20, 20, 10, 10, 20

The following sequence of operations is performed on a stack : PUSH (10), PUSH (20), POP, PUSH (10), PUSH (20), POP, POP, POP, PUSH (20), POP. The sequence of values popped out is: 20, 10, 20, 10, 20 20, 20, 10, 10, 20 10, 20,20,10,20 20,20,10,20,10

63 and 6, respectively

The height of a tree is the length of the longest root-to-leaf path in it. The maximum and the minimum number of nodes in a binary tree of height 5 are: 63 and 6, respectively 64 and 5, respectively 32 and 6, respectively 31 and 5, respectively

depth

The number of edges from the root to the node is called _____of the node.

15, 10, 23, 25, 20, 35, 42, 39, 30

The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree? o 10, 20, 15, 23, 25, 35, 42, 39, 30 o 15, 10, 25, 23, 20, 42, 35, 39, 30 o 15, 20, 10, 23, 25, 42, 35, 39, 30 o 15, 10, 23, 25, 20, 35, 42, 39, 30

O(n^3)

The running time of a program is expressed as, f(n) = 4n^3 + 3n^2+100 A. O(n^5) B. O(n^4) C. O(n^3) D O(n)

X will always be a better choice for larger inputs

What does it mean when we say that an algorithm X is asymptotically more efficient than Y?

Prints all nodes of linked list in reverse order

What does the following function do for a given Linked List with first node as head? void fun1(struct node* head) { if(head == NULL) return; fun1(head->next); printf("%d ", head->data); } o Prints all nodes of linked lists o Prints all nodes of linked list in reverse order o Prints alternate nodes of Linked List o Prints alternate nodes in reverse order

Left subtree is always visited before right subtree

What is common in three different types of tree traversals (Inorder, Preorder and Postorder)? o Root is visited before right subtree o Left subtree is always visited before right subtree o Root is visited after left subtree o None of the above

O(n)

What is the average case time complexity for finding the height of a binary tree? o O(log log n) o O(n * log n) o O(n) o O(log n)

O(n)

What is the best-case time complexity of bubble sort? For example, if the input is already sorted as desired. o O(n^2) o O(nlogn) o O(n) o O(nlog)^2

O(N * N)

What is the time complexity? int a =0; for (i=0; i<N; i++){ for(j=N; j>i; j--){ a=a+i+j; }}

int solution( int N ) { if ( base case ) { return something easily computed } else { divide problem into pieces return something calculated from the solution to each piece } }

Which answer is a correct skeleton for a recursive Java method? o int solution( int N ) { if ( base case ) { return something easily computed } else { divide problem into pieces return something calculated from the solution to each piece } } o int solution( int N ) { if ( base case ) { return something easily computed } else { return solution(N) } } o int solution( int N ) { divide problem into pieces return something calculated from the solution to each piece } o int solution( int N ) { divide problem into pieces if ( base case ) { return something easily computed } else { return something calculated from the solution to each piece } }

Inserting and deleting a new element in an array of elements is expensive, whereas both insertion and deletion can easily be done in Linked Lists Searching for an element is more expensive in linked list compared to arrays Size of the array is fixed, but linked list can grow dynamically

Which of the following are correct statement(s) about array and linked list ? o Inserting and deleting a new element in an array of elements is expensive, whereas both insertion and deletion can easily be done in Linked Lists. o Array and linked list require same amount of memory to represent the same data. o Searching for an element is more expensive in linked list compared to arrays o Size of the array is fixed, but linked list can grow dynamically

o Adjacency list representation uses less space when the graph is sparse o DFS and BSF can be done in O(V+E) time for adjacency list representation. These operations take O(V^2) time in adjacency matrix representation. Here V and E are umber of vertices and edges respectively o Adding a vertex in adjacency list representation is easier than adjacency matrix representation

Which of the following is an advantage of adjacency list representation over adjacency matrix representation of a graph o Adjacency list representation uses less space when the graph is sparse o DFS and BSF can be done in O(V+E) time for adjacency list representation. These operations take O(V^2) time in adjacency matrix representation. Here V and E are umber of vertices and edges respectively o Implementing the functions -outdegree() or indegree() for a graph represented using adjacency matrix will be faster than for the same graph represented using adjacency list o Adding a vertex in adjacency list representation is easier than adjacency matrix representation

both have the same average case complexity. both are recursive. both take O(n) to divide the problem or combine the pieces.

Which of the following is/are TRUE of mergesort and quicksort? o both have the same average case complexity. o both are recursive. o both take O(n) to divide the problem or combine the pieces. o Both can have worst case time complexity as O(n^2).

Delete an element Insert a new element

Which of the following operations is not O(1) for an array of sorted data. You may assume that array elements are distinct. o Find the ith largest element o Delete an element o Find the smallest element o Insert a new element

All keys hash to same index

Which of the following scenarios leads to linear running time for a random search hit in a linear-probing hash table? o All keys hash to same index o All keys hash to different indices o All keys hash to an even-numbered index o All keys hash to an odd-numbered index

insertion sort

Which of the following sorting algorithms in its typical implementation gives best performance when applied on an array which is sorted or almost sorted (maximum 1 or two elements are misplaced). o Quick sort o Heap sort o Merge sort o Insertion sort

Selection sort

Which one of the following in place sorting algorithms needs the minimum number of swaps? o Quick sort o Insertion sort o Selection sort o Heap sort

O(1);O(1)

You are given a recursive function as follows: fn(n): if n <= 1: return 1 return fn(n-1) + fn(n-1) What is the time complexity and space complexity of the algorithm ? O(1); O(1) O(2^n); O(2^n) O(n); O(2^n) O(2^n); O(n)

Transfers all elements from stack S onto stack T , so that the element at the top of S is the first to be inserted onto T , and the element at the bottom of S ends up at the top of T

You are given the implementation of a method - transfer(S,T) - as follows: static <E> void transfer(Stack<E> S, Stack<E> T) { while (!S.isEmpty()) { T.push(S.pop());}} What does this function do ? o Transfers all elements from stack S onto stack T , so that the element at the top of S is the first to be inserted onto T , and the element at the bottom of S ends up at the top of T o Transfers all elements from stack S onto stack T, so that the element that starts at the top of S is the last to be inserted onto T , and the element at the bottom of S ends up at the bottom of T o Is equivalent to T=S o The code will throw error

merge sort

You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate? o Heap sort o Merge sort o Quick sort o Insertion sort

13

int fun(int x, int y) {if (x == 0) return y; return fun(x - 1, x + y); } What is the output of fun(4,3) ? o 13 o 12 o 9 o 10

[12, 17, 29, 56, 18]

public class Test { public static void main(String[] args) { Random rand = new Random(); PriorityQueue<Integer> pq = new PriorityQueue<>(5); for (int i = 0; i < 5; i++) { int x = rand.nextInt(100); pq.add(x); }}} The random numbers generated in the following sequence: 12, 56, 29, 18, 17 Which is the most likely sequence of the numbers that shows the state of "pq" after the code completes, assuming that an array is used internally to store the priority queue.

The code will run for some time and stop when the stack overflows

void my_recursive_function() { my_recursive_function(); } int main() { my_recursive_function(); return 0; } What will happen when the above snippet is executed? o The code will be executed successfully and no output will be generated o The code will be executed successfully and random output will be generated o The code will show a compile time error o The code will run for some time and stop when the stack overflows

O(log N)

what is the time complexity of the following code ? int a = 0, i = N;while (i > 0) {a += i;i /= 2;} O(N) O(sqrt(N)) O(N / 2) O(log N)

O(N+M)

what is the time complexity? int a=0,b=0; for (i = 0; i<; i++){ a=a+rand(); } for (j=0; j< M; j++){ b=b+rand(); }


Kaugnay na mga set ng pag-aaral

Ch. 6: The Skeletal System Axial Division

View Set

Cell Compounds and Biological Molecules

View Set

Chapter 7 (Project Cost Management)

View Set