Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Structures
(Solutions for Vol‐1_Classroom Practice Questions)
: 2 : Data Structures
Retrieval: 08. Ans: (b)
loc of Ai, 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 Ai, j L 0 0 i ib
4 3 or
3 2 (j – jlb)
2 1 i.e.,
except 1 row st
loc. of Ai, j L 0 0
ith (i–1) i 2
or
loc. of Ai, j L 0 3i 4 j i 1
j 1
L 0 2i j 3
If i – j = 0
loc. of Ai, 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 Ai, j L 0 2n 1
Retrieval: i 1
loc. of A[i, j] = L0 + 2D + 1D or
L 0 j 1cols i ib 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 Ai, 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
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 55
[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 1n = L0 + k 1n 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 T0 B1
T1 B2 over flow cases
T2 B3
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
0 1 2 3
m–1
B –1 2 5 8 T0 B0
T –1 2 5 T1 B1 under flow cases T[i] = B[i]
0 3 6 T2 B2
1 4 7
2 5 8
: 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
: 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
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
: 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))
Ackermanm 1,1 if n 0 = A(0, 12)
Ackermanm 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 + ( fgh) – i * j 10
a = – b + /*cde + fgh – *ij 15 15 * 10 = 150 150
a = +–b/*cde + fgh – *ij
a = ++–b/*cdefgh – *ij
8
a = – + + – b/*cde fgh*ij
150 150 – 8 = 142
= a – + + – b/*cde f gh*ij
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
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
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
: 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
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
: 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
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.
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
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 LH0 21
Sol: NH
L
2 L H 1 –1 0
min
1 NH 1 NH 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
: 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 111 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.
: 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