Sei sulla pagina 1di 23

Data

Structures
(Solutions for Vol‐1_Classroom Practice Questions)

(i – 1) rows + one dimensional elements


1. Arrays
= L0 + (1 + 2 + ……. + i  1) + (j – 1)
(i  1)
= L0 + i + (j – 1)
01. Ans: 1010 2
Sol: Loc. of A (I) = L0 + (i-lb) * C
Loc of A [0] = 1000+(0+5)  2 = 1010 05. Ans: (c)
Sol: CMO:
02. Ans: 1024 and 1024 Storage:
Sol: (i) By RMO, the loc. of a11 a21 a31 a41 | a22 a32 a42 | a33 a43 | a44
A[ i, j ]=L0+[(i-b1) (u2-b2+1) +(j-b2)]*C 0 1 2 3 4 5 6 7 8 9
A [0, 5] = 1000 + [ (0+2)  5+ (5-3)]  2
= 1000+24 = 1024 Retrieval:
(ii) By CMO, the loc of loc of Ai, j  L 0  2D  1D
A[i ,j] = L0+[ (j-b2) (u1-b1+1) +(i-b1)]*C  L 0   j  1 cols  i  ib 
A [0, 5] = 1000 +[(5–3)  5 + (0+2)]  2
In each col., ilb = j.
= 1024
Loc. of Ai, j  L 0  [( j  1) cols  (i  j)]
In (j–1) cols
03. Ans: (a)
Sol: In general
The no. of elements is
RMO = L0 + (i – 1) r2 + (j – 1)
n  n  1  ....  n   j  1  1
= 100 + (i  1) 15 + ( j 1)
= 100 + 15 i – 15 + j – 1   j  1 n  1  2  ...  j  2
= 15 i + j + 84
 n  j  1 
 j  1 j  2
2
04. Ans: (c) loc. of A[i, j]
Sol: Lower triangular matrix 
 L 0  n  j  1 
 j  1 j  2  i  
j
 2 
a 0 0 ---- 0
b c 0 ---- 0
d e f 0–0 06. Ans: (d)
g h i j–0 Sol: RMO:
------------------ Storage:
------------------
a11 a12 | a21 a22 a23 | a32 a33 a34 | a43 a44
RMO = L0 + the number of elements in 0 1 2 3 4 5 6 7 8 9
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 2 : Data Structures

Retrieval: 08. Ans: (b)
loc of Ai, j  L 0  2D  1D Sol: Storage & Retrieval:

 L 0  number of elements in i  1 rows a21 a32 a43 | a11 a22 a33 a44 | a12 a23 a34
  j  j b  0 1 2 3 4 5 6 7 8 9
If i – j =1
Row jlb loc of Ai, j  L 0  0  i  ib 
4 3 or
3 2 (j – jlb)
2 1 i.e.,
except 1 row st
loc. of Ai, j  L 0  0
ith (i–1)  i  2
or
loc. of Ai, j  L 0  3i  4   j  i  1
 j  1
 L 0  2i  j  3
If i – j = 0
loc. of Ai, j  L 0  (n  1)
07. Ans: (a)  i  1
Sol: CMO: or
Storage:  j  1
a11 a21 | a12 a22 a32 | a23 a33 a43 | a34 a44 If i – j = – 1 // upper diagonal
0 1 2 3 4 5 6 7 8 9 loc. of Ai, j  L 0  2n  1
Retrieval:  i  1
loc. of A[i, j] = L0 + 2D + 1D or
 L 0   j  1cols  i  ib   j  2
Since i is Varying
09. Ans: (a)
Col ilb Sol: A sample 5  5 S-matrix is given below.
4 3
1 8 3 2 1
3 2
3 0 0 0 0
2 1
6 1 7 4 3
except 1st column 0 0 0 0 1
jth j-1 9 6 5 4 1
loc of Ai, j  L0  3 j 1 1 i   j 1
The compact representation is
 L 0  2 j  i  3 [1,8,3,2,1, 6,1,7,4,3, 9,6,5,4,1, 3, 1].
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 3 : CSIT‐Postal Coaching Solutions

