It is a google interview question and I find most answers online using HashMap or similar data structure. I am trying to find a solution using Trie if possible. Anybody could gi
Assuming a large dictionary and a letter set with less than 10 or 11 members (such as the example given), the fastest method is build a tree containing the possible words the letters can make, then match the word list against the tree. In other words your letter tree's root has seven subnodes: { a, e, f, g, i, r, q }. The branch of "a" has six subnodes { e, f, g, i, r, q }, etc. The tree thus contains every possible word which can be made with these letters.
Go through each word in the list and match it to the tree. If the match is maximum length (uses all the letters), you are done. If the word is less then max, but longer than any previously matched word, remember it, this is the "longest word so far" (LWSF). Ignore any words that have a length equal to less than the LWSF. Also, ignore any words which are longer than the length of the letter list.
This is a linear time algorithm once the letter tree is constructed, so as long as the word list is significantly larger than the letter tree, it is fastest method.