Skip to content

Commit 622d9cb

Browse files
committed
DSA Assignment 1
1 parent 359ca59 commit 622d9cb

File tree

1 file changed

+218
-26
lines changed

1 file changed

+218
-26
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 218 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": null,
24+
"execution_count": 202,
2525
"metadata": {},
26-
"outputs": [],
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"1\n"
32+
]
33+
}
34+
],
2735
"source": [
28-
"print((hash('your first name') % 3) + 1)"
36+
"print((hash('sarita') % 3) + 1)"
2937
]
3038
},
3139
{
@@ -72,20 +80,113 @@
7280
},
7381
{
7482
"cell_type": "code",
75-
"execution_count": null,
83+
"execution_count": 203,
7684
"metadata": {},
77-
"outputs": [],
85+
"outputs": [
86+
{
87+
"ename": "IndentationError",
88+
"evalue": "expected an indented block (2850872121.py, line 8)",
89+
"output_type": "error",
90+
"traceback": [
91+
"\u001b[1;36m Cell \u001b[1;32mIn[203], line 8\u001b[1;36m\u001b[0m\n\u001b[1;33m # TODO\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
92+
]
93+
}
94+
],
7895
"source": [
7996
"# Definition for a binary tree node.\n",
8097
"# class TreeNode(object):\n",
8198
"# def __init__(self, val = 0, left = None, right = None):\n",
8299
"# self.val = val\n",
83100
"# self.left = left\n",
84101
"# self.right = right\n",
85-
"def is_symmetric(root: TreeNode) -> int:\n",
102+
"def is_duplicate(root: TreeNode) -> int:\n",
86103
" # TODO"
87104
]
88105
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {},
110+
"outputs": [
111+
{
112+
"data": {
113+
"text/plain": [
114+
"2"
115+
]
116+
},
117+
"execution_count": 195,
118+
"metadata": {},
119+
"output_type": "execute_result"
120+
}
121+
],
122+
"source": [
123+
"#Example1\n",
124+
"root = TreeNode(1)\n",
125+
"root.left = TreeNode(2)\n",
126+
"root.right = TreeNode(2)\n",
127+
"root.left.left = TreeNode(3)\n",
128+
"root.left.right = TreeNode(5)\n",
129+
"root.right.left = TreeNode(6)\n",
130+
"root.right.right = TreeNode(7)\n",
131+
"# print_in_order(root)\n",
132+
"is_duplicate(root)"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"metadata": {},
139+
"outputs": [
140+
{
141+
"data": {
142+
"text/plain": [
143+
"10"
144+
]
145+
},
146+
"execution_count": 196,
147+
"metadata": {},
148+
"output_type": "execute_result"
149+
}
150+
],
151+
"source": [
152+
"#Example2\n",
153+
"root = TreeNode(1)\n",
154+
"root.left = TreeNode(10)\n",
155+
"root.right = TreeNode(2)\n",
156+
"root.left.left = TreeNode(3)\n",
157+
"root.left.right = TreeNode(10)\n",
158+
"root.right.left = TreeNode(12)\n",
159+
"root.right.right = TreeNode(12)\n",
160+
"# print_in_order(root)\n",
161+
"is_duplicate(root)"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [
169+
{
170+
"data": {
171+
"text/plain": [
172+
"-1"
173+
]
174+
},
175+
"execution_count": 197,
176+
"metadata": {},
177+
"output_type": "execute_result"
178+
}
179+
],
180+
"source": [
181+
"#Example3\n",
182+
"root = TreeNode(10)\n",
183+
"root.left = TreeNode(9)\n",
184+
"root.right = TreeNode(7)\n",
185+
"root.left.left = TreeNode(8)\n",
186+
"# print_in_order(root)\n",
187+
"is_duplicate(root)"
188+
]
189+
},
89190
{
90191
"cell_type": "markdown",
91192
"metadata": {},
@@ -193,12 +294,13 @@
193294
]
194295
},
195296
{
196-
"cell_type": "code",
197-
"execution_count": null,
297+
"cell_type": "markdown",
198298
"metadata": {},
199-
"outputs": [],
200299
"source": [
201-
"# Your answer here"
300+
"# Your answer here\n",
301+
"We need to traverse a binary tree and find any duplicate node values. \n",
302+
" In case of multiple duplicates, the duplicate values closest to the root(Would be the first found duplicate in BFS) should be returned.\n",
303+
"So, essentially we need to perform the breadth-first serach and return the first found duplicate value"
202304
]
203305
},
204306
{
@@ -213,9 +315,62 @@
213315
"cell_type": "code",
214316
"execution_count": null,
215317
"metadata": {},
216-
"outputs": [],
318+
"outputs": [
319+
{
320+
"data": {
321+
"text/plain": [
322+
"2"
323+
]
324+
},
325+
"execution_count": 198,
326+
"metadata": {},
327+
"output_type": "execute_result"
328+
}
329+
],
330+
"source": [
331+
"# Your answer here\n",
332+
"# Example 1 Input: root = [10, 7, 2, 7, 3, 5, 2], output = 2\n",
333+
"root = TreeNode(4)\n",
334+
"root.left = TreeNode(7)\n",
335+
"root.right = TreeNode(2)\n",
336+
"root.left.left = TreeNode(11)\n",
337+
"root.left.right = TreeNode(12)\n",
338+
"root.right.left = TreeNode(5)\n",
339+
"root.right.left.right = TreeNode(2)\n",
340+
"root.right.left.left = TreeNode(14)\n",
341+
"# print_in_order(root)\n",
342+
"is_duplicate(root)"
343+
]
344+
},
345+
{
346+
"cell_type": "code",
347+
"execution_count": null,
348+
"metadata": {},
349+
"outputs": [
350+
{
351+
"data": {
352+
"text/plain": [
353+
"10"
354+
]
355+
},
356+
"execution_count": 199,
357+
"metadata": {},
358+
"output_type": "execute_result"
359+
}
360+
],
217361
"source": [
218-
"# Your answer here"
362+
"# Example 2 Input: root = [1, 5, 7, 2, 2, 3, 7] Output =10\n",
363+
"root = TreeNode(10)\n",
364+
"root.left = TreeNode(7)\n",
365+
"root.right = TreeNode(2)\n",
366+
"root.left.left = TreeNode(1)\n",
367+
"root.left.right = TreeNode(3)\n",
368+
"root.right.left = TreeNode(5)\n",
369+
"root.right.right = TreeNode(18)\n",
370+
"root.right.left.left = TreeNode(12)\n",
371+
"root.right.right.left = TreeNode(10)\n",
372+
"# print_in_order(root)\n",
373+
"is_duplicate(root)"
219374
]
220375
},
221376
{
@@ -232,7 +387,30 @@
232387
"metadata": {},
233388
"outputs": [],
234389
"source": [
235-
"# Your answer here"
390+
"# Your answer here\n",
391+
"# Definition for a binary tree node.\n",
392+
"class TreeNode(object):\n",
393+
" def __init__(self, val = 0, left = None, right = None):\n",
394+
" self.val = val\n",
395+
" self.left = left\n",
396+
" self.right = right\n",
397+
"\n",
398+
"def is_duplicate(root: TreeNode) -> int:\n",
399+
" if not root:\n",
400+
" return -1\n",
401+
" queue = deque([root])\n",
402+
" seen_values = set() \n",
403+
" while queue:\n",
404+
" node = queue.popleft()\n",
405+
" if node.val in seen_values:\n",
406+
" # if value is seen already return as duplicate \n",
407+
" return node.val\n",
408+
" seen_values.add(node.val)\n",
409+
" if node.left:\n",
410+
" queue.append(node.left)\n",
411+
" if node.right:\n",
412+
" queue.append(node.right)\n",
413+
" return -1"
236414
]
237415
},
238416
{
@@ -244,12 +422,12 @@
244422
]
245423
},
246424
{
247-
"cell_type": "code",
248-
"execution_count": null,
425+
"cell_type": "markdown",
249426
"metadata": {},
250-
"outputs": [],
251427
"source": [
252-
"# Your answer here"
428+
"# Your answer here\n",
429+
"While traversing the nodes, we maintain a set of unique values that we come across. If we encounter a duplicate, we return that value. In case it is a new value, we add it to the set.\n",
430+
"The deque function helps us to create a queue which we then access from the left, to process the node values sequentially."
253431
]
254432
},
255433
{
@@ -261,12 +439,13 @@
261439
]
262440
},
263441
{
264-
"cell_type": "code",
265-
"execution_count": null,
442+
"cell_type": "markdown",
266443
"metadata": {},
267-
"outputs": [],
268444
"source": [
269-
"# Your answer here"
445+
"# Your answer here\n",
446+
"Time complexity - In our solution code, each node is visited once , creating a time complexity of O(n)\n",
447+
"\n",
448+
"Space Complexity - The worst case for a highly skewed tree would have a space complexity of O(n)"
270449
]
271450
},
272451
{
@@ -278,12 +457,25 @@
278457
]
279458
},
280459
{
281-
"cell_type": "code",
282-
"execution_count": null,
460+
"cell_type": "markdown",
283461
"metadata": {},
284-
"outputs": [],
285462
"source": [
286-
"# Your answer here"
463+
"# Your answer here\n",
464+
"I was thinking of using a recursive function\n",
465+
"\n",
466+
"The function would need two parameters, the node value and the seen list to keep track of the already visited values\n",
467+
"\n",
468+
"Under a function is_duplicate, do below:\n",
469+
"\n",
470+
"create an empty set to store unique values\n",
471+
"\n",
472+
"Evaluate the first node\n",
473+
"if the node value is encountered the first time, add it to the set\n",
474+
"otherwise, when encountering a value that already exists in set, return the value as a duplicate(and exit the function)\n",
475+
"\n",
476+
"continue evaluating the left and right branches and call the is_duplicate function recursively with the left node value and the set in it's current state\n",
477+
"\n",
478+
"\n"
287479
]
288480
},
289481
{
@@ -345,7 +537,7 @@
345537
"name": "python",
346538
"nbconvert_exporter": "python",
347539
"pygments_lexer": "ipython3",
348-
"version": "3.11.7"
540+
"version": "3.9.15"
349541
}
350542
},
351543
"nbformat": 4,

0 commit comments

Comments
 (0)