You are on page 1of 9

www.Vidyarthiplus.

in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page 1






Document Name: Data Structure and Algorithm
VIVA Questions


www.Vidyarthiplus.in


Facebook: www.facebook.com/vidyarthiplus


Twitter: www.twitter.com/vidyarthiplus









www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page 2

Stack:A stack is a linear list of elements for which all insertions and deletions(usually
accesses) are made at only one end of the list.

They are also called as LIFO lists(Last Input First Output).
The operations supported are :

1)IsEmpty(S): returns whether the stack is empty or not.

2)IsFull(S): return whether the stack is full or not.

3)Push(Element X,S): pushes element X on to the top of the stack.

4)Pop(S) : pops an element from the top of the stack on to the output(printing on to the
output console isn't necessary though in which case we can define another function
Top(S) which gives the top element of the stack).

All the above mentioned operations are of O(1) complexity.

Queue: Queue is a linear list for which all insertions are made at one end and
deletions(accesses as well)
are made at the other end of the list and the lists are also called as FIFO lists(First Input
First Output ).

The operations supported are

1)IsEmpty(Q):returns whether the queue is empty or not.

2)IsFull(Q): return whether the queue is full or not.

3)Enqueue(Element X,Q): inserts an element X on the rear side of the queue.

4)Dequeue(Q): removes the element pointed to by the front end of the queue.

Similar to a stack ,the operations of the queue are also of O(1) complexity.

Dequeue (double ended queue):A Dequeue is a linear list for which insertions and
deletions(accesses as well) occur at the ends.
Analogous to the operations defined for stack and Queue,we can also define some
operations for Dequeue.
A simple observation reveals the fact that we can simulate both stack and queue from
Dequeue by input and output restrictions.

Having dwelt at such a length on these linear lists,we shall also see some interesting
questions based on the simple properties of these linear lists.


www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page 3


Questions

1)How do you implement 2 stacks using only one array. Your stack routines should
not indicate an overflow unless every slot in the array is used?

Solution: given an Array, start the first stack S1 from left end and other stack S2 from the
right end. While S1 gets grows towards right, S2 grows towards left.


2) propose a data structure which supports the stack Push and Pop operations and a
third operation FindMin, which returns the smallest element in the data structure
all in O(1) worst case time.

Solution: Use 2 stacks S1 in to which the elements are pushed and S2 in to which only
the current minimum is pushed.
When one needs to insert an element E ,we first push E on to S1 and then access the top
element T of S2 which is the minimum before E has been inserted.If only E is less than T
, we push E on to S2 .
When one needs to pop an element ,pop the top element of S1 and if this element is also
equal to the one on top of S2, then pop it off S2 as well.
Hence the current minimum will always be on top of S2 .Hence along with other normal
stack operations, access of minimum element is also possible in O(1).

3)Show how to implement 3 stacks in a single array efficiently?(debated question)

Solution: It is still up for debate ,as we haven't yet figured out the exact solution. We will
soon put the best solution for this problem.


4) Consider a empty stack of integers. Let the numbers 1, 2, 3,4,5,6 be pushed on to
this stack only in the order they appeared from left to right. Let S indicates a push
and X indicates a pop operation. Can they be permuted in to the order
325641(output) and order 154623? (if a permutation is possible give the order string
of operations.
(Hint: SSSSSSXXXXXX outputs 654321)

Solution: SSSXXSSXSXXX outputs 325641.
154623 cannot be output as 2 is pushed much before 3 so can appear only after 3 is
output.

5)Given a string containing N S's and N X's where S indicates a push operation and
X indicates a pop operation, and with the stack initially empty,Formulate a rule to
check whether a given string S of operations is admissible or not (Hint: A string S of
operations should always abide by the properties of the stack which in this case only
means you never pop an element from an empty stack)
www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page


Solution:Given a string of length 2N, we wish to check whether the given string of
operations is permissible or not with respect to its functioning on a stack.
The only restricted operation is pop whose prior requirement is that the stack should not
be empty.So while traversing the string from left to right,prior to any pop the stack
shouldn't be empty which means the no of S's is always greater than or equal to that of
X's.Hence the condition is at any stage on processing of the string,
no of S's > no of X's

