https://leetcode.com/problems/insert-delete-getrandom-o1
I got the same with string as key and value instead of int.
public class ClassA
{
public Dictionary<int,int> map;
public List nums;
public Random rand;
public RandomizedSet() {
map = new Dictionary<int,int>();
nums = new List();
rand = new Random();
}
public bool Insert(int val) {
if(map.ContainsKey(val))
return false;
nums.Add(val);
map.Add(val,nums.Count()-1);
return true;
}
public bool Remove(int val) {
if(!map.ContainsKey(val))
return false;
var idx = map[val];
nums[idx]=nums[nums.Count()-1];
map[nums[idx]]=idx;
map.Remove(val);
nums.RemoveAt(nums.Count()-1);
return true;
}
public int GetRandom() {
return nums[rand.Next(0,nums.Count())];
}
}
I gave them above solution and of course with modification to work with strings insted of int. The test cases worked according what interviewer asked.
However, I got a rejection email within 2 hours of this interview. I don't understand.