diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_20.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_20.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_20.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_20.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_20.py new file mode 100644 index 00000000..dfc57cd7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_20.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=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_5/design_sql.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/design_sql.py new file mode 100644 index 00000000..3e929650 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/design_sql.py @@ -0,0 +1,111 @@ +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 + + + + + +# Your SQL object will be instantiated and called as such: +# obj = SQL(names, columns) +# param_1 = obj.ins(name,row) +# obj.rmv(name,rowId) +# param_3 = obj.sel(name,rowId,columnId) +# param_4 = obj.exp(name) \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py new file mode 100644 index 00000000..cebb1d31 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py @@ -0,0 +1,14 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import math + +class Solution: + def minimumTime(self, jobs: List[int], workers: List[int]) -> int: + + jobs.sort() + workers.sort() + + return max([math.ceil(n/d) for n,d in zip(jobs, workers)]) + + + diff --git a/src/my_project/interviews/top_150_questions_round_22/merge_sorted_array.py b/src/my_project/interviews/top_150_questions_round_22/merge_sorted_array.py new file mode 100644 index 00000000..aa9bb971 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/merge_sorted_array.py @@ -0,0 +1,13 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def merge(self, + nums1: List[int], + m: int, nums2: List[int], + n: int) -> None: + + nums1[m:m+n] = nums2 + nums1.sort() + + return nums1 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/remove_element.py b/src/my_project/interviews/top_150_questions_round_22/remove_element.py new file mode 100644 index 00000000..8521e400 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/remove_element.py @@ -0,0 +1,11 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + + def removeElement(self, nums: List[int], val: int) -> int: + + while val in nums: + nums.remove(val) + + return len(nums) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_merge_sorted_array_round_22.py b/tests/test_150_questions_round_22/test_merge_sorted_array_round_22.py new file mode 100644 index 00000000..35ea220c --- /dev/null +++ b/tests/test_150_questions_round_22/test_merge_sorted_array_round_22.py @@ -0,0 +1,12 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.merge_sorted_array import Solution + +class MergeSortedArrayTestCase(unittest.TestCase): + + def test_merge_sorted_array(self): + solution = Solution() + output = solution.merge(nums1=[1,2,3,0,0,0], m=3, nums2=[2,5,6], n=3) + target = [1,2,2,3,5,6] + for k, v in enumerate(target): + self.assertEqual(output[k], v) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_remove_element_round_22.py b/tests/test_150_questions_round_22/test_remove_element_round_22.py new file mode 100644 index 00000000..5e5bd4b0 --- /dev/null +++ b/tests/test_150_questions_round_22/test_remove_element_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.remove_element import Solution + +class RemoveElementTestCase(unittest.TestCase): + + def test_remove_element(self): + solution = Solution() + output = solution.removeElement(nums=[3,2,2,3], val=3) + target = 2 + self.assertEqual(output, target) \ No newline at end of file