10. Ans: 9 (b) D6,4
Sol: 2n – 1 = 10 – 1 = 9 In D6,4  A (5,4) in 3rd diagonal
In D6,5  A (5,4) in 4th diagonal
1 2 3 4

5 1 2 3 (i) i – j value is remained constant through


out the diagonal
6 5 1 2
(ii) As ‘a’ value changes, accordingly K
7 6 5 1
value also changes.
11. Ans: 190900 K value = a – (i –j)
Sol: n + (a – 1) (2n – a) loc. of A[i,j] = L0 + no. of elements in(k–1)
1000 + (101  1) (2.1000  101)
dig + 1D cross
1000 + 100  (2000  101)
loc. of A[i, j] =L0 +no. of element in (k–1)
1000 + 100  1899
1000 + 189900 = 190900 dig + (i – ilb) or (j– jlb)
but here ilb is varying diagonal by diagonal
12. Ans: (a) so prefer jlb which is always 1 in each
Sol: Square Band Matrix: diagonal
It is denoted by Dn,a where n is size of = L0 + no. of elements in (k–1) dig +(j–1)
matrix a is (a-1)’s diagonals exists above
No. of elements in (k–1) diagonals is
and below the matrix diagonal.
[n–(a-1)]+[n– (a–1)+1] + [n – (a – 1) + 2]
 a 11 a 12 a 13 a 14 a 15  + …. [n–a+(k–1)]
a a 22 a 23 a 24 a 25 
D5,4 =  [n– (a–1)] →1st diagonal because it is(a–1)th
21

a 31 a 32 a 33 a 34 a 35 
  diagonal
a 41 a 42 a 43 a 44 a 45 
(n– (a–1)+1) → 2nd diagonal
a 51 a 52 a 53 a 54 a 55  55
[n– (a–1)+2] → 3rd diagonal
Size:- N (diagonal elements ) n–a+(k–1) → (k–1)th term i.e. (k–1)th
+ 2[N–1+N–2+N-3+….+N–(a–1)]
diagonal
= N+2[(a–1)N–(1+2+3+…….+a–1)]
= (k – 1) (n–a) + 1 + 2 +….. +k – 1
Total no. of elements k (k  1)
= (k – 1) (n – a) +
= n+ 2 [n–1 + n–2 + ……. + n– (a–1)] 2
= n + 2 [(a–1) n – [1+2+ ……. + (a–1)]]  loc. of A[i, j]
 a a  1   k k  1 
 n  2 a  1n  = L0 + k  1n  a     j  1
 2   2 
= n+2n(a–1) – a (a–1) = n + (a–1) [2n–a]
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 4 : Data Structures

2. Stacks & Queues T0  B1 

T1  B2  over flow cases
T2  B3
01. (i). Ans: (a) (ii). Ans: (c)
 T[i] = B [ i +1]
Sol: Given array size m, say 9
Number of stacks n, say 3
(ii) POP = underflow = initial
m
0  i < n T[i] = B[i] = i.   1
n 0 1 2
B –1 2 5
9 
i = 0  T [0] = B[0] = 0    1 = 0–1 = –1 T –1 2 5
3 0 3 6
9 1 4 7
i = 1  T [1] = B[1] = 1    1 = 3–1 = 2 2 5 8
3
9
i = 2  T [2] = B[2] = 2    1 = 2[3]–1 = 5
3 0 1 2 3 4 5 6 7 8

when i = 3  B [3] = m – 1 = 9–1 = 8


(i) Push = overflow = size T[0] T[1] T[2] B[3]
i B[0] B[1] B[2]

0 1 2 3
m–1
B –1 2 5 8 T0  B0

