From 879e1af1e0c7b92c3c5ee5a6bbd7e8b4ae7df960 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 31 Mar 2026 04:46:53 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_2.py | 21 ++++++ .../common_algos/valid_palindrome_round_2.py | 22 ++++++ .../ex_116_kth_largest_element_in_array.py | 37 ++++++++++ .../ex_116_kth_largest_element_in_array.ts | 37 ++++++++++ ...6_kth_largest_element_in_array_round_22.py | 72 +++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_116_kth_largest_element_in_array.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_116_kth_largest_element_in_array.ts create mode 100644 tests/test_150_questions_round_22/test_116_kth_largest_element_in_array_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 new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.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_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.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_116_kth_largest_element_in_array.py b/src/my_project/interviews/top_150_questions_round_22/ex_116_kth_largest_element_in_array.py new file mode 100644 index 00000000..a659f61b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_116_kth_largest_element_in_array.py @@ -0,0 +1,37 @@ +import random +from typing import List + + +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + target = len(nums) - k + + def quickselect(left: int, right: int) -> int: + pivot_index = random.randint(left, right) + pivot = nums[pivot_index] + + # 3-way partition (Dutch National Flag) + low = left + mid = left + high = right + + while mid <= high: + if nums[mid] < pivot: + nums[low], nums[mid] = nums[mid], nums[low] + low += 1 + mid += 1 + elif nums[mid] > pivot: + nums[mid], nums[high] = nums[high], nums[mid] + high -= 1 + else: + mid += 1 + + # All elements equal to pivot are in [low, high] + if target < low: + return quickselect(left, low - 1) + elif target > high: + return quickselect(high + 1, right) + else: + return nums[target] + + return quickselect(0, len(nums) - 1) diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_116_kth_largest_element_in_array.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_116_kth_largest_element_in_array.ts new file mode 100644 index 00000000..5fcccdfb --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_116_kth_largest_element_in_array.ts @@ -0,0 +1,37 @@ +function findKthLargest(nums: number[], k: number): number { + const target = nums.length - k; + + function quickselect(left: number, right: number): number { + const pivotIndex = left + Math.floor(Math.random() * (right - left + 1)); + const pivot = nums[pivotIndex]; + + // 3-way partition (Dutch National Flag) + let low = left; + let mid = left; + let high = right; + + while (mid <= high) { + if (nums[mid] < pivot) { + [nums[low], nums[mid]] = [nums[mid], nums[low]]; + low++; + mid++; + } else if (nums[mid] > pivot) { + [nums[mid], nums[high]] = [nums[high], nums[mid]]; + high--; + } else { + mid++; + } + } + + // All elements equal to pivot are in [low, high] + if (target < low) { + return quickselect(left, low - 1); + } else if (target > high) { + return quickselect(high + 1, right); + } else { + return nums[target]; + } + } + + return quickselect(0, nums.length - 1); +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_116_kth_largest_element_in_array_round_22.py b/tests/test_150_questions_round_22/test_116_kth_largest_element_in_array_round_22.py new file mode 100644 index 00000000..8fad621f --- /dev/null +++ b/tests/test_150_questions_round_22/test_116_kth_largest_element_in_array_round_22.py @@ -0,0 +1,72 @@ +import unittest +from typing import List +from src.my_project.interviews.top_150_questions_round_22\ + .ex_116_kth_largest_element_in_array import Solution + + +class KthLargestElementTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + """Test: nums = [3,2,1,5,6,4], k = 2, expected = 5""" + nums = [3, 2, 1, 5, 6, 4] + k = 2 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 5) + + def test_example_2(self): + """Test: nums = [3,2,3,1,2,4,5,5,6], k = 4, expected = 4""" + nums = [3, 2, 3, 1, 2, 4, 5, 5, 6] + k = 4 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 4) + + def test_single_element(self): + """Test: nums = [1], k = 1, expected = 1""" + nums = [1] + k = 1 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 1) + + def test_all_same_elements(self): + """Test: nums = [3,3,3,3], k = 2, expected = 3""" + nums = [3, 3, 3, 3] + k = 2 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 3) + + def test_k_equals_length(self): + """Test: nums = [7,6,5,4,3,2,1], k = 7, expected = 1""" + nums = [7, 6, 5, 4, 3, 2, 1] + k = 7 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 1) + + def test_k_equals_1(self): + """Test: nums = [2,1,4,3], k = 1, expected = 4""" + nums = [2, 1, 4, 3] + k = 1 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 4) + + def test_negative_numbers(self): + """Test: nums = [-1,-2,-3,-4], k = 2, expected = -2""" + nums = [-1, -2, -3, -4] + k = 2 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, -2) + + def test_duplicates_with_target(self): + """Test: nums = [1,2,2,3,3,4], k = 3, expected = 3""" + nums = [1, 2, 2, 3, 3, 4] + k = 3 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 3) + + def test_two_elements(self): + """Test: nums = [2,1], k = 1, expected = 2""" + nums = [2, 1] + k = 1 + result = self.solution.findKthLargest(nums, k) + self.assertEqual(result, 2) \ No newline at end of file