Data Structures Exam Review

Ace your homework & exams now with Quizwiz!

Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. Now consider that a value 35 is inserted into this heap. After insertion, the new heap is: A. 40, 30, 20, 10, 15, 16, 17, 8, 4, 35 B. 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 C. 40, 30, 20, 10, 35, 16, 17, 8, 4, 15 D. 40, 35, 20, 10, 15, 16, 17, 8, 4, 30

B. 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 Explanation: After inserting 35, then swapping 35 with 15 and swapping 35 again with 30, we get the following.

Consider an undirected random graph of eight vertices. The probability that there is an edge between a pair of vertices is 1/2. What is the expected number of unordered cycles of length three? A. 8 B. 7 C. 1 D. 1/8

B. 7 Explanation: A cycle of length 3 can be formed with 3 vertices. There can be total 8C3 ways to pick 3 vertices from 8. The probability that there is an edge between two vertices is 1/2. So expected number of unordered cycles of length 3 = (8C3)*(1/2)^3 = 7.

What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree? A. O(log n) for search ; O(n) for delete and insert B. O(n) for all C. O(log n) for search and insert; O(n) for delete D. O(log n) for all

B. O(n) for all Explanation: In skewed Binary Search Tree (BST), all three operations can take O(n). Read more: https://www.geeksforgeeks.org/data-structures-binary-search-trees-question-1/

What is missing from the outer loop head in following insertion sort algorithm? void insertionSort(int arr[], int n) { int i, key, j; for (______________) { key = arr[i]; j = i-1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } } A. i = 1; i < n - 1; i++ B. i = 1; i < n; i++ C. i = 0; i < n; i++ D. i = 0; i < n - 1; i++

B. i = 1; i < n; i++ Explanation: Observe algorithm for Insertion Sort. https://www.geeksforgeeks.org/insertion-sort/

/* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter(Node* prev_node, int new_data) { if (prev_node == NULL) { cout << "the given previous node cannot be NULL" << endl; return; } Node* new_node =new Node; new_node->data = new_data; new_node->next = prev_node->next; ________________________ } Fill in the missing line. A. prev_node = new_node; B. prev_node->next = new_node; C. prev_node = new_node -> next; D. prev_node->next = NULL;

B. prev_node->next = new_node; Read more: http://www.geeksforgeeks.org/data-structures/linked-list/

What does the following function do? void fun(int n) { queue<int> q; q.push(0); q.push(1); for (int i = 0; i < n; i++) { int a = q.front(); q.pop(); int b = q.front(); q.pop(); q.push(b); q.push(a + b); cout << a << ", "; } } A. print out numbers 0 to n-1 reversely B. print out first n Fibonacci numbers C. print out first n Fibonacci numbers reversely D. print numbers 0 to n-1

B. print out first n Fibonacci numbers Explanation: https://www.geeksforgeeks.org/program-to-print-first-n-fibonacci-numbers/ Stacks: https://www.geeksforgeeks.org/stack-data-structure-introduction-program/ Queues: https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/

Which of the following is a true about Binary Trees? A. Every binary tree is either complete or full. B. Every full binary tree is also a complete binary tree. C. Every complete binary tree is also a full binary tree. D. No binary tree is both complete and full E. None of the above

E. None of the above Read more: http://www.geeksforgeeks.org/binary-tree-data-structure/

You are given pointers to first and last nodes of a double linked list, which of the following operations are dependent on the length of the linked list? A. remove the first element of the list B. insert in front of the list C. remove the last element of the list D. add new element at the end of the list E. None of these

E. None of these. Explanation: a) Can be done in O(1) time by deleting memory and changing the first pointer. b) Can be done in O(1) time, see push() here c) Delete the last element requires pointer to previous of last, which can only be obtained by traversing the list. d) Can be done in O(1) by changing next of last and then last.

Is the following statement valid about shortest paths? Given a graph, suppose we have calculated shortest path from a source to all other vertices. If we modify the graph such that weights of all edges is becomes double of the original weight, then the shortest path remains same only the total weight of path changes. True/False?

True. Explanation: The shortest path remains same. It is like if we change unit of distance from meter to kilo meter, the shortest paths don't change.

We use (Root, LeftSubTree, RightSubtree) notation to represent a tree. Assume that we have AVL tree (7, 5, (8, NULL, 9)), after insert new node 10, what will be the new AVL tree? Here NULL represent empty subtree. In this case, 8 has no left child. A. (7, 5, (9, 8 10)) B. (7, 5, (8, NULL, (9, NULL, 10)) C. None of these D. (8, (7, 5, NULL), (9, NULL, 10))

A. (7, 5, (9, 8 10)) Explanation: http://www.geeksforgeeks.org/avl-tree-set-1-insertion/

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? A. 15, 10, 23, 25, 20, 35, 42, 39, 30 B. 15, 20, 10, 23, 25, 42, 35, 39, 30 C. 10, 20 , 15, 23, 25, 35, 42, 39, 30 D. 15, 10, 25, 23, 20, 42, 35, 39, 30

A. 15, 10, 23, 25, 20, 35, 42, 39, 30 Explanation: The following is the constructed tree:

A binary tree T has 20 leaves. The number of nodes in T having two children is: A. 19 B. 18 C. 17 D. Could be any number between 11 and 19, inclusive

A. 19 Explanation: In a Binary tree, if there are N leaf nodes, then the number of Nodes having two children will be N-1. So in this case, the answer will be 20 - 1 = 19.

What is the output of following function for start pointing to second node of following linked list? 1->2->3->4->5->6? void fun(struct node* start) { if(start == NULL) return; cout << start->data; if(start->next != NULL ) fun(start->next->next); cout << start->data; } A. 2 4 6 6 4 2 B. 1 3 5 1 3 5 C. 1 3 5 5 3 1 D. 2 4 6 2 4 6

A. 2 4 6 6 4 2 Explanation: function iterates to the end beginning at 2, followed by 4, then 6. Then, with 6 as the new start, next == null, so the second if-statement is skipped, and start->data is returned (in this case, a second 6).

The following postfix expression with single digit operands is evaluated using a stack: 8 1 3 + / 2 3 * + 5 1 * - The top two elements of the stack after the first * is evaluated are: A. 6, 2 B. 6, 3 C. 1, 5 D. 5, 7

A. 6, 2 Explanation: The algorithm for evaluating any postfix expression is fairly straightforward: 1. While there are input tokens left o Read the next token from input. o If the token is a value + Push it onto the stack. o Otherwise, the token is an operator (operator here includes both operators, and functions). * It is known a priori that the operator takes n arguments. * If there are fewer than n values on the stack (Error) The user has not input sufficient values in the expression. * Else, Pop the top n values from the stack. * Evaluate the operator, with the values as arguments. * Push the returned results, if any, back onto the stack. 2. If there is only one value in the stack o That value is the result of the calculation. 3. If there are more values in the stack o (Error) The user input has too many values.

Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a given graph? A. Depth First Search B. Prim's Minimum Spanning Tree Algorithm C. Breadth First Search D. Kruskal' Minimum Spanning Tree Algorithm

A. Depth First Search Read more: https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/

Suppose we run Dijkstra's single source shortest-path algorithm on the following edge weighted directed graph with vertex P as the source. In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized? A. P, Q, R, S, T, U B. P, Q, T, R, U, S C. P, Q, R, U, S, T D. P, Q, R, U, T, S

A. P, Q, R, S, T, U Read more: https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/

Which of the following statement(s)is / are correct regarding Bellman-Ford shortest path algorithm? P: Always finds a negative weighted cycle, if one exists. Q: Finds whether any negative weighted cycle is reachable from the source. A. Q only B. P only C. Both P and Q D. Neither P and Q

A. Q only Explanation: Bellman-Ford shortest path algorithm is a single source shortest path algorithm. So it can only find cycles which are reachable from a given source, not any negative weight cycle. Consider a disconnected where a negative weight cycle is not reachable from the source at all. If there is a negative weight cycle, then it will appear in shortest path as the negative weight cycle will always form a shorter path when iterated through the cycle again and again.

In AVL tree insert, suppose node w has been inserted by performing standard Binary Search Tree insertion. Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z. Assume the subtree rooted at z as following (here T1, T2, T3, and T4) are subtrees. What shall you do to pre-balance the AVL tree? Tree: (z, (y (x (T1, T2), T3), T4)) A. Right Rotate z B. Left Rotate y then Right Rotate z C. Right Rotate y then Left Rotate z D. Left Rotate z

A. Right Rotate z Explanation: http://www.geeksforgeeks.org/avl-tree-set-1-insertion/

Which one of the following hash functions on integers will distribute keys most uniformly over 10 buckets numbered 0 to 9 for i ranging from 0 to 2020? A. h(i) = i^3 (mod 10) B. h(i) = i^2 (mod 10) C. h(i) = (11 * i^2) (mod 10) D. h(i) = (12 * i^2) (mod 10)

A. h(i) = i^3 (mod 10) Explanation: Since mod 10 is used, the last digit matters. If you cube all numbers from 0 to 9, you get following table. Therefore all numbers from 0 to 2020 are equally divided in 10 buckets. If we make a table for square, we don't get equal distribution. In the following table. 1, 4, 6 and 9 are repeated, so these buckets would have more entries and buckets 2, 3, 7 and 8 would be empty.

Consider the Node structure as following /* Node of a doubly linked list */ struct Node { int data; Node *next; // Pointer to next node in DLL Node *prev; // Pointer to previous node in DLL }; What will be the code for missing step in C++? // may need two lines of code /* Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the front of the list. */ void push(struct Node** head_ref, int new_data) { /* 1. allocate new node */ /* 2. put in the data into new node */ /* 3. Make next of new node as head and previous as NULL */ /* 4. ____________________________ */ /* 5. move the head to point to the new node */ } A. if(*head_ref != NULL) (*head_ref)->prev = new_node; B. (*head_ref)->prev = new_node; C. if(head_ref != NULL) head_ref->prev = new_node; D. head_ref->prev = new_node;

A. if(*head_ref != NULL) (*head_ref)->prev = new_node; Read more: https://www.geeksforgeeks.org/doubly-linked-list/

If we use binary search algorithm to search 5 4 8 9 21 15 19 20 25 28 32 35 36 48 52 for key 17, list the elements that will be compared with key in order of when the comparison happens. A. none of these answers B. 20 9 15 19 C. 1 8 15 D. cannot use binary search algorithm because the key is not in the array

A. none of these answers Explanation: None of these options are applicable because the array is not sorted numerically, which is a precondition for any binary search operation.

Consider the following C++ function: void fun(int n){ stack<int> s; while(n > 0){ s.push(n%2); n = n/2; } while(!s.empty()){ cout << s.top(); s.pop(); } } What does this function do in general? A. print the binary representation of n B. print the binary representation of n in reverse order C. print the value of square root of n in reverse order D. print the value of square root of n

A. print the binary representation of n Explanation: https://www.geeksforgeeks.org/binary-representation-of-a-given-number/

Suppose that we use merge sort algorithm to sort the following array: {3, 6, 1, 7, 2, 8, 5, 4}. What will be the array right before the last call of merge? A. {1, 3, 6, 7, 2, 4, 5, 8} B. {3, 6, 1, 7, 2, 8, 4, 5} C. {2, 4, 5, 8, 1, 3, 6, 7} D. {1, 2, 3, 4, 5, 6, 7, 8}

A. {1, 3, 6, 7, 2, 4, 5, 8} Explanation: First, array is split into two, then both halves are sorted before the final merge is called to sort the two halves together. Read more: https://www.geeksforgeeks.org/merge-sort/

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? A. 0 2 4 3 1 6 5 9 8 7 B. 0 1 2 3 4 5 6 7 8 9 C. 9 8 6 4 2 3 0 1 5 7 D. 7 4 1 0 3 2 4 6 8 9

B. 0 1 2 3 4 5 6 7 8 9 Explanation: In-order traversal of a BST gives elements in increasing order. So answer c is correct without any doubt.

Consider the following C++ code: struct node { int data; struct node * left, *right; }; void print(node * root, int k, int &count) { if (root != NULL && count <= k) { print(root->right, k, count); count++; if (count == k) cout << root->data; print(root->left, k, count); } } what is output of print(root, 3, count); where int count = 0; and root represent the following tree? (15 (10 (8, 12), 20 (16, 25))) A. 10 B. 16 C. 20 D. 10 20

B. 16 Explanation: The code mainly finds out k'th largest element in BST, see K'th Largest Element in BST for details: https://www.geeksforgeeks.org/kth-largest-element-in-bst-when-modification-to-bst-is-not-allowed/

Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder A. all of three B. 2 and 3 C. 1 and 3 D. 1 and 2

B. 2 and 3 Explanation: When we know either preorder or postorder traversal, we can construct the BST. Note that we can always sort the given traversal and get the inorder traversal. Inorder traversal of BST is always sorted.

If we use binary search algorithm to search 1 4 8 9 11 15 19 20 25 28 32 35 36 48 52 for key 17, list the elements that will be compared with key in order of when the comparison happens. A. 20 9 15 17 B. 20 9 15 19 C. none of these D. 20 35 28 15

B. 20 9 15 19 Explanation: Binary search begins by finding the middle element. If it's larger than the search key, all elements to its left become the new set to be searched. Binary search begins by locating the 20 at the middle of this array, then looks to the left. The new middle between 1 and 19 is the element 9. Since 17 > 9, the new set to be searched is narrowed to 11 to 19. In the middle is the element 15. The last subset is only made up of [15, 19] so 19 is the last element compared before the search for 17 is terminated.

A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below. How many different insertion sequences of the key values will result in the hash table shown above? A. 10 B. 30 C. 40 D. 20

B. 30 Explanation: In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and 33, and 46 must appear before 33.Total number of different sequences = 3! x 5 = 30In the above expression, 3! is for elements 42, 23 and 34 as they can appear in any order, and 5 is for element 46 as it can appear at 5 different places.

Consider the Node structure as following /* Node of a doubly linked list */ struct Node { int data; Node *next; // Pointer to next node in DLL Node *prev; // Pointer to previous node in DLL }; What will be the code for step 2 in C++? /* Given a node as prev_node, insert a new node after the given node */ void insertAfter(struct Node* prev_node, int new_data) { /*1. check if the given prev_node is NULL */ /* 2. allocate new node */ /* 3. put in the data to the new node */ /* 4. Make next of new node as next of prev_node */ /* 5. Make the next of prev_node as new_node */ /* 6. Make prev_node as previous of new_node */ /* 7. Change previous of new_node's next node */ } A. Node new_node; B. Node *new_node = new Node; C. Node new_node = new Node; D. Node *new_node;

B. Node *new_node = new Node; Read more: https://www.geeksforgeeks.org/doubly-linked-list/

Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time on Depth First Search of G? Assume that the graph is represented using adjacency matrix. A. O(n) B. O(n^2) C. O(m+n) D. O(mn)

B. O(n^2) Explanation: Depth First Search of a graph takes O(m+n) time when the graph is represented using adjacency list. In adjacency matrix representation, graph is represented as an "n x n" matrix. To do DFS, for every vertex, we traverse the row corresponding to that vertex to find all adjacent vertices (In adjacency list representation we traverse only the adjacent vertices of the vertex). Therefore time complexity becomes O(n2)

Let G be a weighted undirected graph and e be an edge with maximum weight in G. Suppose there is a minimum weight spanning tree in G containing the edge e. Which of the following statements is always TRUE? A. Edge e cannot be contained in a cycle. B. There exists a cutset in G having all edges of maximum weight. C. All edges in G have the same weight D. There exists a cycle in G having all edges of maximum weight

B. There exists a cutset in G having all edges of maximum weight. Explanation: Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. 1. A spanning tree has exactly V - 1 edges. 2. A single graph can have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree. 3. There can be more that one possible spanning trees of a graph. For example, the graph in this question has 6 possible spanning trees. 4. MST has lightest edge of every cutset. Remember Prim's algorithm which basically picks the lightest edge from every cutset.

Which of the following is not a stable sort? A. selection sort B. quick sort C. merge sort D. Insertion sort

B. quick sort Explanation: Since different elements can be used as pivots, the time it takes to quicksort is highly variable. The worst case occurs when the partition process always picks greatest or smallest element as pivot, O(n^2). The best case occurs when the partition process always picks the middle element as pivot, O(nLogn). Because of this, the default implementation is not stable. However any sorting algorithm can be made stable by considering indexes as comparison parameter. More: https://www.geeksforgeeks.org/quick-sort/

Which of the following statement(s) is TRUE? 1. A hash function takes a message of arbitrary length and generates a fixed length code. 2. A hash function takes a message of fixed length and generates a code of variable length. 3. A hash function may give the same hash value for distinct messages. A. 1 only B. 2 only C. 1 and 3 only D. 2 and 3 only

C. 1 and 3 only Explanation: Hash function is defined as any function that can be used to map data of arbitrary size of data to a fixed size data. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes: Statement 1 is correct. Yes, it is possible that a Hash Function maps a value to a same location in the memmory that's why collision occurs and we have different technique to handle this problem: Statement 3 is correct. eg: we have hash function, h(x) = x mod 3 Acc to Statement 1, no matter what the value of 'x' is h(x) results in a fixed mapping location.Acc. to Statement 3, h(x) can result in same mapping location for different value of 'x' e.g. if x = 4 or x = 7 , h(x) = 1 in both the cases, although collision occurs.

Given array {3, 7, 6, 5, 2, 1, 8, 4}, use quick sort algorithm to sort the array. what will be the return value from the first call of partition? Assume the last element is used as pivot for partition and array index starts from 0. A. 4 B. 5 C. 3 D. 6

C. 3 Explanation: Since the last element being used as the partition, 4 is the first pivot value, so everything < 4 is separated into the left partition, and everything > 4 into the right partition. The next pivot value selected will be the last element of the left partition, which in this case is 3. More on Quicksort: https://www.geeksforgeeks.org/quick-sort/

Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex t at a distance four from the root. If t is the n-th vertex in this BFS traversal, then the maximum possible value of n is: A. 15 B. 16 C. 31 D. 32

C. 31 Explanation: It would be node number 31 for given distance 4. Since t is the n-th vertex in this BFS traversal at distance four from the root, the height of tree is 4. Max number of nodes = 2^{height+1} − 1, so 2^{5} − 1 = 31.

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property. Which one of the following is a valid sequence of elements in an array representing 3-ary max heap? A. 9, 3, 6, 8, 5, 1 B. 1, 3, 5, 6, 8, 9 C. 9, 5, 6, 8, 3, 1 D. 9, 6, 3, 1, 8, 5

C. 9, 5, 6, 8, 3, 1 Explanation: See image.

Which of the following statements is/are TRUE for an undirected graph? P: Number of odd degree vertices is even Q: Sum of degrees of all vertices is even A. P only B. Q only C. Both P and Q D. Neither P or Q

C. Both P and Q Explanation: Q is true since the graph is undirected, every edge increases the sum of degrees by 2. P is true because if we consider sum of degrees and subtract all even degrees, we get an even number (because Q is true). So total number of odd degree vertices must be even.

What is the run time of binary search? A. O(n^2) B. O(1) C. O(ln n) D. O(n)

C. O(ln n) Explanation: https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/running-time-of-binary-search Side-note: This is another way of writing O(logn), because big O notation isn't affected by logarithmic base.

What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array. Build Heap is used in Heap Sort as a first step for sorting. A. O(n^2) B. O(log n) C. O(n) D. O(n log n)

C. O(n) Explanation: Following is algorithm for building a Heap of an input array A. BUILD-HEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for END Although the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity.

Which of the following is True about AVL tree? A. The worst time for insert is O(n) B. The worst time for delete is O(n) C. The worst time for insert and delete are both O(log n) D. The worst time for insert and delete are both O(n)

C. The worst time for insert and delete are both O(log n) Explanation: The rotation operations (left and right rotate) take constant time as only a few pointers are being changed there. Updating the height and getting the balance factor also takes constant time. So the time complexity of AVL insert remains same as BST insert which is O(h) where h is the height of the tree. Since AVL tree is balanced, the height is O(Logn). So time complexity of AVL insert is O(Logn).

You are given pointers to first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list? A. remove the first element of the list B. insert in front of the list C. remove the last element of the list D. add new element at the end of the list

C. remove the last element of the list Explanation: a) Can be done in O(1) time by deleting memory and changing the first pointer. b) Can be done in O(1) time, see push() here c) Delete the last element requires pointer to previous of last, which can only be obtained by traversing the list. d) Can be done in O(1) by changing next of last and then last.

