Datastructures& Algorithms QA

13
Data Structures and Algorithms Section I – Data Structures 1. What is data structure?  A data structure is a way of organizing data that considers not only the items sto red, but als o their rel ationship to eac h other. Adv ance knowledge about the rela tio nsh ip bet ween dat a items all ows design ing of eff icient alg orit hms for the manipulation of data. 2. List out the areas in which data struc tures are appli ed extens ively ?  Compiler Design, Operating System, Database anagement System, Statistical analysis package, !umerical Analysis, "raphics, Artificial #ntelligence, Simulation 3. What are t he majo r da ta s tructures used in the fol lowing areas : !"#$% &et wor' data model ( )ierarchical data model. $D%S & Array 'i.e. Array of structures( !etwork data model & "raph )ierarchical data model & *rees *. +f you are using , langu age to imple ment the het erogeneous lin 'ed list% what pointer type will you use? *he heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. #t is not possible to use ordinary pointers for this. So we go for void pointer. +oi d pointer is capable of storing pointer to any type as it is a generic pointer type.  -. #inimum numer of /ueues needed to imple ment a priori ty /ueue? *wo. One ueue is used for actual storing of data and another for storing priorities. 0. What is the data struc ture used to perfor m recurs ion? 

Transcript of Datastructures& Algorithms QA

Page 1: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 1/13

Data Structures and Algorithms

Section I – Data Structures

1. What is data structure? 

A data structure is a way of organizing data that considers not only the items

stored, but also their relationship to each other. Advance knowledge about the

relationship between data items allows designing of efficient algorithms for the

manipulation of data.

2. List out the areas in which data structures are applied extensively? 

Compiler Design,

Operating System,

Database anagement System,

Statistical analysis package,

!umerical Analysis,

"raphics,

Artificial #ntelligence,

Simulation

3. What are the major data structures used in the following areas : !"#$% &etwor'

data model ( )ierarchical data model.

$D%S & Array 'i.e. Array of structures(

!etwork data model & "raph

)ierarchical data model & *rees

*. +f you are using , language to implement the heterogeneous lin'ed list% what pointer

type will you use? 

*he heterogeneous linked list contains different data types in its nodes and we

need a link, pointer to connect them. #t is not possible to use ordinary pointers for

this. So we go for void pointer. +oid pointer is capable of storing pointer to any type

as it is a generic pointer type.

 

-. #inimum numer of /ueues needed to implement a priority /ueue? 

*wo. One ueue is used for actual storing of data and another for storing

priorities.

0. What is the data structure used to perform recursion? 

Page 2: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 2/13

Stack. %ecause of its -#O '-ast #n irst Out( property it remembers its /caller01

so the function knows where to return when the function has to return. $ecursion

makes use of system stack for storing the return addresses of the function calls.

very recursive function has its e/uivalent iterative nonrecursive4 function.

2ven when such euivalent iterative procedures are written, e3plicit stack is to be

used.

5. What are the notations used in valuation of 6rithmetic xpressions using prefix and

 postfix forms? 

4olish and $everse 4olish notations.

7. ,onvert the expression 6 8 "4 9 , ! 4 ; < 8 =44 to e/uivalent >refix and

>ostfix notations.

4refi3 !otation5

6 7 8 9A%C 7 D2 9 "

4ostfi3 !otation5

A% 9 C 8 D2 7 7 " 9 6

. $orting is not possile y using which of the following methods? 

a4 +nsertion

4 $election

c4 xchange

d4 !eletion

 Answer: 'd(deletion

Explanation:

:sing insertion we can perform insertion sort, using selection we can perform

selection sort, using e3change we can perform the bubble sort 'and other similar

sorting methods(. %ut no sorting method can be done ;ust using deletion.

1@. 6 inary tree with 2@ nodes has null ranches.

 Answer: <=

Explanation:-et us take a tree with > nodes 'n?>(

#t will have only @ 'ie,>9=( null branches. #n general, a  inary tree with n

nodes has exactly n+1 null nodes.

 Null Branches

Page 3: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 3/13

11. What are the methods availale in storing se/uential files ? 

Straight merging,

!atural merging,

4olyphase sort,

Distribution of #nitial runs.

12. )ow many different trees are possile with 1@ nodes ? 

 Answer: ==B

