Open In App

Data Structures and Algorithms | Set 37

Last Updated : 21 Jan, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Que – 1. For 8 keys and 6 slots in a hashing table with uniform hashing and chaining, what is the expected number of items that hash to a particular location. 
(A) 2.33 
(B) 0.75 
(C) 1.33 
(D) 2 

Solution: 
Probability that key1 ends up in slot 1 = 1/6 
Probability that key2 ends up in slot 1 = 1/6 
Probability that key3 ends up in slot x = 1/6 
Probability that key4 ends up in slot x = 1/6 
Probability that key5 ends up in slot x = 1/6 
Probability that key6 ends up in slot x = 1/6 
Expected number of items that hash to a particular location = 8/6 
Option (C) is correct. 

Que – 2. For n keys and m slots in a hashing table, which of the following is the expected number of empty location. 
(A) n((m-1)/m)^n 
(B) m((m-1)/m)^n 
(C) n((n-1)/m)^n 
(D) n((n-1)/n)^m 

Solution: 
Expected number of items that hash to a particular location = n/m 
Probability that slot 1 is empty after n keys are inserted = ((m-1)/m)^n 
Probability that slot 2 is empty after n keys are inserted = ((m-1)/m)^n 
Probability that slot 3 is empty after n keys are inserted = ((m-1)/m)^n 
So for m slots expected number of empty locations = m((m-1)/m)^n 
Option (B) is correct. 

Que – 3. What is the number of binary search trees with 20 nodes with elements 1, 2, 3,…..20 such that the root of tree is 12 and the root of left subtree is 7? 
(A) 2634240 
(B) 1243561 
(C) 350016 
(D) 2642640 

Solution: 
Number of nodes in left subtree = 11 {1, 2, 3, 4….11} 
Number of nodes in the right subtree = 8 {13, 14, ….20} 
Since for the left subtree root is 7 
Number of elements in the left part of left subtree = 6 {1, 2, 3..6} 
Number of elements in the right part of left subtree = 4 {8, 9, 10, 11} 
We know number of Binary Search trees with n nodes = (C(2n,n)/n+1) 
Number of BST with 6 nodes = (C(12,6)/7) = 132 
Number of BST with 4 nodes = (C(8,4)/5) = 14 
Number of BST with 8 nodes = (C(16,8)/9) =1430 
Total number of BST = 2642640 
Option (D) is correct. 

Que – 4. For a graph with E edges and V vertices what is the time complexity of Dijkstra algorithm using array as data structure for storing non-finalized vertices. Graph is undirected and represented as adjacency list? 
(A) O(VE) 
(B) O(ElogV) 
(C) O(V^2 ) 
(D) O(E^2log V) 

Solution: 
Given data structure used is array so initialization of all nodes in array (setting infinity to all nodes which are not reachable), this operation takes O(V) 
At each step we have to delete minimum from the array. For one such operation it takes O(V), using selection sort first pass. We have V such steps. So total complexity due to deletion of minimum element at each step = V*O(V) = O(V^2) 
After a minimum is selected at each step we have to check its adjacent and perform decrease key to the neighbors of selected node. It takes total O(2E) to check adjacents for all steps. Also the decrease key of all the steps is E. O(1) = O(E) . Since the array is unsorted we don’t have to sort the array at each step. 
Total complexity = O(V) + O(V^2) + O(2E) + O(E) = O(V^2) 
Note: For a simple graph V^2 is always >= E 
Option (C) is correct. 

Que – 5. What is the output of the following program? 
 

int a = 5;
main()
{
   extern int a;
   extern int a;
   printf(a);
}

(A) Compile time error. 
(B) Run time error. 
(C) 0 
(D) 5 

Solution: 
Above program does not cause any compile time error since extern does not allocate any memory. So within a function declaration of same variable as extern does not cause any error as long as the variable is declared globally. Output is 5. 
Option (D) is correct. 

 


Previous Article
Next Article

