Amazon Interview Question


Country: India
Interview Type: In-Person




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

Easy. Append one of those strings with itself;
so if I am given to check if vindar and darvin are rotational equivalents, just append vindar to itself. Now i have vindarvindar and ANY rotational equivalent of the string will be a substring of this string!

- Arvind October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool rotation(char *str1 , char *str2)
{
if(str1 == NULL || str2 == NULL)
return 0;
if(strlen(str1) != strlen(str2))
return 0;

char *temp = new char[sizeof(str1) * 2 + 1];

strcat(temp,str1);
strcat(temp,str1);

char *ptr =strstr(temp,str2);

delete temp;

if(ptr == NULL)
return 0;
else
return 1;
}

- Anonymous September 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the strstr () procedure does the string matching.which we can not use.

- arindam.mitra2 September 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then a question comes up: what you can actually do with these strings ? ;) if you cannot compare them char-by-char then what ?
maybe you did not get exactly what the interview wanted

- Anonymous September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done in O(nlgn)

- Anonymous September 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I doubt if it was ever asked by an Amazon Interviewer. The OP probably wants to solve it, and formulate the problem as his wish. Then why do people care to pay heed to such questions.

@arindam.mitra2: Isn't it better if you post such problems on Stackoverflow?

- anonymous2 September 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isSubstring(string s1, string s2) {
int j=0;
for (int i=0; i < s1.size(); ++i) {
if (s1[i] == s2[j])
++j;
else
j=0;

if(j==s2.size()) {return true;}
}
return false;
}

bool isRotationallyEquivalent(string &s1, string &s2) {
return isSubstring(s1+s1, s2);
}

- Anonymous October 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can be done in O(1).

- raararra October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes sir you are a genius!

- Anonymous October 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes u are :)

- Anonymous October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * move1char(char *str)
{
char temp=*str;
static char *strtemp=new char[strlen(str)];
int i=0,len=strlen(str);
while(i<len)
{
strtemp[i]=str[i+1];
i++;
}
strtemp[len-1]=temp;
return(strtemp);
}

bool cyclic(char *str1,char *str2)
{

int m=0,len=strlen(str1);
char *strtemp=new char[strlen(str1)];
if(strlen(str1) != strlen(str2))
return false;
strtemp=str2;
for(int i=0;i<len;i++)
{
if(!strncmp(str1,strtemp,len))
return true;
else
strtemp=move1char(strtemp);
}
return(false);
}

- Kunal Bansal September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool cyclic(char *str1,char *str2)
{

int m=0,len=strlen(str1);
char *strtemp=new char[2*strlen(str1)];
if(strlen(str1) != strlen(str2))
return false;
strcpy(strtemp,str2);
strcat(strtemp,str2);
for(int i=0;i<len;i++)
{
if(!strncmp(str1,strtemp+i,len))
return true;
}
return(false);
}

- Kunal Bansal September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rotationalequi(){
	char *A="Helluo";
	char *B="elloH";
	char *tmp=B;
	char *CC=(char*)malloc(sizeof(char)*strlen(B)*2+1);
	char *C=CC;
	int counter=0;
	while(1){
		while(*tmp){
			*C++=*tmp++;
		}
		counter++;
		if(counter==2){
			*C='\0';
			break;
		}
		tmp=B;
	}
	
	
	C=CC;
	tmp=A;
	cout << " " << C;
	while(*C){
		while((*C==*tmp)&&(*tmp)){
			C++;
			tmp++;
		}
		if(!*tmp){
			cout << " found ";
			break;
		}
		else{
			tmp=A;
		}
		C++;
	}
	
	
	
	free (CC);
};

- Anonymous December 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Testcase:

assert (RotationalCheck("Stupid Interviewer", "Friggin A***ole") == false);

- Anonymous September 27, 2011 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More