diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.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_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..276891bf --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.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_76_binary_search_tree_iterator.py b/src/my_project/interviews/top_150_questions_round_22/ex_76_binary_search_tree_iterator.py new file mode 100644 index 00000000..14917cbd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_76_binary_search_tree_iterator.py @@ -0,0 +1,36 @@ +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 BSTIterator: + + def __init__(self, root: Optional[TreeNode]): + self.stack = [] + self._push_left(root) + + def _push_left(self, node: Optional[TreeNode]) -> None: + """Push all left children of a node onto the stack.""" + while node: + self.stack.append(node) + node = node.left + + def next(self) -> int: + # Pop the top node (next smallest element) + node = self.stack.pop() + + # If it has a right child, push all left descendants of the right child + if node.right: + self._push_left(node.right) + + return node.val + + def hasNext(self) -> bool: + return len(self.stack) > 0 + \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_76_binary_search_tree_iterator.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_76_binary_search_tree_iterator.ts new file mode 100644 index 00000000..2e352917 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_76_binary_search_tree_iterator.ts @@ -0,0 +1,36 @@ +import { TreeNode } from './TreeNode'; + +class BSTIterator { + private stack: TreeNode[]; + + constructor(root: TreeNode | null) { + this.stack = []; + this.pushLeft(root); + } + + private pushLeft(node: TreeNode | null): void { + /** + * Push all left children of a node onto the stack. + */ + while (node !== null) { + this.stack.push(node); + node = node.left; + } + } + + next(): number { + // Pop the top node (next smallest element) + const node = this.stack.pop()!; + + // If it has a right child, push all left descendants of the right child + if (node.right !== null) { + this.pushLeft(node.right); + } + + return node.val; + } + + hasNext(): boolean { + return this.stack.length > 0; + } +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_76_binary_search_tree_iterator_round_22.py b/tests/test_150_questions_round_22/test_76_binary_search_tree_iterator_round_22.py new file mode 100644 index 00000000..d4654d79 --- /dev/null +++ b/tests/test_150_questions_round_22/test_76_binary_search_tree_iterator_round_22.py @@ -0,0 +1,49 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_76_binary_search_tree_iterator import BSTIterator, TreeNode + + +class BSTIteratorTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Input: ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] + [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] + Output: [null, 3, 7, true, 9, true, 15, true, 20, false] + + Explanation: + BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); + bSTIterator.next(); // return 3 + bSTIterator.next(); // return 7 + bSTIterator.hasNext(); // return True + bSTIterator.next(); // return 9 + bSTIterator.hasNext(); // return True + bSTIterator.next(); // return 15 + bSTIterator.hasNext(); // return True + bSTIterator.next(); // return 20 + bSTIterator.hasNext(); // return False + """ + # Build tree: [7, 3, 15, null, null, 9, 20] + # 7 + # / \ + # 3 15 + # / \ + # 9 20 + root = TreeNode(7) + root.left = TreeNode(3) + root.right = TreeNode(15) + root.right.left = TreeNode(9) + root.right.right = TreeNode(20) + + bst_iterator = BSTIterator(root) + + self.assertEqual(bst_iterator.next(), 3) + self.assertEqual(bst_iterator.next(), 7) + self.assertTrue(bst_iterator.hasNext()) + self.assertEqual(bst_iterator.next(), 9) + self.assertTrue(bst_iterator.hasNext()) + self.assertEqual(bst_iterator.next(), 15) + self.assertTrue(bst_iterator.hasNext()) + self.assertEqual(bst_iterator.next(), 20) + self.assertFalse(bst_iterator.hasNext()) \ No newline at end of file