Similar Reads

Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms?
In today's world, data scientists and machine learning engineers play a crucial role in analyzing data and building intelligent systems. As technology continues to advance, the demand for these experts is growing rapidly. Real-world data problems are complex, requiring strong skills in handling data and creating efficient algorithms. In this articl
10 min read
Data Structures and Algorithms | Set 3
Following questions have asked in GATE CS exam. 1. Suppose you are given an array s[1...n] and a procedure reverse (s,i,j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 < k <= n: reverse (s, 1, k); reverse (s, k + 1, n); reverse (s, 1, n); (GATE CS 2000) (a)
4 min read
Data Structures and Algorithms | Set 2
Following questions have been asked in GATE CS exam. 1. Consider the function f defined below. struct item { int data; struct item * next; }; int f(struct item *p) { return ( (p == NULL) || (p->next == NULL) || (( P->data &lt;= p->next->data) &amp;&amp; f(p->next)) ); } For a given linked list p, the function f returns 1
5 min read
Data Structures and Algorithms | Set 4
Following questions have been asked in GATE CS exam. 1. Consider the following C program segment C/C++ Code struct CellNode { struct CelINode *leftchild; int element; struct CelINode *rightChild; } int Dosomething(struct CelINode *ptr) { int value = 0; if (ptr != NULL) { if (ptr->leftChild != NULL) value = 1 + DoSomething(ptr->leftChild); if
3 min read
Data Structures and Algorithms | Set 5
Following questions have been asked in GATE CS exam. 1. Consider the following C function. float f(float x, int y) { float p, s; int i; for (s=1, p=1, i=1; i < y; i ++) { p*= x/i; s+=p; } return s; } For large values of y, the return value of the function f best approximates (GATE CS 2003) a) x^y b) e^x c) ln(1 + x) d) x^x Answer (b) The functio
2 min read
Data Structures and Algorithms | Set 6
Following questions have been asked in GATE CS exam. 1. The usual Θ(n^2) implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will (GATE
3 min read
Data Structures and Algorithms | Set 7
Following questions have been asked in GATE CS 2006 exam. 1. In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006) (A) 0(n) (B) O(logn) (C) 0(loglogn) (D) 0(1) Answer (A) In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf nodes for the minimum value. W
3 min read
Data Structures and Algorithms | Set 8
Following questions have been asked in GATE CS exam. 1. Consider the following functions Which of the following is true? (GATE CS 2000) (a) h(n) is 0(f(n)) (b) h(n) is 0(g(n)) (c) g(n) is not 0(f(n)) (d) f(n) is 0(g(n)) Answer (d) g(n) = 2√n Log n = n√n f(n) and g(n) are of same asymptotic order and following statements are true. f(n) =
3 min read
Data Structures and Algorithms | Set 9
Follow questions have been asked in GATE CS exam. 1 In a heap with n elements with the smallest element at the root, the 7th smallest element can be found in time (GATE CS 2003) a) Θ(n log n) b) Θ(n) c) Θ(log n) d) Θ(1) Answer(d) The 7th smallest element must be in first 7 levels. Total number of nodes in any Binary Heap in
2 min read
Data Structures and Algorithms | Set 10
Following questions have been asked in GATE CS 2007 exam. 1. The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is: (A) 2^h -1 (B) 2^(h-1) - 1 (C) 2^(h+1) -1 (D) 2*(h+1) Answer (C) Maximum number of nodes will be there for a complete tree. Number of nodes in
4 min read
Data Structures and Algorithms | Set 11
Following questions have been asked in GATE CS 2007 exam. 1. Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that '_' den
4 min read
Data Structures and Algorithms | Set 12
Following questions have been asked in GATE CS 2007 exam. 1. Consider the following C program segment where CellNode represents a node in a binary tree: C/C++ Code struct CellNode { struct CellNOde *leftChild; int element; struct CellNode *rightChild; }; int GetValue(struct CellNode *ptr) { int value = 0; if (ptr != NULL) { if ((ptr->leftChild =
3 min read
Data Structures and Algorithms | Set 13
Following questions have been asked in GATE CS 2002 exam 1. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is: a) n/2 b) (n-1)/3 c) (n-1)/2 d) (2n+1)/3 Answer(d) Let L be the number of leaf nodes and I be the number of internal nodes, then following relation holds for above given tree (For details, pleas
3 min read
Data Structures and Algorithms | Set 14
Following questions have been asked in GATE CS 2008 exam. 1. We have a binary heap on n elements and wish to insert n more elements (not necessarily one after another) into this heap. The total time required for this is (A) Θ(logn) (B) Θ(n) (C) Θ(nlogn) (D) Θ(n2) The worst case time complexity for insertion in a binary heap
2 min read
Data Structures and Algorithms | Set 15
Following questions have been asked in GATE CS 2008 exam. 1. The most efficient algorithm for finding the number of connected components in an undirected graph on n vertices and m edges has time complexity. (A) Θ(n) (B) Θ(m) (C) Θ(m + n) (D) Θ(mn) Answer (C) Connected components can be found in O(m + n) using Tarjan's algori
3 min read
Data Structures and Algorithms | Set 17
Following questions have been asked in GATE CS 2006 exam. 1. An implementation of a queue Q, using two stacks S1 and S2, is given below: void insert(Q, x) { push (S1, x); } void delete(Q){ if(stack-empty(S2)) then if(stack-empty(S1)) then { print(“Q is empty”); return; } else while (!(stack-empty(S1))){ x=pop(S1); push(S2,x); } x=pop(S2); } Let n i
2 min read
Data Structures and Algorithms | Set 22
Following questions have been asked in GATE CS 2005 exam. 1) A program P reads in 500 integers in the range [0..100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies? (a) An array of 50 numbers (b) An array of 100 numbers (c) An array of 500 numbe
4 min read
Data Structures and Algorithms | Set 16
Following questions have been asked in GATE CS 2009 exam. 1. Consider a binary max-heap implemented using an array. Which one of the following array represents a binary max-heap? (A) 25,12,16,13,10,8,14 (B) 25,14,13,16,10,8,12 (C) 25,14,16,13,10,8,12 (D) 25,14,12,13,10,8,16 Answer (C) A tree is max-heap if data at every node in the tree is greater
3 min read
Data Structures and Algorithms | Set 18
Following questions have been asked in GATE CS 2006 exam. 1. Consider the polynomial p(x) = a0 + a1x + a2x^2 +a3x^3, where ai != 0, for all i. The minimum number of multiplications needed to evaluate p on an input x is: (A) 3 (B) 4 (C) 6 (D) 9 Answer (A) Multiplications can be minimized using following order for evaluation of the given expression.
3 min read
Data Structures and Algorithms | Set 19
Following questions have been asked in GATE CS 2009 exam. 1. Let X be a problem that belongs to the class NP. Then which one of the following is TRUE? (A) There is no polynomial time algorithm for X. (B) If X can be solved deterministically in polynomial time, then P = NP. (C) If X is NP-hard, then it is NP-complete. (D) X may be undecidable. Answe
4 min read
Data Structures and Algorithms | Set 20
Following questions have asked in GATE CS 2006 exam. 1. Let S be an NP-complete problem and Q and R be two other problems not known to be in NP. Q is polynomial time reducible to S and S is polynomial-time reducible to R. Which one of the following statements is true? (A) R is NP-complete (B) R is NP-hard (C) Q is NP-complete (D) Q is NP-hard Answe
3 min read
Data Structures and Algorithms | Set 21
Following questions have been asked in GATE CS 2008 exam. 1. The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1 ,a2 ,a3 ,...,an} and positive integer W, is there a subset of S whose elements sum to W? A dynamic program for solving this problem uses a 2-dimensional Boolean array X, with n rows and W+1 columns.
3 min read
Data Structures and Algorithms | Set 23
Following questions have been asked in GATE CS 2005 exam. 1. Which one of the following is a key factor for preferring B-trees to binary search trees for indexing database relations? (a) Database relations have a large number of records (b) Database relations are sorted on the primary key (c) B-trees require less memory than binary search trees (d)
3 min read
Data Structures and Algorithms | Set 24
Following questions have been asked in GATE CS 2010 exam. 1. The following C function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank. typedef struct node { int value; struct node *next; }Node; Node *move_to_fron
3 min read
Data Structures and Algorithms | Set 25
Following questions have been asked in GATE 2010 exam. 1 Consider a complete undirected graph with vertex set {0, 1, 2, 3, 4}. Entry Wij in the matrix W below is the weight of the edge {i, j}. What is the minimum possible weight of a spanning tree T in this graph such that vertex 0 is a leaf node in the tree T? (A) 7 (B) 8 (C) 9 (D) 10 Answer (D) T
3 min read
Data Structures and Algorithms | Set 26
Following questions have been asked in GATE 2011 exam. 1) A max-heap is a heap where the value of each parent is greater than or equal to the values of its children. Which of the following is a max-heap? Answer: (B) A binary tree is max-heap if it is a complete binary tree (A complete binary tree is a binary tree in which every level, except possib
3 min read
Data Structures and Algorithms | Set 27
Following questions have been asked in GATE CS 2011 exam. 1) An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,....vn. Two nodes vi , vj are connected if and only if 0 < |i - j| <= 2. Each edge (vi, vj ) is assigned a weight i + j. A sample graph with n = 4 is shown below. What will be the cost of the minimum spanning t
3 min read
Data Structures and Algorithms | Set 28
Following questions have been asked in GATE 2012 exam. 1) Let w(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n. which of the following is ALWAYS TRUE? (A) A(n) = Ω(W(n)) (B) A(n) = Θ(W(n)) (C) A(n) = O(W(n)) (D) A(n) = o(W(n)) Answer (C) The worst case time complexity is
3 min read
Data Structures and Algorithms | Set 29
Following questions have been asked in GATE 2012 exam. 1) The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n discs is (A) T(n) = 2T(n - 2) + 2 (B) T(n) = 2T(n - 1) + n (C) T(n) = 2T(n/2) + 1 (D) T(n) = 2T(n - 1) + 1 Answer (D) Following are the steps to follow to solve Tower of Hanoi problem recursively. Let the
2 min read
Data Structures and Algorithms | Set 31
Following questions have been asked in GATE CS 2013 exam. 1) What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f(int &x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; } (A) 30
5 min read
Practice Tags :
three90RightbarBannerImg