Explanation:

or e3ample, consider a tree with nodes'n?(, it will have the ma3imum

combination of > different 'ie, < 7 ? >( trees.

i ii iii iv v

#n general5

#f there are n nodes, there e3ist 2n-n different trees.

13. List out a few of the applications of tree datastructure in compilersAcompiler design.

*he manipulation of Arithmetic e3pression,

Symbol *able construction,

Synta3 analysis.

1*. List out a few of the applications that ma'e use of #ultilin'ed $tructures.

Sparse matri3,

#nde3 generation.

1-. +n tree construction which is the suitale efficient data structure? 

a 4 6rray

4 Lin'ed list

c4 $tac'

d4 Bueue

e4 none

 Answer: 'b( -inked list

Page 4: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 4/13

10. What type of the algorithm is used in solving the 7 Bueens prolem? 

%acktracking

15. +n an 6CL tree% what is the condition for alancing to e done?  

*he /pivotal value0'or the /)eight factor0( is greater than = or less than&=.

17. What is the uc'et siDe% when overlapping and collision occur at the same time? 

One. #f there is only one entry possible in the bucket, when the collision

occurs, there is no way to accommodate the colliding value. *his results in the

overlapping of values.

1. Eraverse the given tree using +norder% >reorder and >ostorder traversals.

#norder 5 D ) % 2 A C # "

4reorder5 A % D ) 2 C " #

4ostorder5 ) D 2 % # " C A

2@. Ehere are 7% 1-% 13% 1* nodes were there in * different trees. Which of them could

have formed a full inary tree? 

=>.

#n general5*here are 2n-1 nodes in a full binary tree.

%y the method of elimination:

ull binary trees contain odd  number of nodes. So there cannot be full binary

trees with E or =B nodes, so re;ected. Fith = nodes you can form a complete binary

tree but not a full binary tree. So the correct answer is =>.

Note:

ull and Complete binary trees are different. 6ll full inary trees are complete

inary trees ut not vice versa.

A

B   C

D   E   F   G

H I   J

Given tree:

Page 5: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 5/13

21. +n the given inary tree% using array% at which location can can you store the node *? 

 Answer: At location @

Explanation:

= < 7 7 B 7 7 >

$oot -C= $C= -C< $C< -C $C -CB $CB

where -Cn means -eft Child of node n and $Cn means $ight Child of node n

22. $ort the given values using Buic' $ort? 

@> G G> E E> @ >> > B>Sorting takes place from the pivot value, which is the first value of the given

elements, this is marked bold. *he values at the left pointer and right pointer are

indicated using - and $ respectively.

65 G- G> E E> @ >> > B>$

Since pivot is not yet changed the same process is continued after

interchanging the values at - and $ positions

65 B> G> - E E> @ >> > $ G65 B> > E - E> @ >> $ G> G

65 B> > >> E> - @ $ E G> G65 B> > >> @ $ E> - E G> G

Fhen the - and $ pointers cross each other the pivot value is interchanged

with the value at right pointer. #f the pivot is changed it means that the pivot has

occupied its original position in the sorted order 'shown in bold italics( and hence two

different arrays are formed, one from start of the original array to the pivot position7=

and the other from pivot position9= to end.

60 - B> > >> $ 65  85 - E G> G $

55 - B> > $ 60 65   70 $ E - G> 85 

50 - B> $ 55 60 65 70 E - G> $ 85 

#n the ne3t pass we get the sorted form of the array.

1

2 3

4

5

Page 6: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 6/13

45 50 55 60 65 70 75 80 85  

23. <or the given graph% draw the !<$ and "<$? 

"<$: A H " ) 4 2 I

!<$: A H ) 4 2 I "

2*. ,lassify the )ashing <unctions ased on the various methods y which the 'ey value

is found. 

Direct method,

Subtraction method,

odulo7Division method,

Digit723traction method,

id7Suare method,

olding method,

4seudo7random method.

2-. What are the types of ,ollision esolution Eechni/ues and the methods used in each

of the type? 

Open addressing 'closed hashing(.

*he methods used include5

Overflow block.

Closed addressing 'open hashing(*he methods used include5

-inked list, %inary treeJ

20. +n !"#$% what is the efficient data structure used in the internal storage

