From 57abf280b294f96fb3a5467bec1052d49b2bc308 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 10 May 2026 04:57:01 -0600 Subject: [PATCH] adding udpates --- .../ex_12_insert_delete_get_random.py | 71 ++++++++++++++++++ .../ex_12_insert_delete_get_random.ts | 72 +++++++++++++++++++ ...st_12_insert_delete_get_random_round_22.py | 35 +++++++++ ...st_12_insert_delete_get_random_round_23.py | 35 +++++++++ 4 files changed, 213 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_23/ex_12_insert_delete_get_random.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_23/ex_12_insert_delete_get_random.ts create mode 100644 tests/test_150_questions_round_23/test_12_insert_delete_get_random_round_23.py diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_12_insert_delete_get_random.py b/src/my_project/interviews/top_150_questions_round_23/ex_12_insert_delete_get_random.py new file mode 100644 index 00000000..08f299ea --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_12_insert_delete_get_random.py @@ -0,0 +1,71 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import random + + +class RandomizedSet: + + def __init__(self): + self.data_map = {} # dictionary, aka map, aka hashtable, aka hashmap + self.data = [] # list aka array + + def insert(self, val: int) -> bool: + + # the problem indicates we need to return False if the item + # is already in the RandomizedSet---checking if it's in the + # dictionary is on average O(1) where as + # checking the array is on average O(n) + if val in self.data_map: + return False + + # add the element to the dictionary. Setting the value as the + # length of the list will accurately point to the index of the + # new element. (len(some_list) is equal to the index of the last item +1) + self.data_map[val] = len(self.data) + + # add to the list + self.data.append(val) + + return True + + def remove(self, val: int) -> bool: + + # again, if the item is not in the data_map, return False. + # we check the dictionary instead of the list due to lookup complexity + if not val in self.data_map: + return False + + # essentially, we're going to move the last element in the list + # into the location of the element we want to remove. + # this is a significantly more efficient operation than the obvious + # solution of removing the item and shifting the values of every item + # in the dicitionary to match their new position in the list + last_elem_in_list = self.data[-1] + index_of_elem_to_remove = self.data_map[val] + + self.data_map[last_elem_in_list] = index_of_elem_to_remove + self.data[index_of_elem_to_remove] = last_elem_in_list + + # change the last element in the list to now be the value of the element + # we want to remove + self.data[-1] = val + + # remove the last element in the list + self.data.pop() + + # remove the element to be removed from the dictionary + self.data_map.pop(val) + return True + + def getRandom(self) -> int: + # if running outside of leetcode, you need to `import random`. + # random.choice will randomly select an element from the list of data. + return random.choice(self.data) + + + +# Your RandomizedSet object will be instantiated and called as such: +# obj = RandomizedSet() +# param_1 = obj.insert(val) +# param_2 = obj.remove(val) +# param_3 = obj.getRandom() \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_12_insert_delete_get_random.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_12_insert_delete_get_random.ts new file mode 100644 index 00000000..d33ad85c --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_12_insert_delete_get_random.ts @@ -0,0 +1,72 @@ +class RandomizedSet { + private dataMap: Map; // dictionary, aka map, aka hashtable, aka hashmap + private data: number[]; // list aka array + + constructor() { + this.dataMap = new Map(); + this.data = []; + } + + insert(val: number): boolean { + // the problem indicates we need to return False if the item + // is already in the RandomizedSet---checking if it's in the + // dictionary is on average O(1) where as + // checking the array is on average O(n) + if (this.dataMap.has(val)) { + return false; + } + + // add the element to the dictionary. Setting the value as the + // length of the list will accurately point to the index of the + // new element. (len(some_list) is equal to the index of the last item +1) + this.dataMap.set(val, this.data.length); + + // add to the list + this.data.push(val); + + return true; + } + + remove(val: number): boolean { + // again, if the item is not in the dataMap, return false. + // we check the dictionary instead of the list due to lookup complexity + if (!this.dataMap.has(val)) { + return false; + } + + // essentially, we're going to move the last element in the list + // into the location of the element we want to remove. + // this is a significantly more efficient operation than the obvious + // solution of removing the item and shifting the values of every item + // in the dictionary to match their new position in the list + const lastElemInList = this.data[this.data.length - 1]; + const indexOfElemToRemove = this.dataMap.get(val)!; + + this.dataMap.set(lastElemInList, indexOfElemToRemove); + this.data[indexOfElemToRemove] = lastElemInList; + + // change the last element in the list to now be the value of the element + // we want to remove + this.data[this.data.length - 1] = val; + + // remove the last element in the list + this.data.pop(); + + // remove the element to be removed from the dictionary + this.dataMap.delete(val); + return true; + } + + getRandom(): number { + // random.choice will randomly select an element from the list of data. + return this.data[Math.floor(Math.random() * this.data.length)]; + } +} + +/** + * Your RandomizedSet object will be instantiated and called as such: + * var obj = new RandomizedSet() + * var param_1 = obj.insert(val) + * var param_2 = obj.remove(val) + * var param_3 = obj.getRandom() + */ \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py b/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py index e69de29b..b9dc7b2a 100644 --- a/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py +++ b/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py @@ -0,0 +1,35 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_12_insert_delete_get_random import RandomizedSet + +class InsertAndDeleteGetRandomTestCase(unittest.TestCase): + + def test_first_case(self): + randomized_set = RandomizedSet() + self.assertTrue(randomized_set.insert(1)) # Inserts 1, returns true + self.assertFalse(randomized_set.remove(2)) # 2 not present, returns false + self.assertTrue(randomized_set.insert(2)) # Inserts 2, returns true + self.assertIn(randomized_set.getRandom(), [1, 2]) # getRandom returns 1 or 2 + self.assertTrue(randomized_set.remove(1)) # Removes 1, returns true + self.assertFalse(randomized_set.insert(2)) # 2 already present, returns false + self.assertEqual(randomized_set.getRandom(), 2) # Only 2 in set + + def test_insert_duplicate(self): + randomized_set = RandomizedSet() + self.assertTrue(randomized_set.insert(5)) + self.assertFalse(randomized_set.insert(5)) # duplicate + + def test_remove_nonexistent(self): + randomized_set = RandomizedSet() + self.assertFalse(randomized_set.remove(99)) + + def test_remove_existing(self): + randomized_set = RandomizedSet() + randomized_set.insert(10) + self.assertTrue(randomized_set.remove(10)) + self.assertFalse(randomized_set.remove(10)) # already removed + + def test_get_random_single_element(self): + randomized_set = RandomizedSet() + randomized_set.insert(42) + self.assertEqual(randomized_set.getRandom(), 42) diff --git a/tests/test_150_questions_round_23/test_12_insert_delete_get_random_round_23.py b/tests/test_150_questions_round_23/test_12_insert_delete_get_random_round_23.py new file mode 100644 index 00000000..f9272f12 --- /dev/null +++ b/tests/test_150_questions_round_23/test_12_insert_delete_get_random_round_23.py @@ -0,0 +1,35 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_12_insert_delete_get_random import RandomizedSet + +class InsertAndDeleteGetRandomTestCase(unittest.TestCase): + + def test_first_case(self): + randomized_set = RandomizedSet() + self.assertTrue(randomized_set.insert(1)) # Inserts 1, returns true + self.assertFalse(randomized_set.remove(2)) # 2 not present, returns false + self.assertTrue(randomized_set.insert(2)) # Inserts 2, returns true + self.assertIn(randomized_set.getRandom(), [1, 2]) # getRandom returns 1 or 2 + self.assertTrue(randomized_set.remove(1)) # Removes 1, returns true + self.assertFalse(randomized_set.insert(2)) # 2 already present, returns false + self.assertEqual(randomized_set.getRandom(), 2) # Only 2 in set + + def test_insert_duplicate(self): + randomized_set = RandomizedSet() + self.assertTrue(randomized_set.insert(5)) + self.assertFalse(randomized_set.insert(5)) # duplicate + + def test_remove_nonexistent(self): + randomized_set = RandomizedSet() + self.assertFalse(randomized_set.remove(99)) + + def test_remove_existing(self): + randomized_set = RandomizedSet() + randomized_set.insert(10) + self.assertTrue(randomized_set.remove(10)) + self.assertFalse(randomized_set.remove(10)) # already removed + + def test_get_random_single_element(self): + randomized_set = RandomizedSet() + randomized_set.insert(42) + self.assertEqual(randomized_set.getRandom(), 42)