T –1 2 5 T1  B1  under flow cases  T[i] = B[i]
0 3 6 T2  B2
1 4 7
2 5 8

0th stack 1st stack 2nd stack


m-1
0 1 2 3 4 5 6 7 8

B[0] T[1] T[2] B[3]


T[0] B[1] B[2]
over flow

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata




: 5 : CSIT‐Postal Coaching Solutions

02. Ans: (b)
Sol:
Stack Push
operation (10) Push (20) Pop Push (10) Push (20) Pop Pop Pop Push (20) Pop

Stack 10 10 20 10 10 10 10 10 20 10 10 10 20

20 20, 20 20, 20, 20, 20, 20, 20, 20, 20, 10,
Pop list 20 20 10, 10 10, 20
10 10, 10

The sequence of popped out values  20, 20, 10, 10, 20

03. Ans: (d) f (S4) = max {f (0),0} + 2 =2


Sol: f (S3) = max {f (S4),0} + –3 = –1
1 Max {  S3 = push (S4,  3) }
S1 f (S2) = max {f (S3),0} + 2 =2
f (S1) = max {f (S2),0} + (–1) =1
f (S) = max {f (S1),0} + 2 =3
1 Max f (S) = 3
S2
05. Ans: (b)
Sol: Stack insertion order  1,2,3,4,5. The only
An instance of array having two stacks is
possible output sequence 3,4,5,2,1
shown above. Stack1 occupied from 1 to
That occurs when
MAXSIZE and stack2 occupied from
Push (1)
MAXSIZE to 1. Above shown array is
Push (2)
filled completely. So condition for ‘stack
Push (3)
full’ is
Pop (3)  3
Top 1 = Top 2 – 1
( There is no constraint on the order of
04. Ans: (c)
deletion operations)
Sol: Stack S is Push (4)
Pop (4) 3, 4
let S1 S2 S3 S4 Push (5)
2
–1 –1
Pop (5) 3,4,5
2 Pop (2) 3,4,5,2
2 2
–3 –3 –3 –3 Pop (1) 3,4,5,2,1
2 2 2 2 2 Other remaining combinations are not
f (0)= 0 possible
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 6 : Data Structures

06. Ans: 321 08.
Sol: invocation tail (3) Sol: (i) Ans: 41
T (3) = 3 41
fib (7)
T (2) = 2
T (1) = 1 25 15
fib (6) fib (5)
T (0) = stop
Output: 3, 2, 1 15 9
fib (5) fib (4)
9 9
5
07. Ans: 1213121 fib (4) fib (3)
Sol: 5 3
head (x) fib (3) fib (2)
3 1
fib (2) fib (1)
head (x–1) printf (x) head (x–1) 1 1
fib (1) fib (0)
head(3)
(ii) Ans: 67
Sol: Number of calls for evaluating
head(2) printf(x) head(2)
f(n) = 2 f (n+1) – 1
repeat The total number of calls in
head(1) print(x) head(1) Fibonacci (8) = 2 f(9) – 1
Left steps
= 2 × 34 – 1 = 68 – 1 = 67

head print head


(0)
(x) (0) (iii) Ans: 54

1 n 0 1 2 3 4 5 6 7 8 9 10
1 2 3 121
Fib(n) 0 1 1 2 3 5 8 13 21 34 55

Call: 1 1 3 5 9 41 35
Output: 1213121
Add: 2 4

Additions = f (n+1) – 1
f(9) = f(10) –1 = 55 – 1 = 54

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata




: 7 : CSIT‐Postal Coaching Solutions

09. A(1, 11) = A(0, A(1, 10))
Sol: Ackerman(m , n)  = A(0, A(0, A(1, 9)))
n  1 if m  0 = A(0, A(0, 11))

Ackermanm  1,1 if n  0 = A(0, 12)
Ackermanm  1, Ackerman(m, n  1) otherwise = 13

