Hi,
Today I attended a written test conducted by a company. The overall test was focussed on data structures. I got a problem which I thought I solved. But I had a tough time in calculating the Big O function for the data structure. I will provide the question and the answer I came up with.
Given a document you need to store and the words in the document and should be able to return the count when any word is entered. You are provided with
char* GetNextWord()
.
- What Data structure will you choose
- Give the Algorithm
- What will be the order of your algorithm
For question 1, I wrote I will go for TRIE data structure. For question 2, I gave a brief algorithm. I wrote I would construct the TRIE data structure as following.
struct TRIE{
boolean isWord;
int count;
Node* myList;
}
struct Node{
char* character;
Node *next;
TRIE *child;
}
I have methods constructTrie()
which will do a addToTrie()
for each word.
I wrote the order of addToTrie()
would be O(k) where k is the length. And the order of constructTrie()
would be N*O(k) where N would be the number of words.
Now my question is this: Whether the orders I've mentioned is correct or not? If not, how to attack problems like this in the future (given a ds finding the order). I got really confused after using O(k). It makes me assume O(1).
Hints/Tips/Advise are wide open!!
Edit : Corrected the question clearly mentioning that the word count should be stored for all unique words.