From 17a036e65e4184b895250705cb092fb804e90b5d Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 7 Jun 2026 05:05:28 -0600 Subject: [PATCH] adding updates --- .../common_algos/two_sum_round_7.py | 16 +++ .../common_algos/valid_palindrome_round_7.py | 22 ++++ .../round_6/design_sql.py | 102 ++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_6/design_sql.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..2d8643f2 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.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_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..b9954582 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.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/amazon_high_frequency_23/round_6/design_sql.py b/src/my_project/interviews/amazon_high_frequency_23/round_6/design_sql.py new file mode 100644 index 00000000..44d986c9 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_6/design_sql.py @@ -0,0 +1,102 @@ +from typing import List, Union, Collection, Mapping, Optional, Dict + +class Table: + + def __init__(self, name:str, columns: int, rows: Dict, row_count: int = 0): + self.name = name + self.columns = columns + self.rows = rows + self.row_count = row_count + +class SQL: + + def __init__(self, names: List[str], columns: List[int]): + """ + two string arrays, names and columns, both of size n. + + The ith table is represented by the name names[i] and contains columns[i] number of columns + """ + self.names = names + self.columns = columns + self.data = {} + + for i in range(0, len(self.names)): + self.data[self.names[i]] = Table( + name = self.names[i], + columns=self.columns[i], + rows={} + ) + + return + + + def ins(self, name: str, row: List[str]) -> bool: + """ + Inserts row into the table name and returns true. + If row.length does not match the expected number of columns, or name is not a valid table, + returns false without any insertion. + """ + table: Table = self.data.get(name) + if table is None: + return False + if len(row) != table.columns: + return False + table.row_count += 1 + table.rows[table.row_count] = row + + return True + + + def rmv(self, name: str, rowId: int) -> None: + """ + Removes the row rowId from the table name. + If name is not a valid table or there is no row with id rowId, no removal is performed. + """ + table: Table = self.data.get(name, None) + if table is None: + return + + if rowId in table.rows: + del table.rows[rowId] + + return + + + def sel(self, name: str, rowId: int, columnId: int) -> str: + """ + Returns the value of the cell at the specified rowId and columnId in the table name. + If name is not a valid table, or the cell (rowId, columnId) is invalid, returns "". + """ + null = "" + table: Table = self.data.get(name, None) + if table is None: + return null + + if rowId not in table.rows: + return null + + try: + return table.rows[rowId][columnId - 1] + except Exception: + return null + + + def exp(self, name: str) -> List[str]: + """ + Returns the rows present in the table name. + If name is not a valid table, returns an empty array. + Each row is represented as a string, with each cell value (including the row's id) separated by a ",". + """ + table: Table = self.data.get(name, None) + if table is None: + return [] + + result = [] + + for row_id, row in table.rows.items(): + row_str = ",".join(row) + result.append(f"{row_id},{row_str}") + + return result + + \ No newline at end of file