From 257e19e9f3752cff5efc283aa6f216cec703194a Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 30 Mar 2026 04:32:37 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_2.py | 21 ---- .../common_algos/two_sum_round_3.py | 21 ---- .../common_algos/two_sum_round_4.py | 21 ---- .../common_algos/two_sum_round_5.py | 21 ---- .../common_algos/two_sum_round_6.py | 21 ---- .../common_algos/two_sum_round_7.py | 21 ---- .../common_algos/valid_palindrome_round_2.py | 22 ---- .../common_algos/valid_palindrome_round_3.py | 22 ---- .../common_algos/valid_palindrome_round_4.py | 22 ---- .../common_algos/valid_palindrome_round_5.py | 22 ---- .../common_algos/valid_palindrome_round_6.py | 22 ---- .../common_algos/valid_palindrome_round_7.py | 22 ---- .../ex_115_median_of_two_sorted_arrays.py | 45 +++++++ .../ex_115_median_of_two_sorted_arrays.ts | 44 +++++++ ...15_median_of_two_sorted_arrays_round_22.py | 113 ++++++++++++++++++ 15 files changed, 202 insertions(+), 258 deletions(-) delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py delete 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_115_median_of_two_sorted_arrays.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_115_median_of_two_sorted_arrays.ts create mode 100644 tests/test_150_questions_round_22/test_115_median_of_two_sorted_arrays_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py +++ /dev/null @@ -1,21 +0,0 @@ -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_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py +++ /dev/null @@ -1,22 +0,0 @@ -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/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py +++ /dev/null @@ -1,22 +0,0 @@ -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/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py +++ /dev/null @@ -1,22 +0,0 @@ -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_115_median_of_two_sorted_arrays.py b/src/my_project/interviews/top_150_questions_round_22/ex_115_median_of_two_sorted_arrays.py new file mode 100644 index 00000000..43458051 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_115_median_of_two_sorted_arrays.py @@ -0,0 +1,45 @@ +from typing import List + +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + """ + Find the median of two sorted arrays using binary search. + Time complexity: O(log(min(m, n))) + Space complexity: O(1) + """ + # Ensure nums1 is the smaller array for efficiency + if len(nums1) > len(nums2): + return self.findMedianSortedArrays(nums2, nums1) + + m, n = len(nums1), len(nums2) + left, right = 0, m + + while left <= right: + # Partition nums1 + partition1 = (left + right) // 2 + # Partition nums2 such that left side has half the total elements + partition2 = (m + n + 1) // 2 - partition1 + + # Handle edge cases where partition is at the boundary + max_left1 = float('-inf') if partition1 == 0 else nums1[partition1 - 1] + min_right1 = float('inf') if partition1 == m else nums1[partition1] + + max_left2 = float('-inf') if partition2 == 0 else nums2[partition2 - 1] + min_right2 = float('inf') if partition2 == n else nums2[partition2] + + # Check if we found the correct partition + if max_left1 <= min_right2 and max_left2 <= min_right1: + # If total length is even + if (m + n) % 2 == 0: + return (max(max_left1, max_left2) + min(min_right1, min_right2)) / 2 + else: + # If total length is odd + return max(max_left1, max_left2) + elif max_left1 > min_right2: + # Move partition1 to the left + right = partition1 - 1 + else: + # Move partition1 to the right + left = partition1 + 1 + + raise ValueError("Input arrays are not sorted") \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_115_median_of_two_sorted_arrays.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_115_median_of_two_sorted_arrays.ts new file mode 100644 index 00000000..cb02c4bc --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_115_median_of_two_sorted_arrays.ts @@ -0,0 +1,44 @@ +function findMedianSortedArrays(nums1: number[], nums2: number[]): number { + // Ensure nums1 is the smaller array for efficiency + if (nums1.length > nums2.length) { + return findMedianSortedArrays(nums2, nums1); + } + + const m = nums1.length; + const n = nums2.length; + let left = 0; + let right = m; + + while (left <= right) { + // Partition nums1 + const partition1 = Math.floor((left + right) / 2); + // Partition nums2 such that left side has half the total elements + const partition2 = Math.floor((m + n + 1) / 2) - partition1; + + // Handle edge cases where partition is at the boundary + const maxLeft1 = partition1 === 0 ? -Infinity : nums1[partition1 - 1]; + const minRight1 = partition1 === m ? Infinity : nums1[partition1]; + + const maxLeft2 = partition2 === 0 ? -Infinity : nums2[partition2 - 1]; + const minRight2 = partition2 === n ? Infinity : nums2[partition2]; + + // Check if we found the correct partition + if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { + // If total length is even + if ((m + n) % 2 === 0) { + return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2; + } else { + // If total length is odd + return Math.max(maxLeft1, maxLeft2); + } + } else if (maxLeft1 > minRight2) { + // Move partition1 to the left + right = partition1 - 1; + } else { + // Move partition1 to the right + left = partition1 + 1; + } + } + + throw new Error("Input arrays are not sorted"); +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_115_median_of_two_sorted_arrays_round_22.py b/tests/test_150_questions_round_22/test_115_median_of_two_sorted_arrays_round_22.py new file mode 100644 index 00000000..9c64c604 --- /dev/null +++ b/tests/test_150_questions_round_22/test_115_median_of_two_sorted_arrays_round_22.py @@ -0,0 +1,113 @@ +import unittest +from typing import List +from src.my_project.interviews.top_150_questions_round_22\ + .ex_115_median_of_two_sorted_arrays import Solution + + +class MedianOfTwoSortedArraysTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1_odd_total_length(self): + """Test: nums1 = [1,3], nums2 = [2], expected = 2.00000""" + nums1 = [1, 3] + nums2 = [2] + expected = 2.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_example_2_even_total_length(self): + """Test: nums1 = [1,2], nums2 = [3,4], expected = 2.50000""" + nums1 = [1, 2] + nums2 = [3, 4] + expected = 2.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_empty_nums1(self): + """Test: nums1 = [], nums2 = [1], expected = 1.00000""" + nums1 = [] + nums2 = [1] + expected = 1.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_empty_nums2(self): + """Test: nums1 = [2], nums2 = [], expected = 2.00000""" + nums1 = [2] + nums2 = [] + expected = 2.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_single_element_each_odd(self): + """Test: nums1 = [1], nums2 = [2,3], expected = 2.00000""" + nums1 = [1] + nums2 = [2, 3] + expected = 2.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_single_element_each_even(self): + """Test: nums1 = [1], nums2 = [2], expected = 1.50000""" + nums1 = [1] + nums2 = [2] + expected = 1.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_disjoint_arrays(self): + """Test: nums1 = [1,2,3], nums2 = [4,5,6], expected = 3.50000""" + nums1 = [1, 2, 3] + nums2 = [4, 5, 6] + expected = 3.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_overlapping_arrays(self): + """Test: nums1 = [1,3,5,7], nums2 = [2,4,6,8], expected = 4.50000""" + nums1 = [1, 3, 5, 7] + nums2 = [2, 4, 6, 8] + expected = 4.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_negative_numbers(self): + """Test: nums1 = [-5,-3,-1], nums2 = [-4,-2,0], expected = -2.50000""" + nums1 = [-5, -3, -1] + nums2 = [-4, -2, 0] + expected = -2.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_mixed_positive_negative(self): + """Test: nums1 = [-2,0], nums2 = [1,3], expected = 0.50000""" + nums1 = [-2, 0] + nums2 = [1, 3] + expected = 0.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_duplicates(self): + """Test: nums1 = [1,1], nums2 = [1,1], expected = 1.00000""" + nums1 = [1, 1] + nums2 = [1, 1] + expected = 1.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_large_difference_in_sizes(self): + """Test: nums1 = [1], nums2 = [2,3,4,5,6], expected = 3.50000""" + nums1 = [1] + nums2 = [2, 3, 4, 5, 6] + expected = 3.50000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) + + def test_all_elements_in_nums1_smaller(self): + """Test: nums1 = [1,2], nums2 = [3,4,5], expected = 3.00000""" + nums1 = [1, 2] + nums2 = [3, 4, 5] + expected = 3.00000 + result = self.solution.findMedianSortedArrays(nums1, nums2) + self.assertAlmostEqual(result, expected, places=5) \ No newline at end of file