Viva Questions For Data Structures Lab
Viva Questions For Data Structures Lab
Viva Questions For Data Structures Lab
Ans) A Data Structure is a data object together with the relationships that exists among
the instances & among the individual elements that compose an instance.
1. Linear Data Structures: A data structure is said to be linear if the elements form a
sequence. It is sequential and continues in nature i.e. access the data in sequential
manner.
In linear data structure we can not insert an item in middle place and it maintains a linear
relationship between its elements
egs: Array, Linked list, Stack, Queue, Dequeue etc.
2. Non Linear Data Structures: A data structure is said to be non-linear if elements do not
form a sequence. (Not sequential).
It does not maintain any linear relationship between their elements. Every data item is attached to
several other data items in a way that is specific for reflecting relationships. The data items are
not arranged in a sequential structure.
[A data structure is linear if every item is related with next and previous item and it is non
linear if it is attach with many of the items in specific ways to reflect relationship.]
3Q) What is a Singly Linked List?
Ans) Singly Linked List is a Sequence of dynamically allocated Storage elements, each
element of which contains a pointer to its successor. A pointer to the first element of the
list is called as head and a pointer to the last element of the list is called as tail used to
keep track of the list elements.
Ans) In Doubly Linked List each element contains two pointers: One Pointer points to its
successor and another to its predecessor (previous element).It is also called as two way
linked list (traversing can be done in both directions).
Ans
Array Linked List
)
1.Size of the array is fixed 1.Size of the linked list is not fixed
2. Memory is allocated Statically (or) 2. Memory is allocated dynamically (at
Dynamically (at run time). runtime).
(If the memory is allocated for an array
Statically(at compile time) it is called Static
Array and if memory is allocated at run
time (dynamically)using operator new it is
called Dynamic Array)
3.Memory wastage will be there if all the 3.Memory is not wasted as only Required
array positions are not utilized memory is allocated
Ans) Stack is an ordered collection of items into which items can be inserted and deleted from
only one end called as “Top” of the Stack. It is also called as LIFO list.(Last In First
Out).
Ans) Is Stack is empty and POP operation is performed it is not possible to delete the items.
This situation is called Stack Underflow.
Ans) If Stack is full and PUSH operation is performed it is not possible to insert or Push
the new items into the stack. This situation is called Stack Overflow.
Ans) Postfix Expressions are easy to evaluate as postfix expressions does not make use
of operator precedence not does it require the use of parenthesis.
Ans) It is an ordered collection of items into which items can be inserted from one end called
as REAR end and items are deleted from other end called as FRONT end of the Queue.
It is also called as FIRST IN FIRST OUT (FIFO) LIST).
Ans) In Circular Queue, the first position of the array is kept behind the last position of the
array.
Ans) In Linear Queue once the queue is full and the deletion is performed, even if first position
is free(vacant) it is not possible to insert the item in that position whereas in Circular
Queue it is possible since the first position is kept behind the last position.
Ans) In Double Ended Queue insertion and deletion are possible from both the ends.
TREES:
16Q) What is a Tree?
Ans) Tree is a finite non-empty set of nodes with the following properties:
Degree of a node: The number of sub trees attached to a node is called degree of that node and
the maximum degree of any node in a tree is called degree of that tree.
Nodes that have degree zero are called Leaf or Terminal Nodes. Consequently, the other nodes
are referred to as Non-Terminals.
The height or depth of a tree is defined to be the maximum level of any node in the tree.
Ans) A Binary tree T is a finite set of nodes with the following properties:
Ans) Visiting all nodes of a tree exactly once in a systematic way is called Tree Traversal.
Different types of tree traversals are
Ans) A Binary Search Tree T is a finite set of keys. Either the set is empty T=Ø ,or the set
consists of a root “r” and exactly two binary search trees TL and TR,T = {r,TL,TR}
with the following properties:
i) All the keys contained in the left subtree are less than the root key.
ii) All the keys contained in the right subtree are larger than the root
20Q) What is the best, average and worst case time complexity of insertion, deletion and
Search operations in a Binary Search Tree?
Ans) In Best and Avg case O(log n) and in Worst case O(n).
Ans) An AVL Search Tree is a balanced binary search tree. An empty binary tree is AVL
balanced. A non – empty binary tree, T={r,TL,TR} is AVL balanced if both TL & TR
are AVL balanced and | hL – hR | <=1,
Where : hL is the Height of the left subtree and hR is the Height of the right subtree.
Ans) If a binary tree of height ‘h’ has exactly [2h+1 -1] nodes then that binary tree is called as
Full Binary Tree.
Ans) Complete Binary Tree is a binary tree T = {r,TL,TR} with the following properties:
i) The parent of “i” is at position “i/2” [if i=1 it is the root node and has no parent]
ii) The left child of node “i” is at position “2i” [if 2i>n then no left child exists]
iii) The right child of node “i” is at position “2i+1” [if 2i+1>n then no right child
exists]
[Note: In a complete binary tree nodes are filled level by level from left to
PRIORITY QUEUES:
25Q) What is Priority Queue and differentiate Priority Queue and Queue?
In Priority Queue items can be inserted arbitrary order but the items are deleted based
upon the priority that is the item with highest priority is deleted first. Whereas in Queue
the items are inserted from rear end and deleted from front end and the item which is
inserted first is deleted first (FIFO).
Ans) Min Heap is a Complete Binary Tree in which the key value at parent is always less than
or equal to its child values.
Ans) In Max Heap the key value at parent is always larger or equal to its child values.
Ans) A (Binary) Heap is a Complete Binary Tree with Heap Ordered Property (Min Heap
or Max Heap) [Note: Duplicates are allowed in a Heap]
GRAPHS:
29Q) What is a Graph? Name various Graph Representation Techniques?
Ans) A Graph contains Cycles (loops) but a Tree does not contain any cycles.
[A Tree contains a root node but Graph does not contain the root node.]
Ans) A Spanning Tree T = (V', E') is a Sub Graph of G = (V,E) with following properties:
[Note: I f Graph has “n” vertices the Spanning Tree contains exactly “n - 1” edges]
Ans) In linear search the item to be searched is compared with each item starting with the item
in the first position of the array until the item is found or all the items of the array. The
time complexity of linear search in Worst case is O(n).
Ans) Binary Search method can be used only if the list is Sorted. In binary search method first
the array is divided in to two by finding the mi position of the array and the item to be
searched is compared with the mid value.
Ans) i) MERGE SORT ii) QUICK SORT iii) INSERTION SORT iv) SELECTION SORT
v) HEAP SORT
[NOTE: If the given array is already sorted then Insertion Sort technique is best to sort the
given sequence.]
Ans) If the size of the array to be sorted is very large the Heap sort is efficient than Quick
sort since in worst case the time complexity of Heap sort remains same as O (n log n)
but Quick Sort time complexity changes to O (n2).But if the size of the Array is small
then Quick sort is efficient than Heap sort.
Ans) Hashing is used to determine the position of a Key by using the Key itself. To determine
the position of the key it makes use of Function called Hash function.
A hash function takes a group of characters (called a key) and maps it to a value of a
certain length (called a hash value or hash). The hash value is representative of the
original string of characters, but is normally smaller than the original.
(or)
Ans) i) Division method ii) Mid Square method iii) Folding method iv) Digit Analysis method
Hashing Methods
There are eight hashing methods they are:
1. Modulo-division
2. Midsquare
3. Digit Extraction (or) Digit Analysis
4. Folding
(or)
This algorithm works with any list size, but a list size that is a prime number
produces fewer collisions than other list sizes.
The formula to calculate the address
is: Address = key MODULO listsize +
1
Where listsize is the number of elements in the arrary.
Example:
Given data : Keys are : 137456 214562 140145
137456 % 19 +1 = 11, 214562 % 19 + 1 = 15, 140145 % 19 + 1 = 2
(or)
In midsquare hashing the key is squared and the address is selected from the middle
of the square number.
Limitation is the size of the key.
Example:
94522 = 89340304: address is 3403
3. Folding Method
Two folding methods are used they are:
i) Fold shift
ii) Fold boundary
i) Fold Shift
In fold shift the key value is divided into parts whose size matches the size of the
required address. Then the left and right parts are shifted and added with the middle part.
Example:
4. Digit-extraction (or) Digit Analysis Method: Using digit extraction selected digits are
extracted from the key and used as the address.
Example: Using six-digit employee number to hash to a three digit address (000-999), we
could select the first, third, and fourth digits( from the left) and use them as the address.
The keys are: 379452 -> 394, 121267 -> 112, 378845 -> 388
Ans) By using the hash function if two different keys are hashed to the same position, this
situation is called Collision. That is: If x and y are two different (x ≠ y) and h(x) =
h(y) this situation is called Collision.
• A collision occurs when the home bucket for a new pair is occupied by a pair with a
different key.
• An overflow occurs when there is no space in the home bucket for the new pair.
• When a bucket can hold only one pair, collisions and overflows occur together.
Uniform Hash Function: A uniform hash function maps the keys in keySpace into
buckets such that approximately the same number of keys get mapped into each bucket.
• An overflow occurs when the home bucket for a new pair (key, element) is full.
Open Addressing: Search the hash table in some systematic fashion for a bucket
that is not full.
• Quadratic probing.
• Rehashing.
• Random probing.
• Chain.
Ans) There are several methods for handling collisions, each of them independent of the hashing
algorithm.
1. en Addressing : a) Linear Probing b)Quadratic Probing c) Rehashing d) Random
Probing
2. Chaining
Linear Probe : In Linear Probing, when inserting a new pair whose key is k, we search
the hash table bucket in the order, ht[ h(k) + i ] % b,0<= i <= b-1,where h is the hash
function and b is the number of buckets.
Random Probing: In Random Probing, the search for a key , k in a hash table with b
buckets is carried out by examining the buckets in the order h(k), [h(k) + s(i) ] % b, 1<= I
<= b-1 where s(i) is a pseudo random number. The random number generator must
satisfy the property that every number from 1 to b-1 must be generated exactly once as i
ranges from 1 to b-1.
Ans) Hashing is used to construct the symbol table used by the language compilers.
Ans) To access the Private members of one class into another class Friend function is used.