diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py new file mode 100644 index 00000000..6dda1665 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py @@ -0,0 +1,22 @@ +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_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py new file mode 100644 index 00000000..49e72b81 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.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_82_k_smallest_element_in_bst.py b/src/my_project/interviews/top_150_questions_round_22/ex_82_k_smallest_element_in_bst.py new file mode 100644 index 00000000..0f4220cb --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_82_k_smallest_element_in_bst.py @@ -0,0 +1,24 @@ +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 kthSmallest(self, root: TreeNode, k: int) -> int: + # Collect all values in sorted order using in-order traversal + values = [] + + def inorder(node): + if not node: + return + inorder(node.left) + values.append(node.val) + inorder(node.right) + + inorder(root) + return values[k - 1] \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_82_k_smallest_element_in_bst.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_82_k_smallest_element_in_bst.ts new file mode 100644 index 00000000..f38237a9 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_82_k_smallest_element_in_bst.ts @@ -0,0 +1,16 @@ +import { TreeNode } from './TreeNode'; + +function kthSmallest(root: TreeNode | null, k: number): number { + const result: number[] = []; + + function inorder(node: TreeNode | null): void { + if (!node) return; + + inorder(node.left); + result.push(node.val); + inorder(node.right); + } + + inorder(root); + return result[k - 1]; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_82_k_smallest_element_in_bst_round_22.py b/tests/test_150_questions_round_22/test_82_k_smallest_element_in_bst_round_22.py new file mode 100644 index 00000000..66a6866f --- /dev/null +++ b/tests/test_150_questions_round_22/test_82_k_smallest_element_in_bst_round_22.py @@ -0,0 +1,50 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_82_k_smallest_element_in_bst 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,1,4,null,2], k = 1 + # Output: 1 + solution = Solution() + root = self.create_binary_tree([3, 1, 4, None, 2]) + result = solution.kthSmallest(root, 1) + self.assertEqual(result, 1) + + def test_example_2(self): + # Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 + # Output: 3 + solution = Solution() + root = self.create_binary_tree([5, 3, 6, 2, 4, None, None, 1]) + result = solution.kthSmallest(root, 3) + self.assertEqual(result, 3) \ No newline at end of file