Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
use hashset as it does't allow duplicates..time complexity to add elements would be O(n).
'''Python3.3:
Removing duplicates on the fly
E.g.:
a
v
a
then answer:
a, v
'''
if __name__ == "__main__":
name_space = {}
ordered_list = []
while True:
_name = input("")
if(_name == ""): break
if _name not in name_space:
name_space[_name] = 1
ordered_list.append(_name)
print(list(name_space.keys()))
print("ordered list: ", ordered_list)
Here is my solution in C++:
void unique_stream(vector<string> &stream)
{
unordered_map<string, int> strMap;
vector<string> vecResult;
for(auto& str: stream)
{
strMap.find(str) != strMap.end() ? ++strMap[str] : strMap[str] = 1 ;
}
for(auto& str: stream)
{
if(strMap[str] == 1)
vecResult.push_back(str);
}
stream = vecResult;
}
List<String> lstStr = new ArrayList<String>();
lstStr.add("Nuno");
lstStr.add("Paulo");
lstStr.add("Carlos");
lstStr.add("Ana");
lstStr.add("Nuno");
lstStr.add("Paulo");
lstStr.add("Nuno");
lstStr.add("Paulo");
lstStr.add("Sofia");
lstStr.add("Sofia");
lstStr.add("Sofia");
lstStr.add("Sofia");
lstStr.add("Rita");
for(int i=0; i< lstStr.size(); i++)
{
for(int j=1;j<lstStr.size();j++)
{
if(i!= j && lstStr.get(i) == lstStr.get(j))
{
lstStr.remove(j);
j--;
}
if(j>=lstStr.size())
break;
}
if(i>=lstStr.size())
break;
}
for(int k = 0; k<lstStr.size();k++)
{
System.out.println(lstStr.get(k))
}
use TRIE
- truecool June 02, 2014===============
wwwDOTgeeksforgeeks.org/trie-insert-and-search/+&cd=1&hl=en&ct=clnk&gl=in
==================
Using trie, search complexities can be brought to optimal limit (key length). If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree. Using trie, we can search the key in O(M) time. However the penalty is on trie storage requirements.