representation? 

%9 tree. %ecause in %9 tree, all the data are stored only in leaf nodes, that

makes searching easier. *his corresponds to the records that shall be stored in leaf

nodes.

A

HX

G

E

!

"   J

#he $iven $ra%h:

Page 7: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 7/13

25. !raw the "tree of order 3 created y inserting the following data arriving in

se/uence 2 2* 0 5 11 7 22 * - 10 1 2@ 57

27. Ff the following tree structure% which is efficient considering space and time

complexities? 

a4 +ncomplete "inary Eree

4 ,omplete "inary Eree

c4 <ull "inary Eree

 Answer: 'b( Complete %inary *ree.

Explanation:

"y the method of elimination:

ull binary tree loses its nature when operations of insertions and deletions are

done. or incomplete binary trees, e3tra storage is reuired and overhead of !:--

node checking takes place. So complete binary tree is the better one since the

property of complete binary tree is maintained even after operations like additions

and deletions are done on it.

2. What is a spanning Eree? 

A spanning tree is a tree associated with a network. All the nodes of the graph

appear on the tree once. A minimal spanning tree is a spanning tree organized so that

the total edge weight between nodes is minimized.

3@. !oes the minimal spanning tree of a graph give the shortest distance etween any 2

specified nodes? 

 Answer: !o.

Explanation:

inimal spanning tree assures that the total weight of the tree is kept at its

minimum. %ut it doesnGt  mean that the distance between any two nodes involved in

the minimum7spanning tree is minimum.

31. ,onvert the given graph with weighted edges to minimal spanning tree. 

11   &

5   '   1(   24

4   &   )   &   *   &   1)   &   2+   22   '*   (2

Page 8: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 8/13

the euivalent minimal spanning tree is5

32.Which is the simplest file structure? 

a4 $e/uential

4 +ndexed

c4 andom

 Answer: 'a( Seuential

33. +s lin'edlist a linear or nonlinear data structure? 

According to Access strategies -inked list is a linear one.

According to Storage, -inked -ist is a !on7linear one.

3*. !raw a inary Eree for the expression :

 6 9 " , 8 !4 9 > A B4

1 3

2   4

541+

)++

2++

4++

31+

1421

2(*5

)12

&

,   ,

A   B   -   .

C   D   /

1

2

3

4   5

41+   )122++

31+

Page 9: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 9/13

3-. <or the following ,F"FL code% draw the "inary tree? 

= S*:D2!*K$2C.

< !A2.

#$S*K!A2 4#C H'=(.

-AS*K!A2 4#C H'=(.

< I2A$KOKS*:DI.

#$S*KS2 4#C HH.

S2CO!DKS2 4#C HH.

0#DEN#EC

 NA"E !EAF0#D!

FI0#NA"E A0#NA"E FI0#0E" 0ECND0E"

+1

+2 +2

+3 +3 +3 +3

Page 10: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 10/13

Section ## & Algorithms

1. What is an HalgorithmG? 

An algorithm consists of a finite set of steps that may reuire one or more

operations. *hese operations should be definite and effective. An algorithm should

produce one or more output0s and may have zero or more inputs.

*his consists of five distinct areas5

  =. to device algorithms

<. to validate the algorithms

. to e3press the algorithms

B. to analyse the algorithms

>. to test the programs for the algorithms

2. What is a Hcomputational procedureG? 

An algorithm that does not terminate is called /computational procedure0.

23ample for such /computational procedure0 is an /operating system0.

3. !efine Hrecursive algorithmG.

An algorithm is said to be recursive if the same algorithm is invoked in the

body of the algorithm. $ecursive algorithms can be divided into direct recursive and

indirect recursive algorithms.

!irect recursive:

An algorithm that calls itself.

+ndirect ecursive:

An algorithm /A0 is said to be indirect recursive if it calls another algorithm

which in7turn calls algorithm /A0.

*. )ow can you classify performance analysis? 

 /4erformance analysis0 can be classified as5

i. priori analysis

ii. posteriori analysis

>riori 6nalysis:*he bounds of algorithms0 computing time are obtained by formulating a

function.

>osteriori 6nalysis:

*esting the actual computation of space and time are recorded while the

algorithm is e3ecuting.

