Tagged: Data Structures
February 6, 2012 at 4:13 PM #86139
Given numbers 1 to 1000, suggest a data structure to store them such that following operations can be executed in constant time:
4- get_any_number (means return any number if present in the data-structure otherwise return -1).
NOTE: Numbers are unique in the data structure.February 7, 2012 at 2:58 AM #111563
A binary map of size 1000.
To insert an element, do map = true
To delete an element, do map = false
Search(i): return map
get_any_number() would be similar to Search()February 7, 2012 at 3:40 PM #111564
get_any_number() wont be similar to Search() , if you follow it has to follow an upper bound of ‘n’ i.e. O(n).February 7, 2012 at 3:50 PM #111565
Could you please clarify the meaning get_any_number() . What are the input parameters and what is the expected output for this function?February 7, 2012 at 4:26 PM #111566
lets assume we have numbers 1,2,5 in the data structure.. if we call get_any_number() it should return any out of 1,2 or 5 , Now lets delete 2 , and again we call get_any_number() and it should now return any out of 1 or 5. That’s it.February 7, 2012 at 5:41 PM #111567
You can create two more arrays say next and prev of same size. The next array will contain index of next available number in map and the prev array will contain index of previous available number in map . A variable last is also needed that keeps track of last inserted number.
To insert an element, do following
map = true,
next[last] = i,
prev = last
last = i
To delete an element, do following
map = false,
next[prev] = next,
prev[next] = prev
if(last == i)
last = prev
Search(i): return map
get_any_number() : return lastFebruary 7, 2012 at 5:57 PM #111568
Basically, it is kind of array implementation of Doubly Linked List.February 8, 2012 at 4:30 PM #111569
Can you please make it clearer, instead of pseudocode please explain in words.February 9, 2012 at 10:35 AM #111570
bool map is kind of a array implementation…
can you please elaborate get_any_number() in a pseudocode
i dont understand the second concept which is somewhat like doubly linked listFebruary 9, 2012 at 12:14 PM #111571
A variable last is used to keep track of the last inserted number. If a delete operation happens such that it deletes the last element, we update the last to prev of the last. Since get_any_number() is supposed to return any number in the set, we always return last number. When we insert an element, we update last to contain the new index.February 9, 2012 at 5:49 PM #111572
what if delete operation deletes any old number(not last) , would delete be O(1) anymore? Anyways may be i am not catching up with the solution, can you please elaborate the approach? How can we make deletion of a particular element from doubly linked list taking constant time?February 9, 2012 at 6:09 PM #111573
@abzx12: Take a closer look at the delete operation, it changes last only if the element to be deleted is the last element.
Given a pointer to the node to be deleted, it always takes constant time to delete the node in doubly linked list. All you need to do is change some pointers.
Probably, I will publish this as a post with complete code.February 14, 2012 at 4:13 PM #111574
@kartik : Nice solution and application of doubly linked list.
I guess we can also use TRIE as its solution, it supports all operations in constant time. Can you please throw some light on such a solution?February 15, 2012 at 10:36 AM #111575
get_random_number is not implemented in this doubly linked list soln. effectively as it will always return the last element inserted that is not yet deleted.February 15, 2012 at 4:31 PM #111576
Although that wasn’t a requirement in constant time, still get_random_number() can be solved in O(n) time with doubly linked list.
How about using a TRIE?February 24, 2012 at 4:52 PM #111577
@abzx12:How trie?Can You give more insight?March 16, 2012 at 5:14 AM #111578
why taking so much pain in implementing using array ,
use hashtable instead ,
search , insert, delete – O(1).
take extra variable count , to keep track of the total number of elements_till_now in hashtable.
and as it is mentioned that get random is not required in constant time , so i guess it can be done.
You must be logged in to reply to this topic.