Java - Collections - Interview Questions and Answers on Search

Q1.  Can we use Ordered Set for performing Binary Search ?

Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets.

Q2.  What are the pre-requisite for the collection to perform Binary Search ?

Ans. 

1. Collection should have an index for random access.
2. Collection should have ordered elements.

Q3.  Can you provide some implementation of a Dictionary having large number of words ? 

Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.

Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.

Further level up, we can have linked Hashmaps like ,

hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
}

upto n levels ( where n is the average size of the word in dictionary.