Samsung QP Data Struct Solution
Samsung QP Data Struct Solution
[4]
[4] Question 3 Write the Recurrence equation for Merge Sort algorithm. Show your work to derive the equivalent Big-Oh notation from the recurrence. T(1) = a if n = 1 T(n) = 2T(n/2) + bn if n > 1 Repeatedly substitute the value of n by n/2 , n/4 ans so on. Finally we will have an expression in which we can put T(1). Solving that we get T(n) = 2i T(n/2i) + ibn Now, if we let i = log2 n, we end up with: n*T(1) + bn log2 n = an + bn log2 n = O(n log n) (because 2log2n = n). (NOTE: Solution of recurrence using mater theorem has also been marked correct)
Question 4 [4] Write pseudocode for a recursive, divide-and-conquer algorithm MAX(A,p,r) that returns the maximum element of the integer array A[p..r]. The initial call to the algorithm is MAX(A,1,n), where n = length(A). No global variables are allowed.
(NOTE: Other solutions have also been marked correct if found logically right)
[4] Question 5 Write a function that simulates the departure of vehicles queued at a Toll Booth. Assume vehicles are assigned an integer sequence number as soon as they reach the toll station. x = q.items[0]; for(i=0; i<q.rear; i++) q.items[i] = q.items[i+1]; q.rear -- ; (All elements have to be moved to front as happens at a Toll Plaza. Server at Front remains fixed) Question 6 [4] Write a recursive algorithm for a function named PreToPost that converts a prefix expression to its equivalent postfix expression. PreToPost( ){ read character c; if (c is operator) { PretoPost(); PretoPost(); } print c; }
Question 7 Show the rotations required to insert value 4 in the following AVL tree:
[4]
Question 8 Write a function Reverse to reverse a linked list iteratively in template given below: void Reverse(struct node** headRef) { struct node* tmp = NULL; struct node* current = *headRef; struct node* next; while (current!= NULL) { next = current link; current link = tmp; tmp = current ; current = next; } *headRef = tmp; }
[6]
Question 9 [6] Consider a hash table of size m = 12 that uses collision-resolution by open addressing and the quadratic probing hash function h(k, i) = ((k mod m) + i + i2) mod m. Show the hash table resulting from inserting the keys 10, 22, 34 and 16, in this order. The hash function for this hash table does not generate a valid probe sequence. Explain why and give a key that cannot be inserted into the table you produced in part (a) above.
[6]
(i) (ii)
Show the two B-trees resulting from the insertion of key B and then key C into the B-tree above. Show the two B-trees resulting from the deletion of key A and then key D from the original Btree above.
[6] Question 11 Write the pseudocode for finding the Minimum Spanning Tree using Prims Algorithm. Apply the same for the following undirected graph starting from the bottom most vertex. Draw the graph at each step.