## Amazon Interview Question for SDE1s

Team: TCS
Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
6
of 8 vote

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.
Read 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

Comment hidden because of low score. Click to expand.
1
of 5 vote

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;
}``````

Comment hidden because of low score. Click to expand.
0

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?

Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<iostream>
using namespace std;
int main()
{
char str;
int arr,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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

I am assuming that getNext() and hasNext() is given. Giving only the getUniqueCharacter() Implementation.

``````char  getUniqueCharacter() {
int INT_MAX = 1000000007
int pos;
char ch;
int min = INT_MAX, i;
for(i=0;i<256;i++) {
pos[i] = 0;
pos[i] = -1;
}
i=0;
while(hasNext()) {
ch = getNext();
pos[(int) ch]++;
pos[(int) ch] = i;
i++;
}
for(i=0;i<256;i++) {
if(pos[i] == 1) {
if(min > pos[i]) {
ch = (char) i;
min = pos[i];
}
}
}
if(min != INT_MAX)
return ch;
else
return '#';
}``````

Comment hidden because of low score. Click to expand.
0

what if the number of characters is larger than the maximum number of i?

Comment hidden because of low score. Click to expand.
0
of 0 vote

In C# .Net

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UniqueCharacterFromStream
{
class Program
{
static void Main(string[] args)
{

CharacterStream stream = new CharacterStream("caAaaAAbBcBbd");

Console.WriteLine(string.Format("First Unique Character {0}",GetFirstUniqueCharacter(stream)));

}

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

}

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()); }
}
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}``````

Comment hidden because of low score. Click to expand.
-1
of 3 vote

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'

Comment hidden because of low score. Click to expand.
0

data coming as stream, so this algorithm is not good.

Comment hidden because of low score. Click to expand.
-2
of 6 vote

amazon asking baby questions these days? or one weak interviewer maybe

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Mammary error.

Comment hidden because of low score. Click to expand.
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;
}

Comment hidden because of low score. Click to expand.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.