From ec493f114d211689363f698e0e628fb00ad0a987 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 15 Feb 2026 04:49:29 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_4.py | 16 ++++++++++++++ .../common_algos/valid_palindrome_round_4.py | 22 +++++++++++++++++++ .../ex_77_count_complete_tree_nodes.py | 17 ++++++++++++++ .../ex_77_count_complete_tree_nodes.ts | 9 ++++++++ ...t_77_count_complete_tree_nodes_round_22.py | 20 +++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_77_count_complete_tree_nodes.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_77_count_complete_tree_nodes.ts create mode 100644 tests/test_150_questions_round_22/test_77_count_complete_tree_nodes_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py @@ -0,0 +1,16 @@ +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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py new file mode 100644 index 00000000..8bfb7f9e --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.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 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_77_count_complete_tree_nodes.py b/src/my_project/interviews/top_150_questions_round_22/ex_77_count_complete_tree_nodes.py new file mode 100644 index 00000000..7147f9bd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_77_count_complete_tree_nodes.py @@ -0,0 +1,17 @@ +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 countNodes(self, root: Optional[TreeNode]) -> int: + + if not root: + return 0 + else: + return self.countNodes(root.left) + self.countNodes(root.right) + 1 + \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_77_count_complete_tree_nodes.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_77_count_complete_tree_nodes.ts new file mode 100644 index 00000000..98e8ffad --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_77_count_complete_tree_nodes.ts @@ -0,0 +1,9 @@ +import { TreeNode } from './TreeNode'; + +function countNodes(root: TreeNode | null): number { + if (!root) { + return 0; + } else { + return countNodes(root.left) + countNodes(root.right) + 1; + } +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_77_count_complete_tree_nodes_round_22.py b/tests/test_150_questions_round_22/test_77_count_complete_tree_nodes_round_22.py new file mode 100644 index 00000000..751bd4e0 --- /dev/null +++ b/tests/test_150_questions_round_22/test_77_count_complete_tree_nodes_round_22.py @@ -0,0 +1,20 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_77_count_complete_tree_nodes import Solution, TreeNode + + +class CountNodesTestCase(unittest.TestCase): + + def test_count_none(self): + solution = Solution() + tree = None + output = solution.countNodes(root=tree) + self.assertEqual(0, output) + + + def test_count_non_empty_tree(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.countNodes(root=tree) + self.assertEqual(3, output) \ No newline at end of file