A computer science portal for geeks



data structures : amazon

Home Forums Interview Questions data structures : amazon

This topic contains 16 replies, has 6 voices, and was last updated by  atul007 2 years, 8 months ago.

  • Author
  • #86139

    Given numbers 1 to 1000, suggest a data structure to store them such that following operations can be executed in constant time:

    1- insertion,

    2- deletion,

    3- searching,

    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.

  • #111563


    A binary map of size 1000.

    bool map[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()

  • #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).

  • #111565


    Could you please clarify the meaning get_any_number() . What are the input parameters and what is the expected output for this function?

  • #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.

  • #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 last

  • #111568


    Basically, it is kind of array implementation of Doubly Linked List.

  • #111569

    Can you please make it clearer, instead of pseudocode please explain in words.

  • #111570


    bool map[1000] 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 list

  • #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.

  • #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?

  • #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.

  • #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?

  • #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.

  • #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?

  • #111577


    @abzx12:How trie?Can You give more insight?

  • #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.