AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: Mr. Google — October 17, 2006
    +17 votes
      + -

    This is the classic example of Divide and Conquer.

    According to folklore, Professor Skiena would demonstrate binary search by asking for a name, and then looking it up in the phonebook by checking the middle of the book, does a comparison, and literally tear out half the book and throw it in the trash.

    Remember that game 20 questions? You have to ask 20 questions to guess a word. Well, the way to cheat is to narrow it down half way every time. The English Dictionary has about 500,000 words, meaning you would still have queries to spare.

    Yes, it would be boring, but it would go something like this:

    Does your word come before the word “marry”? Yes
    Does your word come before the word “gerrymander”?

    You get the point.

    Assuming you already know you have to sort, you can binary search at O(log_2 N). Which for say, a trillion, is less than 50.

    But its application doesn’t stop there, as it (as well as its cousin ternary search) can help you solve numerical equations (this is actually call bisection, but same paradigm). For example, you want to find the cube root of N (N^(1/3)), but don’t have some sort of library ready. Easy solution? Binary search! You know x=y^3, so “guess” a value for x, and go from there!

  2. Submitted By: Peter — October 23, 2006
    +46 votes
      + -

    Dictionary’s doesn’t contain names.

  3. Submitted By: Anket Dhulekar — December 16, 2006
    +34 votes
      + -

    Well this was asked in one of my technical interviews. Thankfully i knew the answer. Anyone who has ever gone through “Introduction to Algos” would know that B-trees are used for this purpose. A B-tree has a single root node and can store a definite number of values. The concept of B-tree is difficult to explain through text only so heres an example.

    Suppose there we are considering only two values of a root node say Ab and Ad, then a child will lie between the two values and will contain all the entries such that

    (entry > Ab) and (entry

  4. Submitted By: Sasha — January 14, 2007
    -4 votes
      + -

    I would use trie - although the search is o(n) but you have other dictunary actions that is done efficiently in such data structure

  5. Submitted By: Tushar Agrawal — March 10, 2007
    -2 votes
      + -

    I agree with sasha.. Trie should be the answer. When looking for a word in dictionary, what we do practically is nothing but using trie.

    @Sasha: search is O(1) and not O(n) in case of trie :)

  6. Submitted By: darxoul — March 20, 2007
    +5 votes
      + -

    using binary search is fine, but we do something smarter when using a dictionary: we know that A is at the very beginning of the dictionary and L is somewhere in the middle and do the binary search accordingly. No one would open the middle of the dictionary to look up “abuse”. It is obviously at the very first pages of the dict. So a “smart” binary search algorithm is used, I can say.

  7. Submitted By: Shanmugasundaram — April 5, 2007
    +5 votes
      + -

    For lookup name in a dictionary,
    everyone following one method but without knowing name that is called “Binary Search-Indexing”.

    For Example, if we want search name “INDIA”

    First we will go to the “I” Index.

    Then for Sorted names in I Index

  8. Submitted By: Ben Dover — April 11, 2007
    +6 votes
      + -

    Technically, all of those answers are right. However, major companies are looking for the most efficient way. By saying you use a trie, you search basically the way most people do. Slightly brighter people use binary searches, because it is much quicker. Even brighter people use B-Trees. A binary search is basically a B-tree search where the B-tree node has only 1 key and 2 pointers. There is an optimum B-tree that will have a search that is log_n().
    O(log_n) 2.

  9. Submitted By: Ben Dover — April 11, 2007
    +0 votes
      + -

    ignore the last line, i was trying to say O(log_n x) is quicker than O(log_2 x) which is quicker than O(x).

  10. Submitted By: IM Way N — May 16, 2007
    -4 votes
      + -

    If I were only going to be looking up words that were names of either a person, place or thing, I would first eliminate all other non-name words from the dictionary.
    (Oh, this solution assumes that there will be millions of like requests.)
    Put the names in an electronic dictionary and simply type the name into some standard word-search engine.
    Since the (search) time differential between excluding or including non-names (e.g., verbs and adjectives) is pretty small, you could just include the non-names if your search count is in the thousands or hundreds of thousands or if the output is to go directly to a human instead of another machine.

  11. Submitted By: Tik — May 20, 2007
    +3 votes
      + -

    Using a trie is better than binary search or a B-tree. Let us say that the input word to look for is of length w. In order to compare the input word with some word in the dictionary in binary search it takes O(w) time. So if there are n words in the dictionary, it takes O(w*log(n)) time. A similar argument applies for a B-tree also. But with a trie, it only takes O(w) time to search. Morover, the space overhead for a trie is less because most nodes are reused. If we have a sparse dictionary then Patricia trees could be used to eliminate even more space overhead. So I would say that a trie is a better solution.

  12. Submitted By: Manjit — May 24, 2007
    -4 votes
      + -

    Basically a bubble search.

  13. Submitted By: peeyush — July 14, 2007
    -1 votes
      + -

    We can use hashing method for implementing dictionary.
    For each word in a dictionary we will generate the unique no. This all no. are sorted. When a user have to search for a word..he will use the same formula for number generation . THen that no. will be searched in given list of unique no. If no. found then it will display the details of the word in a dictonary .

  14. Submitted By: moiriba — August 27, 2007
    -1 votes
      + -

    I guess, we follow the multi-level hashing method in practical.

  15. Submitted By: sameer — November 12, 2007
    not yet rated
      + -

    i guess i read something known as tries in my college days and thats how dictionaries work, so use tries(WIKI will tell you) and you are done in O(1) time
    cheers

  16. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.