Apply the following insertion sort to array 4 1 2 5 3. Right after i is increased to 2, before the next iteration of loop body, what is the array? void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i-1; while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } } A. 4 1 2 5 3 B. none of these C. 1 2 4 5 3 D. 1 4 2 5 3

D. 1 4 2 5 3 Read more: https://www.geeksforgeeks.org/insertion-sort/

A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys. I. 81, 537, 102, 439, 285, 376, 305 II. 52, 97, 121, 195, 242, 381, 472 III. 142, 248, 520, 386, 345, 270, 307 IV. 550, 149, 507, 395, 463, 402, 270 Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search? A. II and III B. I and III C. III and V D. III only

D. III only Explanation: Key to be searched 273: I) 81, 537, 102, 439, 285, 376, 305 is not correctWe cannot go to 376 from 285 as 273 is smaller than 285. II) 52, 97, 121, 195, 242, 381, 472 is not correct.We cannot go to 472 from 381 as 273 is smaller than 381. III) 142, 248, 520, 386, 345, 270, 307 is correct 550, 149, 507, 395, 463, 402, 270 is not correct. We cannot go to 463 from 395 in search of 273

In a depth-first traversal of a graph G with n vertices, k edges are marked as tree edges. The number of connected components in G is: A. n-k+1 B. k+1 C. k D. n-k

