Data Structures and Algorithms | Set 37
Last Updated :
21 Jan, 2021
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.
Similar Reads
DSA Guide for GATE CS Exam | Notes, Syllabus, Preparation Strategy
The GATE (Graduate Aptitude Test in Engineering) Exam is a critical milestone for computer science enthusiasts seeking advanced education or career opportunities. A solid understanding of Data Structures and Algorithms (DSA) is indispensable for success in this exam, as it forms the core of computer
9 min read
Asymptotic Analysis of Algorithms Notes for GATE Exam [2024]
This Asymptotic Analysis of Algorithms is a critical topic for the GATE (Graduate Aptitude Test in Engineering) exam, especially for candidates in computer science and related fields. This set of notes provides an in-depth understanding of how algorithms behave as input sizes grow and is fundamental
15 min read
Recurrence Relations Notes for GATE Exam [2024]
Recurrence relations are the mathematical backbone of algorithmic analysis, providing a systematic way to express the time complexity of recursive algorithms. As GATE Exam 2024 approaches, a profound understanding of recurrence relations becomes imperative for tackling questions that demand a deep c
13 min read
Array Notes for GATE Exam [2024]
Arrays are fundamental data structures in computer science, and mastering them is crucial for success in the GATE exam. This article aims to provide concise yet comprehensive notes on arrays, covering essential concepts and strategies to help you tackle array-related questions in the GATE 2024 exam.
15+ min read
Linked List Notes for GATE Exam [2024]
The "Linked List Notes for GATE Exam" is a comprehensive resource designed to aid students in preparing for the Graduate Aptitude Test in Engineering (GATE). Focused specifically on linked lists, a fundamental data structure in computer science, these notes offer a detailed and structured approach t
15+ min read
Queue Notes for GATE Exam [2024]
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. Queue is a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.  The element, which is first
15+ min read
Stack Notes for GATE Exam [2024]
Stacks, a fundamental data structure in computer science, are crucial for understanding algorithmic paradigms and solving complex computational problems. As candidates gear up for the GATE Exam 2024, a solid grasp of stack concepts is indispensable. These notes are designed to provide a concise yet
14 min read
Hashing Notes for GATE Exam [2024]
Hashing is a fundamental concept in computer science and plays a pivotal role in various algorithms and data structures. Aspiring candidates preparing for the GATE Exam 2024 must grasp the intricacies of hashing to tackle complex problem-solving scenarios efficiently. These notes aim to provide a co
15+ min read
Trees Notes for GATE Exam [2024]
Trees are foundational structures in computer science, serving as the backbone for numerous algorithms and data representations. GATE aspirants should be well versed in tree structures to prepare for the GATE Exam in 2024. This article aims to provide a concise yet comprehensive overview of trees, e
15 min read
Graph Data Structure Notes for GATE Exam [2024]
Graphs, a fundamental concept in computer science and mathematics, serve as powerful tools for modeling and solving a myriad of real-world problems. As aspirants gear up for the GATE Exam 2024, a comprehensive understanding of graph data structures becomes paramount. These notes aim to provide a con
15+ min read