Qualcomm Interview Question
Software Engineer / DevelopersWhy the used array should have the size 256? I think having the size of 128 is enough as the ascii values are from 0-127.
hi,
it seems like you are adding a character to the answer string immediately after you find that it hasn't been found before. But what if a character is repeated sometime later and now it has already been added to the answer string. Aren't you supposed to remove it from the answer string. So i think this solution is wrong.
Please see this,
char str[50]="I am an Indian, which country you belong to?\0";
int i;
for(i=0; '\0' !=str[i]; i++)
{
int j;
for(j=i+1; '\0' !=str[j]; j++)
{
if(str[i] == str[j])
{
int k;
for(k=j;str[k]!='\0';k++)
{
str[k]=str[k+1];
}
}
}
}
The output is "I amndi,whcoutrybelg?", I think it is expected... :)
Please excuse if there is anything wrong in the sentence
How about this soln.
Read each character one by one and put it in hashtable.Here key will be alphabet itself and value will be its repetition.whenever you get the count more than 1 in hashtable delete the aplhablet from string.
Reading each character and putting in hashtable.O(n) + const time to put in hashtable.
void RemoveDups(char *str)
{
struct hastable *newhash=createHashtable();
int current=0,new=0;
char *str1=malloc(sizeof(str));
while(str[current]!=NULL)
{
If (!Hashpresent(newhash,str[current]))
{
Hashinsert(newhash,str[current]);
str1[new]=str[current];
new=new+1;
}
current=current+1;
}
str1[current]='\0';
}
This will solve purpose in fast and simple code.
It gives result in O(n)
void removeduplicate(char *str)
{
int checker = 0;
int cnt=0;
for(int i=0;i<strlen(str);i++)
{
int val =*(str+i)- (int)'a';
if ((checker & (1 << val)) > 0)
continue;
else{
*(str+cnt)=*(str+i);
cnt++;
}
checker |= (1 << val);
}
*(str+cnt)='\0';
}
there are many ways to do it
if memory is a don't care
then we can simply use indication array :)
- Anonymous October 29, 2009