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 new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py @@ -0,0 +1,21 @@ +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_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.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_113_find_first_and_last_position_of_element.py b/src/my_project/interviews/top_150_questions_round_22/ex_113_find_first_and_last_position_of_element.py new file mode 100644 index 00000000..bdddfac2 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_113_find_first_and_last_position_of_element.py @@ -0,0 +1,59 @@ +from typing import List + + +class Solution: + def searchRange(self, nums: List[int], target: int) -> List[int]: + """ + Find the starting and ending position of a target value in a sorted array. + Time Complexity: O(log n) using binary search + Space Complexity: O(1) + """ + if not nums: + return [-1, -1] + + # Find the leftmost (first) occurrence + left = self.findLeft(nums, target) + if left == -1: + return [-1, -1] + + # Find the rightmost (last) occurrence + right = self.findRight(nums, target) + + return [left, right] + + def findLeft(self, nums: List[int], target: int) -> int: + """Find the leftmost occurrence of target""" + left, right = 0, len(nums) - 1 + result = -1 + + while left <= right: + mid = left + (right - left) // 2 + + if nums[mid] == target: + result = mid + right = mid - 1 # Continue searching in the left half + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + + return result + + def findRight(self, nums: List[int], target: int) -> int: + """Find the rightmost occurrence of target""" + left, right = 0, len(nums) - 1 + result = -1 + + while left <= right: + mid = left + (right - left) // 2 + + if nums[mid] == target: + result = mid + left = mid + 1 # Continue searching in the right half + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + + return result + \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_113_find_first_and_last_position_of_element.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_113_find_first_and_last_position_of_element.ts new file mode 100644 index 00000000..597dcad5 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_113_find_first_and_last_position_of_element.ts @@ -0,0 +1,58 @@ +function searchRange(nums: number[], target: number): number[] { + if (nums.length === 0) { + return [-1, -1]; + } + + // Find the leftmost (first) occurrence + const left = findLeft(nums, target); + if (left === -1) { + return [-1, -1]; + } + + // Find the rightmost (last) occurrence + const right = findRight(nums, target); + + return [left, right]; +} + +function findLeft(nums: number[], target: number): number { + let left = 0; + let right = nums.length - 1; + let result = -1; + + while (left <= right) { + const mid = Math.floor(left + (right - left) / 2); + + if (nums[mid] === target) { + result = mid; + right = mid - 1; // Continue searching in the left half + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return result; +} + +function findRight(nums: number[], target: number): number { + let left = 0; + let right = nums.length - 1; + let result = -1; + + while (left <= right) { + const mid = Math.floor(left + (right - left) / 2); + + if (nums[mid] === target) { + result = mid; + left = mid + 1; // Continue searching in the right half + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_113_find_first_and_last_position_of_element_round_22.py b/tests/test_150_questions_round_22/test_113_find_first_and_last_position_of_element_round_22.py new file mode 100644 index 00000000..effbe98e --- /dev/null +++ b/tests/test_150_questions_round_22/test_113_find_first_and_last_position_of_element_round_22.py @@ -0,0 +1,81 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_113_find_first_and_last_position_of_element import Solution + + +class FindFirstAndLastPositionTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1_target_exists_multiple_times(self): + """Test: nums = [5,7,7,8,8,10], target = 8, expected = [3,4]""" + nums = [5, 7, 7, 8, 8, 10] + target = 8 + expected = [3, 4] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_example_2_target_not_found(self): + """Test: nums = [5,7,7,8,8,10], target = 6, expected = [-1,-1]""" + nums = [5, 7, 7, 8, 8, 10] + target = 6 + expected = [-1, -1] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_example_3_empty_array(self): + """Test: nums = [], target = 0, expected = [-1,-1]""" + nums = [] + target = 0 + expected = [-1, -1] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_single_element_found(self): + """Test: nums = [1], target = 1, expected = [0,0]""" + nums = [1] + target = 1 + expected = [0, 0] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_single_element_not_found(self): + """Test: nums = [1], target = 2, expected = [-1,-1]""" + nums = [1] + target = 2 + expected = [-1, -1] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_all_elements_same_as_target(self): + """Test: nums = [2,2,2,2,2], target = 2, expected = [0,4]""" + nums = [2, 2, 2, 2, 2] + target = 2 + expected = [0, 4] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_target_at_beginning(self): + """Test: nums = [1,1,1,2,3,4], target = 1, expected = [0,2]""" + nums = [1, 1, 1, 2, 3, 4] + target = 1 + expected = [0, 2] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_target_at_end(self): + """Test: nums = [1,2,3,4,4,4], target = 4, expected = [3,5]""" + nums = [1, 2, 3, 4, 4, 4] + target = 4 + expected = [3, 5] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) + + def test_target_single_occurrence(self): + """Test: nums = [1,2,3,4,5], target = 3, expected = [2,2]""" + nums = [1, 2, 3, 4, 5] + target = 3 + expected = [2, 2] + result = self.solution.searchRange(nums, target) + self.assertEqual(result, expected) \ No newline at end of file