Cisco Systems Interview Question
Software Engineer / DevelopersWe can implement this using a queue which implements hash table.
1) We maintain a head an tail pointer.
2) A new entry is inserted into the queue based on hash index.
3) Next pointer of tail is updated to pointer to this location.
i.e. tail->next = newPointer.
4) Tail is updated to point to the new location.
tail = newPointer.
This way searching in the queue takes O(1) time, enqueue takes O(1) time and dequeue takes O(1) time with little overhead of having to store a pointer variable along with the key value.
Hash entry from input queue, use %3 to decide which queue it goes into:
- DN March 14, 2012