29 views

Uploaded by Arjun Edakkat

Data structure and algorithm viva Qns and answer

save

- WebSphere® Process Server v6.1 Business Rule Management Programming Guide
- Dat Structure Homework3
- Netgraph Presentation
- 28-binarytrees3
- 5. Trees-1
- Short Questions on DS
- Managing Hierarchical Data in MySQL
- bst imp (1)
- Algorithms
- Key 1
- Algorithm
- Revision Questions
- Tree (BST) Implementation half.pptx
- OTL_Boot_Camp.pdf
- 01. SpriteKit PG
- SunSte08A
- 1p175
- A Scalable Distributed Algorithm For
- Computer science final file with SQL tables
- Handout 7
- bin_tree
- Learning Unity3D Using C#
- SWIFT Story of Sustainable Change: Upgrading the water system in Namorputh, Turkana
- Mobile Agent
- Chapter_3-Algorithms-for-Query-Processing-and-Optimization.pdf
- Data Structure Question Bank
- networkcomplete.ppt
- advrptdesn.pdf
- Bharat Institute of Engineering
- Multiobjective evolutionary algorithm with a discrete differential mutation operator developed for service restoration in distribution systems 1-s2.0-S0142061514002798-main
- The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- Dispatches from Pluto: Lost and Found in the Mississippi Delta
- Yes Please
- The Unwinding: An Inner History of the New America
- Sapiens: A Brief History of Humankind
- The Prize: The Epic Quest for Oil, Money & Power
- The Emperor of All Maladies: A Biography of Cancer
- This Changes Everything: Capitalism vs. The Climate
- Grand Pursuit: The Story of Economic Genius
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
- Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
- John Adams
- The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
- Smart People Should Build Things: How to Restore Our Culture of Achievement, Build a Path for Entrepreneurs, and Create New Jobs in America
- Rise of ISIS: A Threat We Can't Ignore
- The World Is Flat 3.0: A Brief History of the Twenty-first Century
- The New Confessions of an Economic Hit Man
- Team of Rivals: The Political Genius of Abraham Lincoln
- Bad Feminist: Essays
- Steve Jobs
- Angela's Ashes: A Memoir
- How To Win Friends and Influence People
- The Incarnations: A Novel
- You Too Can Have a Body Like Mine: A Novel
- The Sympathizer: A Novel (Pulitzer Prize for Fiction)
- Extremely Loud and Incredibly Close: A Novel
- Leaving Berlin: A Novel
- The Silver Linings Playbook: A Novel
- The Light Between Oceans: A Novel
- The Flamethrowers: A Novel
- Brooklyn: A Novel
- We Are Not Ourselves: A Novel
- The First Bad Man: A Novel
- The Rosie Project: A Novel
- The Blazing World: A Novel
- The Master
- Bel Canto
- A Man Called Ove: A Novel
- The Love Affairs of Nathaniel P.: A Novel
- Life of Pi
- The Cider House Rules
- A Prayer for Owen Meany: A Novel
- The Bonfire of the Vanities: A Novel
- Lovers at the Chameleon Club, Paris 1932: A Novel
- The Perks of Being a Wallflower
- The Kitchen House: A Novel
- Beautiful Ruins: A Novel
- Interpreter of Maladies
- The Wallcreeper
- Wolf Hall: A Novel
- The Art of Racing in the Rain: A Novel
- A Tree Grows in Brooklyn

You are on page 1of 9

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.

- WebSphere® Process Server v6.1 Business Rule Management Programming GuideUploaded bymaldoreur
- Dat Structure Homework3Uploaded byjitenderthakur490
- Netgraph PresentationUploaded byapi-3744861
- 28-binarytrees3Uploaded byTan Rathana
- 5. Trees-1Uploaded bySarthak Patel
- Short Questions on DSUploaded byKattupalli Sudhakar
- Managing Hierarchical Data in MySQLUploaded byArisetty Sravan Kumar
- bst imp (1)Uploaded byAnkit Jain
- AlgorithmsUploaded bycetec1234
- Key 1Uploaded bydselvara
- AlgorithmUploaded byEbony Roberts
- Revision QuestionsUploaded byJai Gaizin
- Tree (BST) Implementation half.pptxUploaded byMuhammad Bilal Sahi
- OTL_Boot_Camp.pdfUploaded byMohamed Shanab
- 01. SpriteKit PGUploaded byDaniel Gutierrez Daneri
- SunSte08AUploaded byBao Huynh
- 1p175Uploaded byAhmed Taha
- A Scalable Distributed Algorithm ForUploaded byostojic007
- Computer science final file with SQL tablesUploaded byrohit
- Handout 7Uploaded byAbdullah Salem
- bin_treeUploaded byMondar Deb
- Learning Unity3D Using C#Uploaded byAlfian Try Putranto
- SWIFT Story of Sustainable Change: Upgrading the water system in Namorputh, TurkanaUploaded byOxfam
- Mobile AgentUploaded byHarika Imaymissu
- Chapter_3-Algorithms-for-Query-Processing-and-Optimization.pdfUploaded byMy Heo
- Data Structure Question BankUploaded byamitukumar
- networkcomplete.pptUploaded byAshok Sharma
- advrptdesn.pdfUploaded byjamie8889
- Bharat Institute of EngineeringUploaded bydasariorama
- Multiobjective evolutionary algorithm with a discrete differential mutation operator developed for service restoration in distribution systems 1-s2.0-S0142061514002798-mainUploaded bySatishKumarInjeti