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..987b8bf9 --- /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..67993077 --- /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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_98_permutations.py b/src/my_project/interviews/top_150_questions_round_22/ex_98_permutations.py new file mode 100644 index 00000000..89ade253 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_98_permutations.py @@ -0,0 +1,38 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + """ + Generate all possible permutations of distinct integers. + + Approach: Backtracking + - Use recursion to build permutations + - At each step, try adding each unused number + - When current permutation is complete, add to results + - Backtrack and try other possibilities + + Time Complexity: O(n! * n) - n! permutations, each takes O(n) to build + Space Complexity: O(n) - recursion depth + current permutation + """ + result = [] + + def backtrack(current: List[int], remaining: List[int]): + # Base case: no more numbers to add + if not remaining: + result.append(current[:]) + return + + # Try each remaining number as the next element + for i in range(len(remaining)): + # Choose: add remaining[i] to current permutation + current.append(remaining[i]) + # Explore: recurse with remaining numbers + new_remaining = remaining[:i] + remaining[i+1:] + backtrack(current, new_remaining) + # Unchoose: backtrack + current.pop() + + backtrack([], nums) + return result \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_98_permutations.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_98_permutations.ts new file mode 100644 index 00000000..465846c0 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_98_permutations.ts @@ -0,0 +1,25 @@ +function permute(nums: number[]): number[][] { + const result: number[][] = []; + + function backtrack(current: number[], remaining: number[]): void { + // Base case: no more numbers to add + if (remaining.length === 0) { + result.push([...current]); + return; + } + + // Try each remaining number as the next element + for (let i = 0; i < remaining.length; i++) { + // Choose: add remaining[i] to current permutation + current.push(remaining[i]); + // Explore: recurse with remaining numbers + const newRemaining = remaining.slice(0, i).concat(remaining.slice(i + 1)); + backtrack(current, newRemaining); + // Unchoose: backtrack + current.pop(); + } + } + + backtrack([], nums); + return result; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_98_permutations_round_22.py b/tests/test_150_questions_round_22/test_98_permutations_round_22.py new file mode 100644 index 00000000..6ed20402 --- /dev/null +++ b/tests/test_150_questions_round_22/test_98_permutations_round_22.py @@ -0,0 +1,84 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_98_permutations import Solution + + +class PermutationsTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1_three_elements(self): + """Test with three distinct integers""" + nums = [1, 2, 3] + result = self.solution.permute(nums) + expected = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] + self.assertEqual(len(result), len(expected)) + for perm in expected: + self.assertIn(perm, result) + + def test_example_2_two_elements(self): + """Test with two distinct integers""" + nums = [0, 1] + result = self.solution.permute(nums) + expected = [[0, 1], [1, 0]] + self.assertEqual(len(result), len(expected)) + for perm in expected: + self.assertIn(perm, result) + + def test_example_3_single_element(self): + """Test with single integer""" + nums = [1] + result = self.solution.permute(nums) + expected = [[1]] + self.assertEqual(result, expected) + + def test_negative_numbers(self): + """Test with negative numbers""" + nums = [-1, 0, 1] + result = self.solution.permute(nums) + expected = [[-1, 0, 1], [-1, 1, 0], [0, -1, 1], [0, 1, -1], [1, -1, 0], [1, 0, -1]] + self.assertEqual(len(result), len(expected)) + for perm in expected: + self.assertIn(perm, result) + + def test_four_elements(self): + """Test with four elements""" + nums = [1, 2, 3, 4] + result = self.solution.permute(nums) + # 4! = 24 permutations + self.assertEqual(len(result), 24) + # Check all permutations are unique + unique_perms = [tuple(perm) for perm in result] + self.assertEqual(len(set(unique_perms)), 24) + # Check each permutation contains all original numbers + for perm in result: + self.assertEqual(sorted(perm), sorted(nums)) + + def test_five_elements(self): + """Test with five elements""" + nums = [1, 2, 3, 4, 5] + result = self.solution.permute(nums) + # 5! = 120 permutations + self.assertEqual(len(result), 120) + # Check all permutations are unique + unique_perms = [tuple(perm) for perm in result] + self.assertEqual(len(set(unique_perms)), 120) + + def test_mixed_positive_negative(self): + """Test with mixed positive and negative numbers""" + nums = [-2, 3] + result = self.solution.permute(nums) + expected = [[-2, 3], [3, -2]] + self.assertEqual(len(result), len(expected)) + for perm in expected: + self.assertIn(perm, result) + + def test_zero_and_negatives(self): + """Test with zero and negative numbers""" + nums = [0, -1] + result = self.solution.permute(nums) + expected = [[0, -1], [-1, 0]] + self.assertEqual(len(result), len(expected)) + for perm in expected: + self.assertIn(perm, result) \ No newline at end of file