Ackerman(2, 5) = 13
(i) Ans: 9
Sol: Ackerman(2, 3) = A(1, A(2, 2)) = A(1, 7) (iii) Ans: 4
A(2,2) = A(1, A(2,1)) = A(1, 5) = 7 Sol: Ackerman(0, 3) = 4
A(2,1) = A(1, A(2,0)) = A(1, 3) = 5
A(2, 0) = A(1,1) = 3 (iv) Ans: 5
A(1, 1) = A(0, A(1, 0)) = A(0, 2) = 2 +1 = 3 Sol: Ackerman(3, 0) = A(2, 1)
A(1, 0) = A(0, 1) = 2 A(2,1) = A(1, A(2, 0))
A(0, 1) = 1 + 1 = 2 A(2, 0) = A(1, 1)
A(1,3) = A(0,A(1, 2)) = A(0, 4) = 4 + 1 = 5 A(1, 1) = A(0, A(1, 0))
A(1,2) = A(0,A(1, 1)) = A(0, 3)= 3 + 1 = 4 A(1, 0) = A(0, 1) = 2
A(1, 5) = A(0, A(1, 4)) A(1, 1) = A(0, 1) = 3
= A(0, A(0, A(1, 3)))
= A(0, A(0, 5)) A(2, 0) = 3
= A(0, 6) = 6 +1 = 7 A(2, 1) = A(1, 3)
A(1, 7) = A(0, A(1, 6)) A(1, 3) = 5 from (i)
= A(0, A(0, A(1, 5))) A(2, 1) = 5
= A(0, A(0, 7)) Ackerman(3, 0) = 5
= A(0, 8) = 9
Ackerman(2, 3) = 9 10.
Sol: (a) After N +1 calls we have the first move.
(ii) Ans: 13 So after 4 calls we have the first move.
Sol: Ackerman(2, 5) = A(1, A(2, 4)) (b) After total calls –1 calls, we have the
= A(1, A(1, A(2, 3))) last move.
= A(1, A(1, 9)) (c) Total moves 2N – 1 = 7
A(1, 9) = A(0, A(1, 8)) (d) Total invocations = 2N+1 – 1
= A(0, A(0, A(1, 7))) = 24 – 1 = 15
= A(0, A(0, 9))
= A(0, 10) 11. Ans: (b)
= 11 Sol: Post fix expression A B C D + * F /+DE * +
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 8 : Data Structures

