diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py new file mode 100644 index 00000000..edaea7a4 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..c21f9762 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_78_lowest_common_ancestor_of_binary_tree.py b/src/my_project/interviews/top_150_questions_round_22/ex_78_lowest_common_ancestor_of_binary_tree.py new file mode 100644 index 00000000..82a7fd31 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_78_lowest_common_ancestor_of_binary_tree.py @@ -0,0 +1,29 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def lowestCommonAncestor(self, root:TreeNode, p, q): + """ + :type root: TreeNode + :type p: TreeNode + :type q: TreeNode + :rtype: TreeNode + """ + + if not root: + return + elif root.val == p.val or root.val == q.val: + return root + + l = self.lowestCommonAncestor(root.left,p,q) + r = self.lowestCommonAncestor(root.right,p,q) + if l and r: + return root + else: + return l or r \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/test_78_lowest_common_ancestor_of_binary_tree_round_22.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/test_78_lowest_common_ancestor_of_binary_tree_round_22.ts new file mode 100644 index 00000000..858a96c9 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/test_78_lowest_common_ancestor_of_binary_tree_round_22.ts @@ -0,0 +1,16 @@ +import { TreeNode } from './TreeNode'; + +function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { + if (root === null || root === p || root === q) { + return root; + } + + const left = lowestCommonAncestor(root.left, p, q); + const right = lowestCommonAncestor(root.right, p, q); + + if (left !== null && right !== null) { + return root; + } + + return left !== null ? left : right; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_78_lowest_common_ancestor_of_binary_tree_round_22.py b/tests/test_150_questions_round_22/test_78_lowest_common_ancestor_of_binary_tree_round_22.py new file mode 100644 index 00000000..6650480d --- /dev/null +++ b/tests/test_150_questions_round_22/test_78_lowest_common_ancestor_of_binary_tree_round_22.py @@ -0,0 +1,71 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_78_lowest_common_ancestor_of_binary_tree import Solution, TreeNode + + +class LowestCommonAncestorBinaryTreeTestCase(unittest.TestCase): + + def build_tree(self, values: List[Optional[int]]) -> Optional[TreeNode]: + """Build a binary tree from level-order array representation.""" + if not values or values[0] is None: + return None + + root = TreeNode(values[0]) + queue = [root] + i = 1 + + while queue and i < len(values): + node = queue.pop(0) + + # Left child + if i < len(values) and values[i] is not None: + node.left = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + # Right child + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root + + def find_node(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + """Find a node with given value in the tree.""" + if not root: + return None + if root.val == val: + return root + left = self.find_node(root.left, val) + if left: + return left + return self.find_node(root.right, val) + + def test_example_1(self): + """Test case: LCA of nodes 5 and 1 is 3.""" + solution = Solution() + tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]) + p = self.find_node(tree, 5) + q = self.find_node(tree, 1) + output = solution.lowestCommonAncestor(root=tree, p=p, q=q) + self.assertEqual(3, output.val) + + def test_example_2(self): + """Test case: LCA of nodes 5 and 4 is 5 (node can be ancestor of itself).""" + solution = Solution() + tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]) + p = self.find_node(tree, 5) + q = self.find_node(tree, 4) + output = solution.lowestCommonAncestor(root=tree, p=p, q=q) + self.assertEqual(5, output.val) + + def test_example_3(self): + """Test case: LCA of nodes 1 and 2 is 1.""" + solution = Solution() + tree = self.build_tree([1, 2]) + p = self.find_node(tree, 1) + q = self.find_node(tree, 2) + output = solution.lowestCommonAncestor(root=tree, p=p, q=q) + self.assertEqual(1, output.val) \ No newline at end of file