Set - Google Top Interview Questions
Problem Statement :
Implement a set data structure with the following methods: CustomSet() constructs a new instance of a set add(int val) adds val to the set exists(int val) returns whether val exists in the set remove(int val) removes the val in the set This should be implemented without using built-in set. Constraints n ≤ 100,000 where n is the number of calls to add, exists and remove Example 1 Input methods = ["constructor", "add", "exists", "remove", "exists"] arguments = [[], [1], [1], [1], [1]]` Output [None, None, True, None, False] Explanation c = CustomSet() c.add(1) c.exists(1) == True c.remove(1) c.exists(1) == False
Solution :
Solution in C++ :
class CustomSet {
int S = 1e3;
vector<list<int>> v;
hash<int> hs;
public:
CustomSet() {
v.resize(S);
}
void add(int val) {
if (exists(val)) return;
int id = hs(val) % S;
v[id].push_back(val);
}
bool exists(int val) {
int id = hs(val) % S;
for (int x : v[id]) {
if (x == val) return true;
}
return false;
}
void remove(int val) {
int id = hs(val) % S;
for (auto it = v[id].begin(); it != v[id].end(); it++) {
if (*it == val) {
v[id].erase(it);
return;
}
}
}
};
Solution in Java :
import java.util.*;
class CustomSet {
ArrayList<Integer>[] buckets;
int T;
public CustomSet() {
T = 300;
buckets = new ArrayList[T];
}
public void add(int val) {
if (!exists(val)) {
buckets[map(val)].add(val);
}
}
public boolean exists(int val) {
int m = map(val);
if (buckets[m] == null)
buckets[m] = new ArrayList<Integer>();
return buckets[m].contains(val);
}
public void remove(int val) {
if (exists(val))
buckets[map(val)].remove(Integer.valueOf(val));
}
public int map(int val) {
return ((val % T) + T) % T;
}
}
Solution in Python :
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
class CustomSet:
def __init__(self):
self.mod = 2069 # any large prime number
self.lst = [None] * self.mod
def add(self, val):
hashkey = val % self.mod
if not self.lst[hashkey]:
self.lst[hashkey] = ListNode(val)
else:
curr = self.lst[hashkey]
while curr.next:
if curr.val == val:
return
curr = curr.next
if curr.val == val:
return
curr.next = ListNode(val)
def exists(self, val):
hashkey = val % self.mod
if self.lst[hashkey]:
curr = self.lst[hashkey]
while curr:
if curr.val == val:
return True
curr = curr.next
return False
def remove(self, val):
hashkey = val % self.mod
if self.lst[hashkey]:
curr = self.lst[hashkey]
if curr.val == val:
self.lst[hashkey] = curr.next
return
while curr.next:
if curr.next.val == val:
curr.next = curr.next.next
return
curr = curr.next
View More Similar Problems
Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →