Junilu Lacar wrote:On second thought, I'm not sure a red-black tree would be that great at implementing autocorrect suggestions. I'm thinking maybe a hash table or map with the key as a Soundex code and the value as a list of words that have that same Soundex code. You'd have to implement your own list data structure and map, and you'd still want some kind of balanced tree to do quick searches of the Soundex code. That actually sounds like an interesting exercise. Of course, in the context of an interview, you have to consider how much time you are given to come up with a solution. You could start simple at first, then maybe discuss improvements when you get to the next round in the interview process.
Rafael Rodriguez wrote:I'm not allowed to implement any data structure or a map. When it comes to variables, I'm allowed to use only primitives (char, short, int,....) .
I read your suggestions, including what you wrote about Red-Black Tree, and I still don't see how to implement what I want with primitives only. Do you see a way for implementation, and can explain how do you implement it?
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:But must you go that distance? If I look at OP's first (given) code:
That does not seem all too heavy.
so my suggestion would be to either have this Tree[26] as Treee[27], with one element filled with, say, '\0', to indicate that a word is finished here,
Piet Souris wrote:A simple alternative is to have a Map<Character, List<String>>
Junilu Lacar wrote:
I don't think that's necessary. The end of a word would be when you get to a leaf node. Does your suggestion have anything to do with the comment about assuming that words will contain only small English letters? Because I think that actually means that it's OK to assume that the input only contains lowercase letters.
Junilu wrote:OP said he was specifically not allowed to do that. I take "primitives" to mean primitive data types and arrays. OP would have to create his own abstract data type like Map or List or, in this case, Tree.
There are three kinds of actuaries: those who can count, and those who can't.
Junilu Lacar wrote:It helps if you try to visualize what your object graph will look like.
Junilu Lacar wrote:It helps if you try to visualize what your object graph will look like. Here's my interpretation of what the tree that contains the words "hello" and "her" looks like:
Something else you might find useful: char variables are integer types so you can do math on them. That means that if you subtract 'a' from 'z', you get 25. That is:
Do you see how this might be useful when you have an array that can hold 26 things in it?
Piet Souris wrote:
Now, one of the two words is finished, so my suggestion would be to either have this Tree[26] as Treee[27], with one element filled with, say, '\0', to indicate that a word is finished here, while we also have a Tree element containg the char 'r'.
...
Interestingl Hope to hear.
Rafael Rodrigez wrote:(...)
I can't use Object, Array, String or any other non-primitive.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:
That brings me to my earlier suggestion and that is to 'store' the complete word in every Tree that is marked with the boolean 'isComplete'.
Rafael Rodrigez wrote:
The problem is that I don't know how to implement this without using non-primitives (besides non-primitives that were parameters as I wrote in my post).
I.E. , I can use only char, short, int etc.
I can't use Object, Array, String or any other non-primitive.