Amazon Interview Question
SDE1sTeam: TCS
Country: India
Interview Type: Written Test
I would use a HashMap. The keys are the characters. The value is the first position the character occurs or -1 if duplicate. The time complexity is O(n), n being the number of characters in the stream.
If we know the range of characters is the ASCII values, we can just use an array instead. If the characters can be any unicode char, then a hash map should be better.
// hasNext() and getNext() are already defined.
char getUniqueCharacter() {
HashMap<Character, Integer> hash = new HashMap<Character, Integer>();
int pos = 0;
while (hashNext()) {
pos++;
char c = getNext();
if (hash.containsKey(c))
hash.put(c, -1);
else
hash.put(c, pos);
}
char result = '#';
pos++; // inexistent position
for (Map.Entry<Character, Integer> entry : hash.entrySet()) {
int p = entry.getValue();
if (p != -1 && p < pos) {
pos = p;
result = entry.getKey();
}
}
return result;
}
good and ideal solution.
What if the input stream has a range of characters which is bigger than the memory available to hold them, in that case we cannot use in-memory hashmap.
Any solution to this follow up?
In those cases, we need to use something like a bloom filter.
It is essentially a hash table but uses less memory. The consequence of this is that we can have false positives when looking up if a character was already seen before, but not false negatives (saying that a character didn't occur before but it did).
Despite the false positives, there are many reports of very successful uses of bloom filters, check quora.com/What-are-the-best-applications-of-Bloom-filters
I am assuming that getNext() and hasNext() is given. Giving only the getUniqueCharacter() Implementation.
char getUniqueCharacter() {
int INT_MAX = 1000000007
int pos[256][2];
char ch;
int min = INT_MAX, i;
for(i=0;i<256;i++) {
pos[i][0] = 0;
pos[i][1] = -1;
}
i=0;
while(hasNext()) {
ch = getNext();
pos[(int) ch][0]++;
pos[(int) ch][1] = i;
i++;
}
for(i=0;i<256;i++) {
if(pos[i][0] == 1) {
if(min > pos[i][1]) {
ch = (char) i;
min = pos[i][1];
}
}
}
if(min != INT_MAX)
return ch;
else
return '#';
}
In C# .Net
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniqueCharacterFromStream
{
class Program
{
static void Main(string[] args)
{
CharacterStream stream = new CharacterStream("caAaaAAbBcBbd");
Console.WriteLine(string.Format("First Unique Character {0}",GetFirstUniqueCharacter(stream)));
Console.Read();
}
private static char GetFirstUniqueCharacter(CharacterStream stream)
{
Dictionary<char,int> characters = new Dictionary<char,int>();
while(stream.HasNext)
{
var nextChar = stream.GetNext;
if(characters.ContainsKey(nextChar))
characters[nextChar] ++;
else
characters.Add(nextChar,0);
}
return characters.FirstOrDefault((data) => data.Value == 0).Key;
}
}
class CharacterStream
{
char[] Characters;
int _CurrentIndex = -1;
public CharacterStream(string characters)
{
this.Characters = characters.ToCharArray();
}
public char GetNext
{
get { return Characters[++ _CurrentIndex];}
}
public bool HasNext
{
get { return ((_CurrentIndex + 1) < Characters.Count()); }
}
}
}
Not sure if this is a perfect logic or not, however it works fine.
char getUniqueCharacter()
{
string unique = "";
while (hasNext() == true)
{
char next = getNext();
string::size_type itr = unique.find(next);
if (itr == std::string::npos)
{
unique += next;
}
else
{
unique.erase(itr, 1);
}
}
if (unique.size() == 0)
return '#';
return unique[0];
}
It s very ques..I dnt knw y amazon asking easy quesns now a days..(is it a fake?)
Any way i m gng 2 tryyyyyy..the qus "first unique character in a Stream"
Algorithm:
1)get count of each and every char in string array.
2)use "Java - String indexOf() Method" to find which element comes first in string array whose count is 1.
##CODE -SAMPLE:##
String str = new String("aAbBABac ");
for(i=0;i<str[i].length();i++)
{
if(str[i]==str[i+1])
{
count++;//count of all char
}
ex:
aAbBABac
count of 'a'=2,'A'=2,'B'=2,'b'=1,'c'=1;
return the char whose count is 1;
b,c;
##now finding the first unique char :##
System.out.println(Str.indexOf( 'b' ));
System.out.println(Str.indexOf( 'c' ));
should return index of both 'b','c':
index of 'b'=2;
index of 'c'=7;(chk both)
Here 'b' comes first befre 'c' ;so
OUTPUT:
'b'
#include<iostream>
using namespace std;
int main()
{
char str[1000];
int arr[256],i,j=0;
for(i=0;i<256;i++)
arr[i]=0;
cout<<"Enter the stream of characters:";
cin>>str;
char* ptr=str;
while(*ptr)
{
if(arr[*ptr]==0)
arr[*ptr++]=++j;
else if(arr[*ptr] > 0)
arr[*ptr++]=-arr[*ptr];
else
{
ptr++;
j++;
}
}
int min=257;
for(i=0;i<256;i++)
{
if(arr[i] > 0 && arr[i]<min)
min=arr[i];
}
if(min!=257)
cout<<min<<" "<<str[min-1];
else
cout<<"#";
return 0;
}
/* Returns an array of size 256 containg count
of characters in the passed char array */
int *getCharCountArray(char *str)
{
int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}
/* The function returns index of first non-repeating
character in a string. If all characters are repeating
then reurns -1 */
int firstNonRepeating(char *str)
{
int *count = getCharCountArray(str);
int index = -1, i;
for (i = 0; *(str+i); i++)
{
if(count[*(str+i)] == 1)
{
index = i;
break;
}
}
return index;
}
Use a ASCII array of integer of length 256, it represent the of character of its corresponding index. Initially it contains value -1 as absence of character of corresponding index.
- Rahul September 28, 2013Read character from stream and update ASCII array as
If index of corresponding ascii value of character is -1 then update with its position in the stream.
If index of corresponding ascii value of character is +ve(char has already came) then update it with -2 (Means Not unique)
Finally after getting all the value from stream, trace the array and get min +ve value and its corresponding ASCII char will be the unique char
If array contains only -ve value then # as result