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 new file mode 100644 index 00000000..2fa397c1 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py @@ -0,0 +1,23 @@ +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_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..cad2b1e6 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.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_87_evaluate_division.py b/src/my_project/interviews/top_150_questions_round_22/ex_87_evaluate_division.py new file mode 100644 index 00000000..e61e3b42 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_87_evaluate_division.py @@ -0,0 +1,46 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class Solution: + def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: + # Build graph: adjacency list with weights + graph = defaultdict(dict) + + for (dividend, divisor), value in zip(equations, values): + graph[dividend][divisor] = value + graph[divisor][dividend] = 1.0 / value + + def bfs(start: str, end: str) -> float: + # Check if variables exist in graph + if start not in graph or end not in graph: + return -1.0 + + # If start equals end and it exists in graph + if start == end: + return 1.0 + + # BFS to find path from start to end + queue = deque([(start, 1.0)]) # (node, accumulated_product) + visited = {start} + + while queue: + node, product = queue.popleft() + + # Check all neighbors + for neighbor, weight in graph[node].items(): + if neighbor == end: + return product * weight + + if neighbor not in visited: + visited.add(neighbor) + queue.append((neighbor, product * weight)) + + return -1.0 + + # Process all queries + results = [] + for dividend, divisor in queries: + results.append(bfs(dividend, divisor)) + + return results \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_87_evaluate_division.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_87_evaluate_division.ts new file mode 100644 index 00000000..7df54b70 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_87_evaluate_division.ts @@ -0,0 +1,62 @@ +function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] { + // Build graph: adjacency list with weights + const graph = new Map>(); + + for (let i = 0; i < equations.length; i++) { + const [dividend, divisor] = equations[i]; + const value = values[i]; + + if (!graph.has(dividend)) { + graph.set(dividend, new Map()); + } + if (!graph.has(divisor)) { + graph.set(divisor, new Map()); + } + + graph.get(dividend)!.set(divisor, value); + graph.get(divisor)!.set(dividend, 1.0 / value); + } + + function bfs(start: string, end: string): number { + // Check if variables exist in graph + if (!graph.has(start) || !graph.has(end)) { + return -1.0; + } + + // If start equals end and it exists in graph + if (start === end) { + return 1.0; + } + + // BFS to find path from start to end + const queue: [string, number][] = [[start, 1.0]]; // [node, accumulated_product] + const visited = new Set([start]); + + while (queue.length > 0) { + const [node, product] = queue.shift()!; + + // Check all neighbors + const neighbors = graph.get(node)!; + for (const [neighbor, weight] of neighbors.entries()) { + if (neighbor === end) { + return product * weight; + } + + if (!visited.has(neighbor)) { + visited.add(neighbor); + queue.push([neighbor, product * weight]); + } + } + } + + return -1.0; + } + + // Process all queries + const results: number[] = []; + for (const [dividend, divisor] of queries) { + results.push(bfs(dividend, divisor)); + } + + return results; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_86_clone_graph_round_22.py b/tests/test_150_questions_round_22/test_86_clone_graph_round_22.py index 622809a5..4bd23da2 100644 --- a/tests/test_150_questions_round_22/test_86_clone_graph_round_22.py +++ b/tests/test_150_questions_round_22/test_86_clone_graph_round_22.py @@ -1,5 +1,6 @@ import unittest -from src.my_project.interviews.top_150_questions_round_22.ex_86_clone_graph import Solution, Node +from src.my_project.interviews.top_150_questions_round_22\ + .ex_86_clone_graph import Solution, Node from typing import Optional, List diff --git a/tests/test_150_questions_round_22/test_87_evaluate_division_round_22.py b/tests/test_150_questions_round_22/test_87_evaluate_division_round_22.py new file mode 100644 index 00000000..c64e6ef9 --- /dev/null +++ b/tests/test_150_questions_round_22/test_87_evaluate_division_round_22.py @@ -0,0 +1,56 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ + .ex_87_evaluate_division import Solution +from typing import Optional, List + + +class EvaluateDivisionTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Example 1: Basic division chain + Given: a / b = 2.0, b / c = 3.0 + queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? + return: [6.0, 0.5, -1.0, 1.0, -1.0] + """ + solution = Solution() + equations = [["a", "b"], ["b", "c"]] + values = [2.0, 3.0] + queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]] + expected = [6.0, 0.5, -1.0, 1.0, -1.0] + + result = solution.calcEquation(equations, values, queries) + self.assertEqual(result, expected) + + def test_example_2(self): + """ + Example 2: Multiple variables and chains + Given: a / b = 1.5, b / c = 2.5, bc / cd = 5.0 + queries are: a / c = ?, c / b = ?, bc / cd = ?, cd / bc = ? + return: [3.75, 0.4, 5.0, 0.2] + """ + solution = Solution() + equations = [["a", "b"], ["b", "c"], ["bc", "cd"]] + values = [1.5, 2.5, 5.0] + queries = [["a", "c"], ["c", "b"], ["bc", "cd"], ["cd", "bc"]] + expected = [3.75, 0.4, 5.0, 0.2] + + result = solution.calcEquation(equations, values, queries) + self.assertEqual(result, expected) + + def test_example_3(self): + """ + Example 3: Single equation with various queries + Given: a / b = 0.5 + queries are: a / b = ?, b / a = ?, a / c = ?, x / y = ? + return: [0.5, 2.0, -1.0, -1.0] + """ + solution = Solution() + equations = [["a", "b"]] + values = [0.5] + queries = [["a", "b"], ["b", "a"], ["a", "c"], ["x", "y"]] + expected = [0.5, 2.0, -1.0, -1.0] + + result = solution.calcEquation(equations, values, queries) + self.assertEqual(result, expected) +