D. n-k Explanation: Tree edges are the edges that are part of DFS tree. If there are x tree edges in a tree, then x+1 vertices in the tree. The output of DFS is a forest if the graph is disconnected. Let us see below simple example where graph is disconnected.

Use the following selection sort to sort int array {2, 5, 3, 6, 4, 1}, Right after i is increased to 3, before the next outer loop, what is the array? void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element swap(&arr[min_idx], &arr[i]); } } A. {2, 5, 3, 6, 4, 1} B. {1, 5, 3, 6, 4, 1} C. {1, 2, 3, 4, 6, 5} D. {1, 2, 3, 6, 4, 5}

D. {1, 2, 3, 6, 4, 5} Explanation: Selection sort swaps values as the algorithm iterates to each index. Once i is increased to 3, the following swaps would have already taken place: swap 2 with 1 --> [1, 5, 3, 6, 4, 2] swap 5 with 2 --> [1, 2, 3, 6, 4, 5] Once you arrive at the third element (i = 3), but before the next swap, this is the answer.


Related study sets

Real Estate Principles (Chpt. 1 Quiz)

View Set

DM 150 PMBOK 7 Scenario-Based PMP Exam Questions and Answers (2022)

View Set

ARTH103 Chapter 17 QUIZ: Japan before 1333

View Set

Basic Insurance concepts and principles

View Set

Chapter 48: Musculoskeletal or Articular Dysfunction NCLEX

View Set