-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgistFiles
More file actions
318 lines (243 loc) · 10.6 KB
/
Copy pathgistFiles
File metadata and controls
318 lines (243 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
FAQs---------------------------------------------------------------------------
Q1 Can we use a stack instead of Queue?
ANS No, because in that case order of elements gets changed as expected.
Q2 Why are we taking a 'size' variable, instead of using 'queue.size()' method?
ANS Because in inner while loop, further elements will also get added in the queue due to which the value of this method gets changed continuously but we want to process only those elements that were present initially, in one go.
Q3 What are we storing in arraylist of arraylist?
ANS In this data structure each inner arraylist is representing a diagonal of tree.
Q4 Can we use another approach to solve this problem?
ANS Yes, we can solve this problem using recursive approach too.
Q5 What does a an empty queue signifies?
ANS It signifies that all elements of tree has been processed.
HINTS-------------------------------------------------------------------------
1. All the nodes that are lying on the line of slope -1 lies in the same diagonal.
2. Now, create an arrayList of arraylist 'ans' to store the different diagonals in each arraylist.
3. At a node, it's right child will lie in the same diagonal as of node while left child will act as a initial node of some component in next diagonal.
4. Then we perform the diagonal order traversal with the help of an extra queue i.e. while we are at 1 diagonal, prepare next diagonal correspondingly.
5. Now, each time while accessing the node we add its left child in the queue i.e. next diagonal, and keep moving to the right child until reaches a NULL value and add it in the 'smallAns' arraylist i.e. current diagonal.
MCQ------------------------------------------------------------------------------
Q1 What is the time complexity of used approach?
A) O(N)
B) O(N^2)
C) O(N LOG N)
D) O(LOG N)
ANS = A
Q2 2
/ \
/ \
7 15
/ \ / \
3 6 14 9
\ \
4 13
List all the nodes belonging to same DIAGONAL level.
A) {3},{7,4},{2,6,14},{15},{9},{13}
B) {2},{7,15},{3,6,14,9},{4,13}
C) {2,15,9,13},{7,6,14},{3,4}
D) None of the above
ANS = C
Q3 Queue implements:
A FIFO(First In First Out)
B LIFO(Last In First Out)
C both (a) and (b)
D none of the above
ANS = A
Q4 2
/ \
/ \
7 15
/ \ / \
3 6 14 9
\ \
4 13
What will be the number of diagonals present in above tree?
A) 2
B) 3
C) 4
D) None of the above
ANS = B
Q5 "While we are at one diagonal then, in order to prepare next diagonal we add ______ nodes of all the nodes in queue." Complete the statement.
A) right
B) same
C) left
D) Wrong statement
ANS = C
___________________________________________________________________________________________________QUES 2
FAQs---------------------------------------------------------------------------
Q1 Can we use a stack instead of Queue?
ANS No, because in that case order of elements gets changed as expected.
Q2 Why are we taking a 'size' variable, instead of using 'queue.size()' method?
ANS Because in inner while loop, further elements will also get added in the queue due to which the value of this method gets changed continuously but we want to process only those elements that were present initially, in one go.
Q3 What are we storing in arraylist of arraylist?
ANS In this data structure each inner arraylist is representing a diagonal(anti-clockwise) of tree.
Q4 Can we use another approach to solve this problem?
ANS Yes, we can solve this problem using recursive approach too.
Q5 What does a an empty queue signifies?
ANS It signifies that all elements of tree has been processed.
HINTS-------------------------------------------------------------------------
1. All the nodes that are lying on the line of slope +1 lies in the same diagonal.
2. Now, create an arrayList of arraylist 'ans' to store the different diagonals in each arraylist.
3. At a node, it's left child will lie in the same diagonal as of node while right child will act as a initial node of some component in next diagonal.
4. Then we perform the anti-clockwise diagonal order traversal with the help of an extra queue i.e. while we are at 1 diagonal, prepare next diagonal correspondingly.
5. Now, each time while accessing the node we add its right child in the queue i.e. next diagonal, and keep moving to the left child until reaches a NULL value and add it in the 'smallAns' arraylist i.e. current diagonal.
MCQ------------------------------------------------------------------------------
Q1 What is the time complexity of used approach?
A) O(N)
B) O(N^2)
C) O(N LOG N)
D) O(LOG N)
ANS = A
Q2 2
/ \
/ \
7 15
/ \ / \
3 6 14 9
\ \
4 13
List all the nodes belonging to same DIAGONAL(anti-clockwise) level.
A) {3},{7,4},{2,6,14},{15},{9},{13}
B) {2,15,9,13},{7,6,14},{3,4}
C) {2,7,3},{15,14,6,4},{9},{13}
D) None of the above
ANS = C
Q3 2
/ \
/ \
7 15
/ \ / \
3 6 14 9
\ \
4 13
What will be the number of components present in above tree's diagonal 2?
A) 1
B) 4
C) 2
D) 3
ANS = D
Q4 2
/ \
/ \
7 15
/ \ / \
3 6 14 9
\ \
4 13
What will be the number of diagonals present in above tree?
A) 2
B) 3
C) 4
D) None of the above
ANS = C
Q5 "While we are at one diagonal then, in order to prepare next diagonal we add ______ nodes of all the nodes in queue." Complete the statement.
A) right
B) same
C) left
D) Wrong statement
ANS = A
_______________________________________________________________________________QUES 3
FAQs--------------------------------------------
Q1 How we got the formula for a node label's complement?
ANS Since, for a node (n'-start)=(end-n) => n'=end+start-n. And, on the basis of observation [start = (2^level no) = llv(last level value)] and [end = 2*llv-1]. Therefore, n'=(2*llv-1)+(llv)-n = 3*llv-1-n where, n is label given.
Q2 What is the llv variable representing?
ANS It represents the last level's value on which our node with given label lies and level value is given as 2^(level number).
Q3 What is the significance of 'llv /= 2;'?
ANS In upper while loop, llv gets updated 1 time extra. So, to get last level value we update llv by llv/2 using this statement.
Q4 What is the significance of 'int par = comp / 2;'?
ANS It is basically calculating the parent node's label for the calculated complementary node.
Q5 Are we creating a tree?
ANS No, actually no tree has been created. We are just working with node labels and formulas.
HINTS-------------------------------------------
1. No tree formation is required.
2. Based on the label of the current node, find what the label must be for the parent of that node.
3. Use the fact that in normal binary tree, parent is calulated as node_label/2.
4. In zig zag binary tree, directions has been swapped. So, for a node calculate node's complement on the same level and find parent for that.
5. To find node's complement drive a formula using the fact that n and n' will be equidistant from the start and end of that level respectively.
MCQs--------------------------------------------
Q1 If we are at a node'n', then what will be the label of it's parent node in a normal complete binary tree?
A) (2^n)
B) (n/2)
C) (n+1)/2
D) (n-1)/2
ANS = B
Q2 What will be the time complexity of this approach with n=label?
A) O(LOG N)
B) O(N)
C) O(N LOG N)
D) O(N^2)
ANS = A
Q3 What will be the space complexity of this approach with n=label?
A) O(LOG N)
B) O(N)
C) O(N LOG N)
D) O(N^2)
ANS = A
Q4 Is binary tree symmetric?
A) NO
B) YES
C) MAY BE
ANS = B
Q5 Time complexity of Collections.reverse() method?
A) LOGARITHMIC
B) QUADRATIC
C) LINEAR
D) EXPONENTIAL
ANS = C
Ques : Word Search li_____________________________________________________________________________________
FAQs---------------------------------------------
Q1 Explain the approach used.
ANS To find all the strings in crossword, we iterate over all characters in crossword and for each character check whether any string to be found has initial as this character. Instead of picking a particular string and checking its existence in whole crossword. This will result in reduced time complexity.
Q2 What is the significance of node.str?
ANS It marks the ending of a complete string.
Q3 What is the significance of node.count?
ANS It represents how many strings are using that node.
Q4 Name the algorithm technique used.
ANS Backtracking
Q5 What is the use of checking 'if (child.str != null)' condition?
ANS If we got a node whose str data member value is not null, it represents the end of a string means a complete string has been reached. So, add it in ans arraylist.
MCQ---------------------------------------------
Q1) Trie is also known as _________
a) Digital Tree
b) Treap
c) Binomial Tree
d) 2-3 Tree
ANS = A
Q2) Which of the following is the efficient data structure for searching words in dictionaries?
a) BST
b) Linked List
c) Balancded BST
d) Trie
ANS = D
Q3) Following code snippet is the function to insert a string in a trie. Find the missing line.
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (curr.childs[ch - 'a'] == null) {
curr.childs[ch - 'a'] = new Node();
curr.count++;
}
______________________________________
}
curr.str = s;
a) curr = curr.childs[(ch - 'a')++];
b) curr = curr.childs[ch];
c) curr = curr.childs[ch - 'a'];
d) Nothing is missing
ANS = C
Q4) Which of the following is not true?
a) Trie requires less storage space than hashing
b) Trie allows listing of all the words with same prefix
c) Tries are collision free
d) Trie is also known as prefix tree
ANS = A
Q5) What can be the maximum depth of the trie with n strings and m as the maximum sting the length?
a) log2n
b) log2m
c) n
d) m
ANS = D
HINTS------------------------------------------
1) Try to use Trie data structure for efficient searching.
2) Make a trie for all the strings need to be searched in crossword puzzle.
3) Now, iterate over all the characters in crossword and check for any word with character as its initial.
4) Proceed for matching characters in order to match further string.
5) Use backtracking to check rest of the characters of word with the help of an visited array.