Data Structures Final Study Set

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

Given an n-element 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?

O(n2)

Consider the following recursive function: 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) ?

13

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

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(nlogn)

If G is a simple undirected graph with 12 vertices and 3 connected components,what is the largest number of edges it might have?

45

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

The number of operations executed by algorithms A and B is 8nlogn and 2n2, respectively. Algorithm A is faster than algorithm B, for any possible input size.

False

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

False

A stack data structure can be implemented using an array or a linked list.

true

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.

true

In a binary tree with two nodes, the root is not a leaf node.

true

The Depth First Search traversal of a graph results into a Tree.

true

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

To be a heap data structure, one of the conditions is that the binary tree must be a complete tree

true

Any iterative procedure can be converted into a recursive procedure.

False

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

O(log N)

In a binary min-heap containing n numbers, the largest element can be found in time ...

O(n)

What is the average case time complexity for finding the height of a binary tree?

O(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 _____ ) operation, and dequeue is ______ ) operation.

log n log n

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?

0 1 2 3 4 5 6 7 8 9

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?

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

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).

Insertion Sort

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(2^n)

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(2^n); O(n)

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 ?

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

A recursive definition can have one or more base cases.

True

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

True

The depths of any two leaves in a max heap differ by at most 1.

True

You are given the following code snippet: 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

[12, 17, 29, 56, 18]

Which of the following is/are TRUE of mergesort and quicksort?

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

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

0(1)

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?

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

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, 20, 10, 10, 20

Fill-in the blank nodes of the following binary max-heap with valid integer values. The value of the left blank node appears before the value of the right blank node.

4, 11(refer to midterm)

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?

46, 34, 42, 23, 52, 33 ( refer to HW 5)

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

Which of the following is an advantage of adjacency list representation over adjacency matrix representation of a graph?

Adjacency list representation uses less space when the graph is sparse. 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 is V and E are number of vertices and edges respectively. Adding a vertex in adjacency list representation is easier than adjacency matrix representation.

Which of the following scenarios leads to linear running time for a random search hit in a linear-probing hash table?

All keys hash to same index

Which of the following is a max-heap ?

B(refer to homework 4)

For the tree shown in the picture, mark which of the tree properties are true.

Binary Tree(refer to midterm)

Which of the following operations is not O(1) for an array of sorted data. You may assume that array elements are distinct.

Delete an Element Insert a new Element

A binary tree is said to be a heap data structure if it is a complete binary tree.

False

When the runtime of an 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.

False

In an array-based implementation of a doubly linked list, identify which are correct statements.

Implementation of enqueue operation has time complexity O(1). Implementation of dequeue operation has time complexity O(1). Implementation of "first" operation to check the value of the first element has time complexity O(1). Implementation of "size" operation has time complexity O(1)

Which of the following are correct statement(s) about array and linked list ?

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

What is common in three different types of tree traversals (Inorder, Preorder and Postorder)?

Left subtree is always visited before right subtree

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

Merge Sort

Suppose you have a bunch of computers networked together (haphazardly) using wires. You want to send a message to every other computer as fast as possible. Unfortunately, some wires are being monitored by some shadowy organization that wants to intercept your messages. After doing some reconnaissance, you were able to assign each wire a "risk factor" indicating the likelihood that the wire is being monitored. For example, if a wire has a risk factor of zero, it is extremely unlikely to be monitored; if a wire has a risk factor of 10, it is more likely to be monitored. The smallest possible risk factor is 0; there is no largest possible risk factor. You must implement a solution that selects wires to send your message such that (a) every computer receives the message and (b) you minimize the total risk factor. The total risk factor is defined as the sum of the risks of all of the wires you use. Which of the following algorithms would be most suitable ?

Minimum Spanning Tree

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

O(N * N)

What is the time complexity of the following code ? int a = 0, b = 0;for (i = 0; i < N; i++) {a = a + rand();}for (j = 0; j < M; j++) {b = b + rand();}

O(N + M)

What is the best-case time complexity of bubble sort? For example, if the input is already sorted as desired.

O(n)

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(n2)

In an unweighted, undirected connected graph, the shortest path from a node S to every other node is computed most efficiently, in terms of time complexity, by

Performing a BFS starting from S.

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); }

Prints all nodes of linked list in reverse order

Which one of the following in place sorting algorithms needs the minimum number of swaps?

Selection sort

For the Abstract Data Type "List", which of the following implementations are possible ?

Singly Linked List Doubly Linked List Array ArrayList

Recursion is a method in which the solution of a problem depends on <_______>.

Smaller instances of the same problem

Consider the following code snippet: void my_recursive_function() { my_recursive_function(); } int main() { my_recursive_function(); return 0; } What will happen when the above snippet is executed?

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

The best case, in terms of the asymptotic running time, for QuickSort happens when:

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 running time of a program is expressed as, f(n) = 4n^3 + 3n^2 + 100. Which of the following is a correct statement?

The running time is O(n^3)

In a min-heap, parent node can have value equal to one of its children.

True

The space complexity of the recursive code to compute sum of a list is O(n).

True

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.

True

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

X will always be a better choice for large inputs

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

depth

Binary recursion is an example of tail recursion.

false

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

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

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.

false

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

false

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.

false

In the implementation of linked list data structure, the "head" variable is an instance of the "Node" class.

false

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

false

Which answer is a correct skeleton for a recursive Java method?

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 } }


Ensembles d'études connexes

JN 150 Semicolons, Colons, Dashes & Hyphens Quiz

View Set

Ch. 10 Promoting Healthy Pregnancy

View Set

Chapter 32: The Child with a Chronic Health Problem

View Set

Chapter 27: Safety, Security, and Emergency Preparedness

View Set

Define Computer Science , Algorithms and Information Technology

View Set

Pénzügyi jog 1 - Adóeljárás

View Set

Business Final 2022 (Quiz 1-7) (Objective Exam 1-3)

View Set