Note that there are some explanatory texts on larger screens.

plurals
  1. POData Structure that supports queue like operations and mode finding
    primarykey
    data
    text
    <p>This was an interview question asked to me almost 3 years back and I was pondering about this a while back. </p> <blockquote> <p>Design a data structure that supports the following operations: insert_back(), remove_front() and find_mode(). Best complexity required.</p> </blockquote> <p>The best solution I could think of was O(logn) for insertion and deletion and O(1) for mode. This is how I solved it: Keep a queue DS for handling which element is inserted and deleted. Also keep an array which is max heap ordered and a hash table. The hashtable contains an integer key and an index into the heap array location of that element. The heap array contains an ordered pair (count,element) and is ordered on the count property.</p> <p>Insertion : Insert the element into the queue. Find the location of the heap array index from the hashtable. If none exists, then add the element to the heap and heapify upwards. Then add the final location into the hashtable. Increment the count in that location and heapify upwards or downwards as needed to restore the heap property. </p> <p>Deletion : Remove element from the head of the queue. From the hash table, find a location in the heap array index. Decrement the count in the heap and reheapify upward or downwards as needed to restore the heap property. </p> <p>Find Mode: The element at the head of the array heap (getMax()) will give us the mode.</p> <p>Can someone please suggest something better. The only optimization I could think of was using a Fibonacci heap but I am not sure if that is a good fit in this problem. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload