From 6bcb19641f663b354cafee611f35643e4f211f84 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 9 Feb 2026 04:39:35 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_7.py | 15 +++ .../common_algos/valid_palindrome_round_7.py | 22 ++++ ...ulating_next_right_pointer_in_each_node.py | 37 ++++++ ...ulating_next_right_pointer_in_each_node.ts | 60 +++++++++ ...ext_right_pointer_in_each_node_round_22.py | 123 ++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_71_populating_next_right_pointer_in_each_node.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts create mode 100644 tests/test_150_questions_round_22/test_71_populating_next_right_pointer_in_each_node_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..15af3a9a --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,15 @@ +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_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..854c89ac --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_71_populating_next_right_pointer_in_each_node.py b/src/my_project/interviews/top_150_questions_round_22/ex_71_populating_next_right_pointer_in_each_node.py new file mode 100644 index 00000000..f4258c66 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_71_populating_next_right_pointer_in_each_node.py @@ -0,0 +1,37 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque + +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next + +class Solution: + def connect(self, root: 'Node') -> 'Node': + if not root: + return None + + # BFS using queue + queue = deque([root]) + + while queue: + level_size = len(queue) + + # Process all nodes at current level + for i in range(level_size): + node = queue.popleft() + + # Connect to next node in the level (if not the last node) + if i < level_size - 1: + node.next = queue[0] + + # Add children to queue for next level + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + return root \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts new file mode 100644 index 00000000..9402db54 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts @@ -0,0 +1,60 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque + +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next + +class Solution: + def connect(self, root: 'Node') -> 'Node': + if not root: + return None + + # BFS using queue + queue = deque([root]) + + while queue: + level_size = len(queue) + + # Process all nodes at current level + for i in range(level_size): + node = queue.popleft() + + # Connect to next node in the level (if not the last node) + if i < level_size - 1: + node.next = queue[0] + + # Add children to queue for next level + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + return root + + +/** + * Definition for _Node. + * class _Node { + * val: number + * left: _Node | null + * right: _Node | null + * next: _Node | null + * + * constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * this.next = (next===undefined ? null : next) + * } + * } + */ + + +function connect(root: _Node | null): _Node | null { + +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_71_populating_next_right_pointer_in_each_node_round_22.py b/tests/test_150_questions_round_22/test_71_populating_next_right_pointer_in_each_node_round_22.py new file mode 100644 index 00000000..4c5c0c57 --- /dev/null +++ b/tests/test_150_questions_round_22/test_71_populating_next_right_pointer_in_each_node_round_22.py @@ -0,0 +1,123 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_71_populating_next_right_pointer_in_each_node import Solution, Node + +class PopulatingNextRightPointersInEachNodeTestCase(unittest.TestCase): + + def build_tree(self, values: List[Optional[int]]) -> Optional[Node]: + """Build tree from level-order list representation.""" + if not values: + return None + + root = Node(values[0]) + queue = [root] + i = 1 + + while queue and i < len(values): + node = queue.pop(0) + + # Add left child + if i < len(values) and values[i] is not None: + node.left = Node(values[i]) + queue.append(node.left) + i += 1 + + # Add right child + if i < len(values) and values[i] is not None: + node.right = Node(values[i]) + queue.append(node.right) + i += 1 + + return root + + def verify_next_pointers(self, root: Optional[Node]) -> List[str]: + """Verify next pointers and return serialized output.""" + if not root: + return [] + + result = [] + leftmost = root + + while leftmost: + current = leftmost + while current: + result.append(str(current.val)) + current = current.next + result.append('#') + + # Move to next level + leftmost = leftmost.left if leftmost.left else leftmost.right + if not leftmost: + # Find the first node in the next level + temp = root + queue = [root] + while queue: + node = queue.pop(0) + if node.left and node.left not in [n for level in [root] for n in [root]]: + leftmost = node.left + break + if node.right: + leftmost = node.right + break + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + break + + return result + + def get_level_order_with_next(self, root: Optional[Node]) -> str: + """Get level order traversal following next pointers.""" + if not root: + return "" + + result = [] + leftmost = root + + while leftmost: + current = leftmost + level_vals = [] + while current: + level_vals.append(str(current.val)) + current = current.next + result.extend(level_vals) + result.append('#') + + # Find leftmost node of next level + temp = leftmost + leftmost = None + while temp and not leftmost: + if temp.left: + leftmost = temp.left + elif temp.right: + leftmost = temp.right + else: + temp = temp.next + + return ','.join(result) + + def test_example_1(self): + """ + Input: root = [1,2,3,4,5,null,7] + Output: [1,#,2,3,#,4,5,7,#] + """ + solution = Solution() + root = self.build_tree([1, 2, 3, 4, 5, None, 7]) + result = solution.connect(root) + output = self.get_level_order_with_next(result) + expected = "1,#,2,3,#,4,5,7,#" + self.assertEqual(output, expected) + + def test_example_2(self): + """ + Input: root = [] + Output: [] + """ + solution = Solution() + root = self.build_tree([]) + result = solution.connect(root) + output = self.get_level_order_with_next(result) + expected = "" + self.assertEqual(output, expected) \ No newline at end of file