From b57b6d9a9cc82bc5b0c77a1871b7ec04118f76e0 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 17 Feb 2026 04:35:40 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_6.py | 18 +++++ .../common_algos/valid_palindrome_round_6.py | 23 +++++++ .../ex_79_binary_tree_right_side_view.py | 35 ++++++++++ ..._lowest_common_ancestor_of_binary_tree.ts} | 0 .../ex_79_binary_tree_right_side_view.ts | 33 ++++++++++ ...79_binary_tree_right_side_view_round_22.py | 66 +++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_79_binary_tree_right_side_view.py rename src/my_project/interviews_typescript/top_150_questions_round_1/{test_78_lowest_common_ancestor_of_binary_tree_round_22.ts => ex_78_lowest_common_ancestor_of_binary_tree.ts} (100%) create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_79_binary_tree_right_side_view.ts create mode 100644 tests/test_150_questions_round_22/test_79_binary_tree_right_side_view_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py new file mode 100644 index 00000000..fbc8a524 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py @@ -0,0 +1,18 @@ +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_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py new file mode 100644 index 00000000..f1320ee0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.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_79_binary_tree_right_side_view.py b/src/my_project/interviews/top_150_questions_round_22/ex_79_binary_tree_right_side_view.py new file mode 100644 index 00000000..b10af91e --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_79_binary_tree_right_side_view.py @@ -0,0 +1,35 @@ +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 rightSideView(self, root: Optional[TreeNode]) -> List[int]: + if not root: + return [] + + result = [] + queue = deque([root]) + + while queue: + level_size = len(queue) + + for i in range(level_size): + node = queue.popleft() + + # Add the rightmost node of each level + if i == level_size - 1: + result.append(node.val) + + # Add children to queue (left first, then right) + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + return result \ 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/ex_78_lowest_common_ancestor_of_binary_tree.ts similarity index 100% rename from src/my_project/interviews_typescript/top_150_questions_round_1/test_78_lowest_common_ancestor_of_binary_tree_round_22.ts rename to src/my_project/interviews_typescript/top_150_questions_round_1/ex_78_lowest_common_ancestor_of_binary_tree.ts diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_79_binary_tree_right_side_view.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_79_binary_tree_right_side_view.ts new file mode 100644 index 00000000..24babd1f --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_79_binary_tree_right_side_view.ts @@ -0,0 +1,33 @@ +import { TreeNode } from './TreeNode'; + +function rightSideView(root: TreeNode | null): number[] { + if (!root) { + return []; + } + + const result: number[] = []; + const queue: TreeNode[] = [root]; + + while (queue.length > 0) { + const levelSize = queue.length; + + for (let i = 0; i < levelSize; i++) { + const node = queue.shift()!; + + // Add the rightmost node of each level + if (i === levelSize - 1) { + result.push(node.val); + } + + // Add children to queue (left first, then right) + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } + } + } + + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_79_binary_tree_right_side_view_round_22.py b/tests/test_150_questions_round_22/test_79_binary_tree_right_side_view_round_22.py new file mode 100644 index 00000000..6a47ac46 --- /dev/null +++ b/tests/test_150_questions_round_22/test_79_binary_tree_right_side_view_round_22.py @@ -0,0 +1,66 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_79_binary_tree_right_side_view import Solution, TreeNode + +class BinaryTreeRightSideViewTestCase(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 = [1,2,3,null,5,null,4] + # Output: [1,3,4] + solution = Solution() + root = self.create_binary_tree([1, 2, 3, None, 5, None, 4]) + result = solution.rightSideView(root) + self.assertEqual(result, [1, 3, 4]) + + def test_example_2(self): + # Example 2: Input: root = [1,2,3,4,null,null,null,5] + # Output: [1,3,4,5] + solution = Solution() + root = self.create_binary_tree([1, 2, 3, 4, None, None, None, 5]) + result = solution.rightSideView(root) + self.assertEqual(result, [1, 3, 4, 5]) + + def test_example_3(self): + # Example 3: Input: root = [1,null,3] + # Output: [1,3] + solution = Solution() + root = self.create_binary_tree([1, None, 3]) + result = solution.rightSideView(root) + self.assertEqual(result, [1, 3]) + + def test_example_4(self): + # Example 4: Input: root = [] + # Output: [] + solution = Solution() + root = self.create_binary_tree([]) + result = solution.rightSideView(root) + self.assertEqual(result, []) \ No newline at end of file