12. Ans: (a)
6
Sol: a = – b + c * d/e + f  g  h – i * j 60 60 / 6 = 10 10
Prefix: 15 15
a = – b + c * d/e +( f gh) – i * j
a = – b + *cd/e + ( fgh) – i * j 10
a = – b + /*cde + fgh – *ij 15 15 * 10 = 150 150
a = +–b/*cde + fgh – *ij
a = ++–b/*cdefgh – *ij
8
a = – + + – b/*cde fgh*ij
150 150 – 8 = 142
 = a – + + – b/*cde f gh*ij

13. Ans: (a)


Sol: Infix expression: [a+(b×c)]– d  e  f    142

Postfix expression: abc × +def   – Value of the postfix expression is 142

14. Ans: (a) 16. Ans: (c)


Sol: Sol: (i) ab
3 3 (ii) b
2 2 8 2 2 6 (iii) byz
8 8 8 8 1 1 1 1 (iv) yz
8 2 3  / 2 3 * Output is  yz
23 8/8 23
=8 =6
17. Ans: (b)
 The top two elements are 6, 1
Sol:

15. Ans: (c) ............ 4 0 4 .......


Sol: 10, 5, +, 60, 6, /, *, 8, –
Queue rear Replace with predecessor
5
10 10 10 + 5 = 15
18. Ans: (i) 322 and (iii) 324
Sol:
15
302 318 320 322 324 326 328 336

6 ............. 5 .....
60
15
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 9 : CSIT‐Postal Coaching Solutions

Until first ‘0’ is encountered, stack contains 20. Ans: (b)
Sol: The given recursive procedure simply
9
6 reverses the order of elements in the queue.
5 Because in every invocation the deleted
So 5+6+ 9 = 20 is enqueued in Q2 @ loc element is stored in ‘i’ and when the queue
326 becomes empty.
Until second ‘0’ is encountered, stack Then the insert ( ) function call will be
contains executed from the very last invoked
7 function call. So, the last deleted element
5
will be inserted first and the procedure goes
So 5 + 7 = 12 is enqueued is Q2 @ loc 328 on
Then simply 2 and 6 are pushed in stack

3. Linked Lists
322 324
6 2
01. Ans: (d)
Sol: ii
So the location of 6 and 20 are 322 and 324
a b c d e f g
19. Ans (c)
first i
Sol: Suppose that array contains iii
p

1 2 3 4 5 6 Print = d
Initial configuration: a b c
  02. Ans: (d)
R F
Sol: ii
Delete element
1 2 3 4 5 6 a b c d e f
Dequeue( )  a b
c   i iii
F R first p
Now two added
1 2 3 4 5 6 Print = d
enqueue (x) and
 a b x y
enqueue (y) :
 
F R 03. Ans: (a)
 (R, F) = (4,1) Sol: while (P) or while (P!= Null)
while P is pointing to somebody
Option (c).
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 10 : Data Structures

04. Ans: (d) 08. Ans: (a)
Sol: Recursive routine for ‘Count’ Sol:
Before
a b c d
3 p a b c
f call
Return 1+ 2 q v w x y z
Return 1+ call
Return 1+ 1
call After
Return 1+ 0
a b c
Return 0 call
f
v w x y z
No. of nodes = 4

concatenation of two single linked lists by


05. Ans: (b)
choosing alternative nodes.
Sol: either causes a null pointer dereference or
append list m to the end of list n.
09. Ans: (d)
Sol: Cur. Next = New Node (X, Cur. Next)
06. Ans: (b)
(i) Struct Node * n = Get Node ( ) ;
Sol:
(ii) n  data = X ;
Before
a b c (iii) n  Next = Cur  Next ;
(iv) Cur  Next = n
f
After
c b a 10. Ans: (a)
r q Sol: Linked stack push ( ) = insert front ( )
f
Logic
Initial Do( )
a b c
NULL
f f
Do(a)
a
07. Ans: (b) f
Sol: This is recursive routine for reversing a
Do(b)
SLL.
b a
\

f
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 11 : CSIT‐Postal Coaching Solutions

11. 12. Ans: (b)
Sol: Operation Left most Right most Middle Sol: Inserts to the left of middle node in doubly
Insert 3 3 4 linked list.
Delete 1 1 2

13. Ans: (a)


Sol: Before reverse:

15 5 20 10 25 15 5 20
25 10 5 a 15 10 b 20 15 c 25 20 d 5
5 10 15 20 25
H

t : t Llink
After reverse:

10 25 5 d 20 25 c 15 20 b 10 15 a 5
5 25 20 15 10
H

4. Trees
03. Ans: (c)
Sol:
A
01. Ans: (d)
a
Sol: 1. Traverse the left subtree in postorder.
2. Traverse the right subtree in postorder. B B

3. Process the root node b e


A A
c f
02. Ans: (c)
Sol: 1. Traverse the right sub tree in postorder. B B
d g
2. Traverse the left sub tree in postorder.
3. Process the root node A(t) =a b c d f g e

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata




: 12 : Data Structures

B 06. Ans: (c)
a
Sol: Preorder : A B C D E F G
A A In-order : B D C A F G E
b e
Post-order: D C B G F E A
B B
c f

A A BDC A FGE
d g

B(t) =b d c a e f g
B DC FG E

04. Ans: (b)


Sol: a D C F G

b e D G

c f 07. Ans: 3
Sol: Note: If pre-order is given, along with
d g
terminal node information & all right child
B(t) = egfabcd
information the unique pattern can be
found. If post-order is given along with
05. Ans: 5 terminal information and all left child
Sol: information the unique pattern can be
a a a
a a identified.
b b b
b c b V0
c c c c a

 Totally 5 distinct trees possible V0 b d V0


Note: The number of binary trees can be
V0
formulated with unlabeled nodes are t = null c e V0 f V0
2n
Cn
.
n 1

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata




: 13 : CSIT‐Postal Coaching Solutions

08. Ans: 3 Post 8 9 6 7 4 5 2 3 1
Sol: a In 8 6 9 4 7 2 5 1 3
Height = 4

b c
10. (a) Ans: 19

d e f g Sol: Leaf nodes (L) = Total nodes – internal nodes


L = In+1-I
09. Ans: 4 L = I(n-1)+1
Sol: Post 8 9 6 7 4 5 2 3 1 L = 20
In 8 6 9 4 7 2 5 1 3
I=?
20 = I(2 – 1) +1
In 8 6 9 4 7 2 5 1 3
20 = I + 1

1 I = 19

86947 2 5 3
10. (b) Ans: 199
1 Sol: L = I(n-1)+1

3 L = 200
2
200 = I + 1
5 I = 199
869 4 7
1
11. Ans: (b)
2 3
Sol:
L=0 1
4 5

7 L=1 2
8 6 9
L=2
L= 0 4
1
L=3
2 3 L= 1 8

4 5 L= 2 15
Minimum = 3, Maximum = 14
6 7
L= 3
8 9
L= 4
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 14 : Data Structures

12. Ans: 2 & 1 14. Ans: (d)
Sol: Sol: v 0 3
xL 0+1+2 a
xR
2 a
3

1
xL v 0 v0 2
xR xL xR
0 b 1 2 1 0+0+1 b 0+1+1 d
d

v 0 v
xL t = Null 0 v01
0 1 v01
t=NULL c e
xR f
xR
0 c 0
e 1 xR xL 0
0 g
15. Ans: 3
Sol: a (b, c (e (f, g, h)),d)
xL
0 Parent of f, g, h is e. i.e. internal parenthesis
f 0
has children of parent which is out of
13. Ans: (a) parenthesis.
Sol: a III
Before Swap After swap
c b c d
c II

e I
b b
c c f g h 0

e e
16. Ans: 4
Sol: Given are 3 trees
d d
1 A p
e e

a 2 4 B C q r
a 3

b d d b
To get the converted binary tree of these
c e e c given trees
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 15 : CSIT‐Postal Coaching Solutions

 parent is not given we have to assume 19. Ans: 6
virtual parent Sol: Expanded as
Among siblings ((1+1) – (0 – 1)) + ((1 – 0) + (1+1))
 Keep the leftmost as it is, =3+3=6
 Cut and connect right siblings as shown
in diagram 20. Ans: –2
V Sol: (0 + 0) – (1 – 0) + (0 – 1) + (0 + 0)
= –1 + (–1) = –2

1 A p
21. Ans: 4
Sol: 40

2 3 4 B C q r
20 60
1

2 A 10 30 50 70
P
3 B 22. Ans: (b)
C
q Sol: Preorder = 12, 8, 6, 2, 7, 9, 10, 16, 15, 19,
4
r 17, 20
17. Ans: (d)
Inorder = 2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20
Sol: Count the number of trees in forest.

18. Ans: (b) 2, 6, 7, 8, 9, 10 15, 16 ,17,19,20


Sol: =

a + 2, 6, 7 9, 10 15
17, 19 ,20

+ 2 7 10 17 20

12
+
* 16
↑ 8
/ i j
– 9 15 19
f ↑ 6
* e
b g 2 7 10 17 20
h
c d = 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 16 : Data Structures

23. Ans: 4 SUN
(b) 5 3 1 2 6 4 8 7
Sol:
5
MON TUE

3 6
FRI
SAT THU WED

1 4 Not possible
24. Ans: 67
Sol: 71, 65, 84, 69, 67, 83 insert into empty
2
binary search tree

71 71 71
(c) 5 3 2 4 1 6 7 8

65 65 84 65 84 5

69 3

71 71 Not possible
2 4

65 84 65 84
1

69 69 83
(d) 5 3 1 2 4 7 6 8

67 67 5

 Element in the lowest level is 67


3 7
25. Ans: 30
1 4 6 8
26. Ans: (d)
5
Sol: (a) 5 3 1 2 4 7 8 6 2

3 7
27. Ans: 15
1 8 Sol: 1. Jump right
4
2. Go on descend left
2 6

Not possible
IN: not sorted order
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 17 : CSIT‐Postal Coaching Solutions

28. Ans: 88 +2
26 –1
 1 LH0 21

Sol: NH  
L
2 L  H 1 –1 0
min
1  NH  1  NH  2 L  H  1 30
 9 0 9 26 –2
N(H) = 1 + N(H-1) + N (H-2) R R
N(2) = 1 + N(1) + N(0) 4
21 +1 14 30 +1
=1+2+1 4 0 0
0 L
=4
28
N(3) = 1+N(2)+N(1) 14
0
=1+4+2 0
+1
=7
21
N(8) = 1 + N(7) + N(6)
–1
= 1 + 54 + 33 = 88
9 28 0
H 0 1 2 3 4 5 6 7 8
–1
N(H) 1 2 4 7 12 20 33 54 88 4 0
14 26 0 0 30

29. Ans: 14 18
+1
Sol: 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7 0
21
0 -1 -2
0 –2
21 21 21
26 9 28 0
R R
–1
26 +1
26 4 26 30
21 30 15
0 R 0
0 0
0 0 +1 L
30
14 8
0 21 +2
+1 26 0
26
10 +1
+2 28 0
+1 0 14
21 30
21 30 +1 –1
L 26 30
0 +1 9 15
0
9 +1 0
9
0 4 10 18
L
0 0
4
2
0
0
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 18 : Data Structures

30. Ans: 28
14
Sol:
Delete 2, 3
9 21 0
2 14
4 15 28
10 +2 0
L A
–1 9 21
30
2 18 26 L 0
–1 15
R 28
–1 4
3
R
0
7 18 26 30
14 0 0 0 0

+2
21 Delete 10 Delete 18, 4, 9
9 14
21
L

1 3 15 28 21
10 7
R 14 28

4 – 30 15
2 18 26 28
1 0 4
0 0 0 9 7 30
15 26
7 30
18 26
0

14 Delete 14 Delete 7

21
–1 21 –2
4 21
+1 0 0 0
15 28 15 28
3 15 28
9 R
7 26 30
30 0 26 30
2 7 18 26 0
10 0 0
Delete 15
28

21 30

26
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 19 : CSIT‐Postal Coaching Solutions

5. Graphs Not pushed vertices are → V6, V7, V5
Vertices are not pushed in more than once
→ V1, V4, V8
01. Ans: (b)
Sol: V8 is pushed in for two times
04. Ans: (a)
Input 1 Sol: (a) invalid (b) valid
(c) valid (d) valid
2 3

4 5 6 7 (a) 1 (b) 4
2
2 3
8

5 6 1 5 6
Output: 1, 2, 4, , 5 step back 7
7
, 6, 3, 7.
8 8
V8 : Two times Step back only when already
explored vertices are there

02. Ans: 2 V8
Sol: Sequence of exploration V8 (c) 1 (d) 1
2
V5→V2→V1→V3→ V6 3
V3 2 3
V6→V8→V7→V4
V1 4 5 6
Sequence of stack contents 6 7 7
V2 4 5
Not pushed vertices are  V4, V7 V5 8
Vertices are pushed in more than once 8
→ V1, V2, V3, V6, V5
05. Ans:(c)
03. Ans: 3 Sol: (a) valid (b) valid
Sol: Sequence of exploration
V8→V4→V2→V1→V3→V6→V7→V5 (c) invalid (d) valid
Sequence of stack contents
(a) 1 (b) 1
V2
V3
V3 2 2
3 3
V1
5
V2 4 6 4
5
6 7
7
V4
8 8
V8
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 20 : Data Structures

(c) (d) 1 08. Ans: (d)
4 Sol:
5
2 3 V1
8
Step back only when 4 6 V2 V3
5 7
already explored vertices
are there
8 V4 V5 V6 V7

06. Ans: 19
Sol: V8
19 14 13 7 6 Traversal: BFS

V1
18 15 12 8 5 2 1

17 16 11 10 9 4 3 V2 V3

Maximum possible recursion depth = 19


V4 V5 V6 V7
(The dashed link ‘nodes’ are explored while
stepping backward.)
V8
07. Ans: 8
Sol: 6 7 8 g f e queue V2 V3
Dequeue
5 4 3 b c d 1 queue V3 V4 V5
start Dequeue
11 1 a
queue V4 V5 V6 V7
Dequeue
Minimum possible recursion depth = 8
(The dashed link ‘nodes’ are explored while queue V5 V6 V7 V8
stepping backward.)
09. Ans: (c)
Sol: (a) valid (b) valid
(c) invalid (d) valid

10. Ans: (d)


ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata


: 21 : CSIT‐Postal Coaching Solutions

04. Ans: (b)
6. Hashing
Sol: Hash function
h (x) = (3x + 4) % 7
01. Ans: (d)
h (1) = (3+4) % 7 = 0
Sol: 1 to 1000
h (3) = (9 + 4) % 7 = 6
h (8) = (24 +4) % 7 = 0
02. Ans: (a) h (10) = (30 +4) % 7 = 6
Sol: Assume Linear probing for collision
0 resolution
Maximum
1 28 19 10
The table will be like
2 20

3
12 1 8 10 3
4 0 1 2 3 4 5 6
5 5
05. Ans: (d)
6 15 33
Sol: After inserting all keys, the hash table is
Minimum
7 Key 43 36 92 87 11 4 71 13 14
17
Loc 10 3 4 10 0 4 5 2 3
8

0 1 2 3 4 5 6 7 8 9 10
87 11 13 36 92 4 71 14 43
3 111 2 1
 1 (average)
9
Last element is stored at the position 7

03. Ans: 80
06. Ans: (c)
Sol: Slots = 25
Sol: Resultant hash table.
Elements = 2000
In linear probing, we search hash table
elements
Load factor = sequentially starting from the original
slots
2000
location. If a location is occupied, we check
= = 80
25 the next location. We wrap around from the
last table location to the first table location
if necessary.

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata




: 22 : Data Structures

07. Ans: (c) 08. Ans: (c)
Sol: Sol: Case (I): To store 52
    Variable part Fixed part
A B C D 42 23 34 52 46 33
0 42 34 23 52 46 33
1 23 42 34 52 46 33
2 42 42 42 42 23 34 42 52 46 33
3 52 23 23 23 34 42 23 52 46 33
4 34 34 34 23 34 23 42 52 46 33
5 23 52 52 34 3! = 6
6 46 33 46 46 Case (II): To store 33
7 33 46 33 52
Variable part Fixed part
8
42 23 34 52 33
9 42 23 34 46

4! =24
Since 46 is not getting collided with any
other key, it can be moved to the variable
part.
Case (I) & Case (II) are mutually exclusive
Case (I) + Case (II) = 24 + 6 = 30
Total 30 different insertion sequences

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

Potrebbero piacerti anche