AFFIRM | OA ROUND 2| Random Access With Duplicates
Anonymous User
2706

The question was implement a version of hash Map that has following functions

put(String K,int v)
delete(k) -> Initially ->(n)
get(k) - Initalliy -> O(n)
findRandom() - return a random value time complexity has to be 0(1)

There can be duplicate values in the map. and random needs to account for it

Very similar to https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
but with caveat of there also being a string key.

Same as here.
https://leetcode.com/discuss/interview-question/1591333/Affirm-or-phone-interview-or-Insert-Delete-GetRandom-O(1)

The trick is in ArrayList should be of type Pair<String,Integer> instead of integer.

Challenges:
1)Puts gets a bit challenging since a new value can come for same key
2)Edge case when remove is the last index itselt

Followups
1)How can we change this such that the probability does not matter on frequency of values . i.e
A->1
B->1
C->2

probability of 1 is 1/2 and 2 is 1/2 (unlike before where 1 is 2/3 and 2 is 1/3)

2)How can you make the put operation in o(1) time.
If we use ArrayList<Pair<>> instead of ArrayList this becomes trivial.

Comments (3)