6)Find a simple formula for An, the number of permutations the can be printed on
an input of n distinct characters to a stack (similar to question 4)

Solution:The numbers are input in the order 1,2,3,...,N.
So the problem amounts to the number of strings each of N pushes(denoted by S) and N
pops(denoted by X).
The only criteria to select a string is at any stage of its processing character by character
we should have no of S's > no of X's .
This problem is no different from parenthesis problem where N needs to give the no of
possible permutations of N parenthesis , which if given by Nth catalan number
Cn=(2n)!/((n+1)! n!).
For more insite into catalan number refer this link.

7)Show that it is possible to obtain the permutation P1P2.........Pn from 1,2,.........n
using a stack if and only if there are no indices i < j < k such that Pj < Pk < Pi.

Solution:The solution can be arrived simply by veirfying that among the 6 possible
orders of Pi,Pj,Pk the rest 5 are possible and the ordering in question i.e is Pi,Pj,Pk is not
possible.
We leave it to the reader the verification of possibility of the 5 orderings and deal only
with proving that the order Pi,Pj,Pk is not possible.
Suppose say that the order Pi,Pj,Pk is possible.
As Pi is the largest and printed first (i < j < k) followed by Pj and Pk,just before the
popping of Pi the ordering of these 3 on the stack shall be Pi,Pk followed by Pj(from
www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page 5

top).But as j<k ,Pj is printed prior to Pk contradicting the ordering on the stack. Hence
this ordering is not possible.

8) What is mean by d-queue?

Solution: D-queue stands for double ended queue. It is a abstract data structure that
implements a queue for which elements can be added to front or rear and the elements
can be removed from the rear or front. It is also called head-tail linked list

9) Example of linear and non-linear data structures?

Solution: The data structure is said to be a Linear data structure if its elements are in
sequence and form a linear list.Ex. Arrays, Stacks, Queues,Linked Lists.
If the elements of data structure do not form a sequence or a linear list then that
type of data structure is called as Non-Linear data structure.Ex. Trees, BST(Binary
Search Trees) etc.

10) How to find the number of possible tree in the given tree.

Solution: The number of possible tree = (2 power of n) - n.
For example: A tree contain three node. So if n=3, the possible number of trees = 8 - 3 =
5.

11) What is hashing

Solution: Hashing is a way retrieving records from memory in faster way. Record is
inserted into memory by using hash function (division, midsqure, folding, digit
analysis)and also records
are retrieved using same hash function.

12) What is almost complete binary tree?

www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page !

Solution: An almost complete binary tree is a tree in which each node that has a right
child also has a left child. Having a left child does not require a node to have a right
child. Stated alternately, an almost complete binary tree is a tree where for a right child,
there is always a left child, but for a left child there may not be a right child. The number
of nodes in a binary tree can be found using this formula: n = 2^h Where n is the amount
of nodes in the tree, and h is the height of the tree.

13) What is AVL tree?

Solution: Avl tree is self binary tree in which balancing factor lie between the -1 to 1.It is
also known as self balancing tree.
14)What is a B tree?
Solution:
A B-tree of order m (the maximum number of children for each node) is a tree which
satisfies the following properties :
1. Every node has <= m children.
2. Every node (except root and leaves) has >= m/2 children.
3. The root has at least 2 children.
4. All leaves appear in the same level, and carry no information.
5. A non-leaf node with k children contains k – 1 keys
14) What are threaded binary trees?
Solution
In a threaded binary tree, if a node 'A' has a right child 'B' then B's left pointer must be
either a child, or a thread back to A.
In the case of a left child, that left child must also have a left child or a thread back to A,
and so we can follow B's left children until we find a thread, pointing back to A.
This data structure is useful when stack memory is less and using this tree the treversal
around the tree becomes faster.
15) What is a B+ tree?
Solution
It is a dynamic, multilevel index, with maximum and minimum bounds on the number of
keys in each index segment. all records are stored at the lowest level of the tree; only
keys are stored in interior blocks.
www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page "

