diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py new file mode 100644 index 00000000..0f997fd2 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py @@ -0,0 +1,19 @@ +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_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py new file mode 100644 index 00000000..d2f5d34b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py @@ -0,0 +1,23 @@ +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 characters + 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 + diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_81_binary_tree_level_order_traversal.py b/src/my_project/interviews/top_150_questions_round_22/ex_81_binary_tree_level_order_traversal.py new file mode 100644 index 00000000..f05adee2 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_81_binary_tree_level_order_traversal.py @@ -0,0 +1,31 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + if not root: + return [] + + result = [] + queue = deque([root]) + + while queue: + level_size = len(queue) + level = [] + for _ in range(level_size): + node = queue.popleft() + level.append(node.val) + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + result.append(level) + + return result \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_81_binary_tree_level_order_traversal.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_81_binary_tree_level_order_traversal.ts new file mode 100644 index 00000000..076bd9d2 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_81_binary_tree_level_order_traversal.ts @@ -0,0 +1,31 @@ +import { TreeNode } from './TreeNode'; + +function levelOrder(root: TreeNode | null): number[][] { + if (!root) { + return []; + } + + const result: number[][] = []; + const queue: TreeNode[] = [root]; + + while (queue.length > 0) { + const levelSize = queue.length; + const level: number[] = []; + + for (let i = 0; i < levelSize; i++) { + const node = queue.shift()!; + level.push(node.val); + + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } + } + + result.push(level); + } + + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_81_binary_tree_level_order_traversal_round_22.py b/tests/test_150_questions_round_22/test_81_binary_tree_level_order_traversal_round_22.py new file mode 100644 index 00000000..64a67fc1 --- /dev/null +++ b/tests/test_150_questions_round_22/test_81_binary_tree_level_order_traversal_round_22.py @@ -0,0 +1,59 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_81_binary_tree_level_order_traversal import Solution, TreeNode + + +class BinaryTreeLevelOrderTraversalTestCase(unittest.TestCase): + + def create_binary_tree(self, values): + """ + Helper function to create a binary tree from a list of values (level-order). + + :param values: List of node values (None represents null nodes) + :return: Root of the binary tree + """ + if not values: + return None + + root = TreeNode(values[0]) + queue = [root] + i = 1 + + while queue and i < len(values): + node = queue.pop(0) + + if i < len(values) and values[i] is not None: + node.left = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root + + def test_example_1(self): + # Example 1: Input: root = [3,9,20,null,null,15,7] + # Output: [[3],[9,20],[15,7]] + solution = Solution() + root = self.create_binary_tree([3, 9, 20, None, None, 15, 7]) + result = solution.levelOrder(root) + self.assertEqual(result, [[3], [9, 20], [15, 7]]) + + def test_example_2(self): + # Example 2: Input: root = [1] + # Output: [[1]] + solution = Solution() + root = self.create_binary_tree([1]) + result = solution.levelOrder(root) + self.assertEqual(result, [[1]]) + + def test_example_3(self): + # Example 3: Input: root = [] + # Output: [] + solution = Solution() + root = self.create_binary_tree([]) + result = solution.levelOrder(root) + self.assertEqual(result, []) \ No newline at end of file