Design a data structure with
- O(1) searching
- O(n) printing in the order the elements are received
- O(1) insertion
Answer: This data structure will keep a combination of two other data structures
- a hash table
- linked list
When searching, use the hash table. Since there's no guarantee of order in a hash table, when printing, use the linked list. Insertion into both data structures is O(1). Notice there are no restrictions on deleting elements. Also, keep pointers instead of the actual objects to reduce redundancy and save memory space.
No comments:
Post a Comment