From a264f2f361466f78ebca750c9b21f641a3d96b20 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 23 Nov 2025 04:33:05 -0600 Subject: [PATCH] algo passes --- .../common_algos/two_sum_round_15.py | 16 ++++++++++++ .../common_algos/valid_palindrome_round_15.py | 23 +++++++++++++++++ ...maximum_sum_distinct_subarrays_lenght_k.py | 25 +++++++++++++++++++ .../round_4/merge_intervals.py | 16 ++++++++++++ .../palindrome_number.py | 9 +++++++ .../test_palindrome_number_round_21.py | 15 +++++++++++ 6 files changed, 104 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_15.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_15.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_4/maximum_sum_distinct_subarrays_lenght_k.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_4/merge_intervals.py create mode 100644 src/my_project/interviews/top_150_questions_round_21/palindrome_number.py create mode 100644 tests/test_150_questions_round_21/test_palindrome_number_round_21.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_15.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_15.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_15.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_15.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_15.py new file mode 100644 index 00000000..82f08a6e --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_15.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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_4/maximum_sum_distinct_subarrays_lenght_k.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/maximum_sum_distinct_subarrays_lenght_k.py new file mode 100644 index 00000000..808bc101 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/maximum_sum_distinct_subarrays_lenght_k.py @@ -0,0 +1,25 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def maximumSubarraySum(self, nums: List[int], k: int) -> int: + max_sum = 0 + current_sum = 0 + freq_sum = dict() + left = 0 + + for right in range(len(nums)): + current_sum += nums[right] + + freq_sum[nums[right]] = freq_sum.get(nums[right], 0) + 1 + + if right - left > k - 1: + freq_sum[nums[left]] -= 1 + if freq_sum[nums[left]] == 0: + del freq_sum[nums[left]] + current_sum -= nums[left] + left += 1 + + if right - left == k - 1 and len(freq_sum) == k: + max_sum = max(max_sum, current_sum) + + return max_sum diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_4/merge_intervals.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/merge_intervals.py new file mode 100644 index 00000000..c71a5069 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/merge_intervals.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + + list_answer = list() + + for i in sorted(intervals, key=lambda x: x[0]): + + if list_answer and i[0] <= list_answer[-1][-1]: + list_answer[-1][-1] = max(list_answer[-1][-1], i[-1]) + else: + list_answer.append(i) + + return list_answer \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/palindrome_number.py b/src/my_project/interviews/top_150_questions_round_21/palindrome_number.py new file mode 100644 index 00000000..80952ebd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/palindrome_number.py @@ -0,0 +1,9 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isPalindrome(self, x: int) -> bool: + + str_x = str(x) + + return str_x == str_x[::-1] \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_palindrome_number_round_21.py b/tests/test_150_questions_round_21/test_palindrome_number_round_21.py new file mode 100644 index 00000000..466c3b12 --- /dev/null +++ b/tests/test_150_questions_round_21/test_palindrome_number_round_21.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.palindrome_number import Solution + +class PalindromeNumberTestCase(unittest.TestCase): + + def test_is_palindrome_number(self): + solution = Solution() + output = solution.isPalindrome(x=121) + self.assertTrue(output) + + def test_is_no_palindrome_number(self): + solution = Solution() + output = solution.isPalindrome(x=123) + self.assertFalse(output) \ No newline at end of file