From 8a1cb19b821ad8c38352d7145b5439efd085608a Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 11 Mar 2026 04:36:10 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_3.py | 22 ++++++++ .../common_algos/valid_palindrome_round_3.py | 22 ++++++++ .../ex_99_combination_sum.py | 32 ++++++++++++ .../ex_99_combination_sum.ts | 31 +++++++++++ .../test_99_combination_sum_round_22.py | 51 +++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_99_combination_sum.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_99_combination_sum.ts create mode 100644 tests/test_150_questions_round_22/test_99_combination_sum_round_22.py 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..31db08a9 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.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_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..67993077 --- /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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_99_combination_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_99_combination_sum.py new file mode 100644 index 00000000..95a602f8 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_99_combination_sum.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + + def backtrack(start: int, current: List[int], remaining: int): + # Base case: found a valid combination + if remaining == 0: + result.append(current[:]) + return + + # Base case: exceeded target + if remaining < 0: + return + + # Explore all candidates starting from 'start' index + for i in range(start, len(candidates)): + # Include candidates[i] in the current combination + current.append(candidates[i]) + + # Recurse with the same start index (we can reuse the same number) + backtrack(i, current, remaining - candidates[i]) + + # Backtrack: remove the last added element + current.pop() + + backtrack(0, [], target) + return result + \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_99_combination_sum.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_99_combination_sum.ts new file mode 100644 index 00000000..8010aa46 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_99_combination_sum.ts @@ -0,0 +1,31 @@ +function combinationSum(candidates: number[], target: number): number[][] { + const result: number[][] = []; + + function backtrack(start: number, current: number[], remaining: number): void { + // Base case: found a valid combination + if (remaining === 0) { + result.push([...current]); + return; + } + + // Base case: exceeded target + if (remaining < 0) { + return; + } + + // Explore all candidates starting from 'start' index + for (let i = start; i < candidates.length; i++) { + // Include candidates[i] in the current combination + current.push(candidates[i]); + + // Recurse with the same start index (we can reuse the same number) + backtrack(i, current, remaining - candidates[i]); + + // Backtrack: remove the last added element + current.pop(); + } + } + + backtrack(0, [], target); + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_99_combination_sum_round_22.py b/tests/test_150_questions_round_22/test_99_combination_sum_round_22.py new file mode 100644 index 00000000..8507c8b2 --- /dev/null +++ b/tests/test_150_questions_round_22/test_99_combination_sum_round_22.py @@ -0,0 +1,51 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_99_combination_sum import Solution + + +class CombinationSumTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example1(self): + """Test case: candidates = [2,3,6,7], target = 7""" + candidates = [2, 3, 6, 7] + target = 7 + result = self.solution.combinationSum(candidates, target) + expected = [[2, 2, 3], [7]] + self.assertEqual(sorted([sorted(x) for x in result]), + sorted([sorted(x) for x in expected])) + + def test_example2(self): + """Test case: candidates = [2,3,5], target = 8""" + candidates = [2, 3, 5] + target = 8 + result = self.solution.combinationSum(candidates, target) + expected = [[2, 2, 2, 2], [2, 3, 3], [3, 5]] + self.assertEqual(sorted([sorted(x) for x in result]), + sorted([sorted(x) for x in expected])) + + def test_example3(self): + """Test case: candidates = [2], target = 1""" + candidates = [2] + target = 1 + result = self.solution.combinationSum(candidates, target) + expected = [] + self.assertEqual(result, expected) + + def test_single_element_exact(self): + """Test case: single element that equals target""" + candidates = [5] + target = 5 + result = self.solution.combinationSum(candidates, target) + expected = [[5]] + self.assertEqual(result, expected) + + def test_multiple_use_same_number(self): + """Test case: using same number multiple times""" + candidates = [3] + target = 9 + result = self.solution.combinationSum(candidates, target) + expected = [[3, 3, 3]] + self.assertEqual(result, expected) \ No newline at end of file