Google Interview Question
Software Engineer / Developersthe algorithm involves a parser for sure, but what are the operations it should support???
I think it's an open question. any operation you would like to see: +-*/^ sqrt, mod, km TO mile, etc...
Create a trie structure where each node can contain another trie. In the leaf node, an operation will be defined which takes in an input and returns an output.
Class TrieNode {
TrieNode nextNode;
Trie trie;
Operator op;
boolean isOperator;
}
Operator for C-F
public double CtoF(double degCelsius){
....
}
C*-[F*]
|
M*-[M-M*]
Using a stack for each word and when a specific word comes like "to" "+" "-" "/" "%" "*"
- cac October 10, 2010or another conversion word according to the operation(for ex. if to comes pop the stack to times and use them in the conversion operation) we can handle the operation.
To give an example:
"56 F to C"
stack content
56
56 F
56 F to -> to is get and
F to C operation is known by the program
according to the conversion 56 is changed to C