>.   !efine H"ig FG. 

or the function f'n(

fn4IFgn44

Page 11: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 11/13

iff there e3ist positive constants c and d such that5

fn4 JIc9gn4

for all n,nL?d.

*his is defined to be the worst7time comple3ity of the function f'n(.

or e3ample5

O'n(?n9< because,

  n9< M?Bn for all nL?<.

0. =ive various computing times and their meaning.

ew of the important computing times are5

  ,omputing Eime #eaning

O'=( 5 constant computing time

O'n( 5 linear computing time

O'n8n( 5 uadratic computing time

O'n8n8n( 5 cubic computing time

O'<8<8<8<8..............8n( 5 e3ponential computing time

 

5. =ive the most important Hasic designsH of algorithms.

  *here are five important basic designs for algorithms. *hey are5

i. Divide and conuer,

ii. *he greedy method,

iii. Dynamic programming,

iv. %ack7tracking,

v. %ranch and bound.

 

7. )ow does Hdivide and con/uerG algorithms wor'? 

or a function to compute on n inputs the divide and conuer strategy

suggests the inputs into a k distinct subsets, =MkM?n, yielding k sub7problems. *hese

sub7problems must be solved and then a method must be found to combine the sub7

solutions into a solution of the whole.

An e3ample for this approach is /binary search0 algorithm. *he time comple3ityof binary search algorithm is O'log n(.

. What is =reedy #ethod? 

*he greedy method suggests that one can devise an algorithm that works in

stages, considering one input at a time. At each stage, a decision is made regarding

whether a particular input is an optimal solution. An e3ample for solution using

greedy method is /knapsack problem0.

1@. What is !ynamic >rogramming? 

Page 12: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 12/13

Dynamic 4rogramming is an algorithm design method that can be used when

the solution to a problem can be viewed as the result of a seuence of decisions. An

e3ample for algorithm using dynamic programming is /multistage graphs0.

11. What are the time complexities for the following algorithms? 

%inary search 5 O'logn(

inding ma3imum and minimum for a given set of numbers

5 O'nN<7<(

erge Sort 5 O'nlogn(

#nsertion Sort 5 O'n8n(1

uick Sort 5 O'nlogn(

Selection Sort 5 O'n(

12. What is the difference etween #erge $ort and Buic' sort? 

%oth erge7sort and uick7sort have same time comple3ity i.e. O'nlogn(. #n

merge sort the file aP=5nQ was divided at its midpoint into sub7arrays which are

independently sorted and later merged. Fhereas, in uick sort the division into two

sub7arrays is made so that the sorted sub7arrays do not need to be merged latter.

13. +s there any optimum solution for #atrix multiplication? 

Ies. Divide and conuer method suggests Strassen0s matri3 multiplication

method to be used. #f we follow this method, the time comple3ity is

O'n8n8nJJ..8<.E=( times rather O'n8n8n8JJJ8( times.

1*. !efine Hminimum cost spanning methodG.

-et "?'+,2( be an undirected connected graph. A sub7graph t ?'+, 20( of " is

a spanning tree of " if and only if t is a tree.

*o find out minimum cost spanning method we have following method0s1

4rim0s Algorithm 5 O'n8n(

Rruskal0s Algorithm 5 O'e loge(

1-. !efine Garticulation pointsG.

A +erte3 + in a connected graph " is an articulation point if and only if the

deletion of verte3 + together will all edges incident for disconnects the graph into two

or more non7empty Components.

10. !efine Hiconnected graphG.

A graph " is biconnected if and only if it contains no articulation points.

=G.   What are HexplicitG and HimplicitG constraints?  

Page 13: Datastructures& Algorithms QA

8/13/2019 Datastructures& Algorithms QA

http://slidepdf.com/reader/full/datastructures-algorithms-qa 13/13

 /23plicit constraints0 are rules that restrict each 3i to take on values only from a

given set. /#mplicit constraints0 are rules that determine which of the tuples in the

solution space of i satisfy the criterion function.

17. =ive H,oo'ies theoremG.

$atisfiaility is in > iff >I&> 

where 4 is the set of all decision problems solvable by deterministic algorithms

in polynomial time and !4 is the set of all decision problems solvable by non7

deterministic algorithms in a polynomial7time.