16) Describe Tree database. Explain its common uses.
Solution: A tree is a data structure which resembles a hierarchical tree structure. Every
element in the structure is a node. Every node is linked with the next node, either to its
left or to its right. Each node has zero or more child nodes. The length of the longest
downward path to a leaf from that node is known as the height of the node and the length
of the path to its root is known as the depth of a node.
Common Uses:
- To manipulate hierarchical data
- Makes the information search, called tree traversal, easier.
- To manipulate data that is in the form of sorted list.
- To give visual effects for digital images using as a work flow by compositing them.
17) What is binary tree? Explain its uses.
Solution:A binary tree is a tree structure, in which each node has only two child nodes.
The first node is known as root node. The parent has two nodes namely left child and
right child.
Uses of binary tree:
- To create sorting routine.
- Persisting data items for the purpose of fast lookup later.
- For inserting a new item faster
18) How do you find the depth of a binary tree?
Solution: The depth of a binary tree is found using the following process:
1. Send the root node of a binary tree to a function
2. If the tree node is null, then return false value.
3. Calculate the depth of the left tree; call it ‘d1’ by traversing every node. Increment the
counter by 1, as the traversing progresses until it reaches the leaf node. These operations
are done recursively.
4. Repeat the 3rd step for the left node. Name the counter variable ‘d2’.
5. Find the maximum value between d1 and d2. Add 1 to the max value. Let us call it
‘depth’.
6. The variable ‘depth’ is the depth of the binary tree.
19) Explain pre-order and in-order tree traversal.
Solution:A non-empty binary tree is traversed in 3 types, namely pre-order, in-order and
post-order in a recursive fashion.
www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page #

Pre-order:
Pre-order process is as follows:
- Visit the root node
- Traverse the left sub tree
- Traverse the right sub tree
In-Order:
In order process is as follows:

- Traverse the left sub tree
- Visit the root node
- Traverse the right sub tree
20) What is a B+ tree? Explain its uses.
Solution: B+ tree represents the way of insertion, retrieval and removal of the nodes in a
sorted fashion. Every operation is identified by a ‘key’. B+ tree has maximum and
minimum bounds on the number of keys in each index segment, dynamically. All the
records in a B+ tree are persisted at the last level, i.e., leaf level in the order of keys.
B+ tree is used to visit the nodes starting from the root to the left or / and right sub tree.
Or starting from the first node of the leaf level. A bi directional tree traversal is possible
in the B+ tree.
21) Define threaded binary tree. Explain its common uses
Solution: A threaded binary tree is structured in an order that, all right child pointers
would normally be null and points to the ‘in-order successor’ of the node. Similarly, all
the left child pointers would normally be null and points to the ‘in-order predecessor’ of
the node.
Uses of Threaded binary tree:
- Traversal is faster than unthreaded binary trees
- More subtle, by enabling the determination of predecessor and successor nodes that
starts from any node, in an efficient manner.
- No stack overload can be carried out with the threads.
- Accessibility of any node from any other node
- It is easy to implement to insertion and deletion from a threaded tree.
22) Explain implementation of traversal of a binary tree.
Solution: Binary tree traversal is a process of visiting each and every node of the tree.
The two fundamental binary tree traversals are ‘depth-first’ and ‘breadth-first’.
www.Vidyarthiplus.in

© Copyright 2011 – 2015 – Vidyarthiplus.in (VP Group) Page $

he depth-first traversal are classified into 3 types, namely, pre-order, in-order and post-
order.
Pre-order: Pre-order traversal involves visiting the root node first, then traversing the
left sub tree and finally the right sub tree.
In-order: In-order traversal involves visiting the left sub tree first, then visiting the root
node and finally the right sub tree.
Post-order: Post-order traversal involves visiting the left sub tree first, then visiting the
right sub tree and finally visiting the root node.
The breadth-first traversal is the ‘level-order traversal’. The level-order traversal does not
follow the branches of the tree. The first-in first-out queue is needed to traversal in level-
order traversal.
23) Explain implementation of deletion from a binary tree.
Solution: To implement the deletion from a binary tree, there is a need to consider the
possibilities of deleting the nodes. They are:
- Node is a terminal node: In case the node is the left child node of its parent, then the left
pointer of its parent is set to NULL. In all other cases, if the node is right child node of its
parent, then the right pointer of its parent is set to NULL.
- Node has only one child: In this scenario, the appropriate pointer of its parent is set to
child node.
- Node has two children: The predecessor is replaced by the node value, and then the
predecessor of the node is deleted.