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:
The Insertion, deletion and searching will take O(1) amount of time. To solve this problem, we will use one Boolean array.
Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. In a stack, adding and removing of elements are performed at single position which is known as "top". That means, new element is added at top of the stack and an element is removed from the top of the stack.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With