diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_18.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_18.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_18.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_18.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_18.py new file mode 100644 index 00000000..a911a94f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_18.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='[^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_3/number_of_islands.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py index cc03860f..7cac8ad7 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py @@ -11,18 +11,16 @@ def numIslands(self, grid: List[List[str]]) -> int: rows, cols = len(grid), len(grid[0]) islands = 0 - def dfs(r: int, c: int): + def bfs(r: int, c: int): queue = deque([(r,c)]) - grid[r][c] = 0 + grid[r][c] = '0' - while queue: + while queue: row, col = queue.popleft() - for dr, dc in [(-1,0), (1,0), (0,-1), (0,1)]: + for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]: nr, nc = row + dr, col + dc - if (0 <= nr < rows - and 0 <= nc < cols - and grid[nr][nc] == '1'): + if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'): grid[nr][nc] = '0' queue.append((nr,nc)) @@ -30,7 +28,7 @@ def dfs(r: int, c: int): for c in range(cols): if grid[r][c] == '1': islands += 1 - dfs(r,c) + bfs(r,c) return islands @@ -38,21 +36,20 @@ def dfs(r: int, c: int): def numIslands_DFS(grid: List[List[str]]) -> int: """DFS approach - explores island depth-first""" - if not grid: + if not grid: return 0 - + rows, cols = len(grid), len(grid[0]) - islands = 0 def dfs(r: int, c: int): if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'): return - grid[r][c] = '0' + grid[r][c] = '0' - dfs(r-1, c) - dfs(r+1, c) + dfs(r-1,c) + dfs(r+1,c) dfs(r,c-1) dfs(r,c+1) @@ -64,4 +61,3 @@ def dfs(r: int, c: int): return islands - diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_islands.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_islands.py new file mode 100644 index 00000000..7cac8ad7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_islands.py @@ -0,0 +1,63 @@ +from typing import List, Union, Collection, Mapping, Optional +from collections import deque + +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + """BFS approach - explores island level by level""" + + if not grid: + return 0 + + rows, cols = len(grid), len(grid[0]) + islands = 0 + + def bfs(r: int, c: int): + queue = deque([(r,c)]) + grid[r][c] = '0' + + while queue: + row, col = queue.popleft() + + for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]: + nr, nc = row + dr, col + dc + if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'): + grid[nr][nc] = '0' + queue.append((nr,nc)) + + for r in range(rows): + for c in range(cols): + if grid[r][c] == '1': + islands += 1 + bfs(r,c) + + return islands + + + def numIslands_DFS(grid: List[List[str]]) -> int: + """DFS approach - explores island depth-first""" + + if not grid: + return 0 + + rows, cols = len(grid), len(grid[0]) + islands = 0 + + def dfs(r: int, c: int): + if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'): + return + + grid[r][c] = '0' + + dfs(r-1,c) + dfs(r+1,c) + dfs(r,c-1) + dfs(r,c+1) + + for r in range(rows): + for c in range(cols): + if grid[r][c] == '1': + islands += 1 + dfs(r,c) + + return islands + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_ways_to_select_building.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_ways_to_select_building.py new file mode 100644 index 00000000..dccb5781 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/number_of_ways_to_select_building.py @@ -0,0 +1,44 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def numberOfWays(self, s: str) -> int: + """ + Count valid 3-building selections forming "010" or "101" patterns. + + Key insight: Track how many 2-character patterns we can form, + then extend them with the appropriate third character. + + Time: O(n), Space: O(1) + """ + + count_0 = 0 # Count of '0' seen so far + count_1 = 0 # Count of '1' seen so far + count_01 = 0 # Count of "01" patterns (0 followed by 1) + count_10 = 0 # Count of "10" patterns (1 followed by 0) + + result = 0 + + for char in s: + if char == '0': + # Current '0' can complete "010": we need "01" before this '0' + result += count_01 + + # Current '0' can start new "01" patterns: pair with each previous '0' + # Wait no, "01" means 0 then 1, so we can't create "01" with another 0 + + # Current '0' extends all previous '1' to form "10" pattern + count_10 += count_1 + + count_0 += 1 + else: # char == '1' + # Current '1' can complete "101": we need "10" before this '1' + result += count_10 + + # Current '1' extends all previous '0' to form "01" pattern + count_01 += count_0 + + count_1 += 1 + + return result + diff --git a/src/my_project/interviews/top_150_questions_round_21/pow_x_n.py b/src/my_project/interviews/top_150_questions_round_21/pow_x_n.py new file mode 100644 index 00000000..70b662d3 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/pow_x_n.py @@ -0,0 +1,14 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def myPow(self, x, n): + + if not n: + return 1 + if n < 1: + return self.myPow(1/x, -n) + if n % 2: + return x*self.myPow(x,n-1) + else: + return self.myPow(x**2,n//2) \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_pow_x_n_round_21.py b/tests/test_150_questions_round_21/test_pow_x_n_round_21.py new file mode 100644 index 00000000..54147ffa --- /dev/null +++ b/tests/test_150_questions_round_21/test_pow_x_n_round_21.py @@ -0,0 +1,29 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.pow_x_n import Solution + +class PowxnTestCase(unittest.TestCase): + + def test_powxn_zero_power(self): + solution = Solution() + output = solution.myPow(x=2, n=0) + target = 1 + self.assertEqual(output, target) + + def test_powxn_negative_power(self): + solution = Solution() + output = solution.myPow(x=2, n=-1) + target = 0.5 + self.assertEqual(output, target) + + def test_powxn_odd_power(self): + solution = Solution() + output = solution.myPow(x=2, n=1) + target = 2 + self.assertEqual(output, target) + + def test_powxn_even_power(self): + solution = Solution() + output = solution.myPow(x=2, n=2) + target = 4 + self.assertEqual(output, target) \ No newline at end of file