I was given this problem in an interview. How would you have answered?
Design a data structure that offers the following operations in O(1) time:
- insert
- remove
- contains
- get random element
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Consider a data structure composed of a hashtable H and an array A. The hashtable keys are the elements in the data structure, and the values are their positions in the array.
since the array needs to auto-increase in size, it’s going to be amortize O(1) to add an element, but I guess that’s OK.