Amazon Interview Question for Software Engineer / Developers






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

concatenate the original string
back to back
and then find the rotated string as a substring
if it exists you are done.

- Anonymous July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

too good...

- Anonymous August 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int circular(char* str1, char* str2)
{
char* tmp1=NULL;
char*tmp2 =NULL;
int size1=0;
int size2 = 0;

tmp1=str1;
while(*tmp1!='\0')
{
tmp1++;
size1++;
}

tmp1=str2;
while(*tmp1!='\0')
{
tmp1++;
size2++;
}

if(size1!=size2)
return 0;

tmp2=str2;
tmp1=str1;
while(*tmp2 != *str1 && *tmp2!='\0')
tmp2++;
if(*tmp2=='\0')
return 0;

while(size1)
{
if(*tmp2=='\0')
tmp2=str2;
else if(*tmp2==*tmp1 )
{
tmp2++;
tmp1++;
size1--;

}
else
return 0;
}

return 1;

}

- rishirishi November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c" line="1" title="CodeMonkey93165" class="run-this">#include<stdio.h>
#include<string.h>
int main()
{
char a[50],b[50];
int res;
gets(a);
gets(b);
res=isperm(a,b);
if(res==1)
printf("\nYeah it is");
else
printf("\nNo it is nt ");
return 0;
}
int isperm(char a[], char b[])
{
char c[50];
int l1,l2,i,j,k,s;
l1=strlen(a);
l2=strlen(b);
if(l1!=l2)
return(9);
for(i=0;i<l1;i++)
{
j=i+1;
k=0;
while(k<l1)
{
c[k]=a[j];
k++;
j=(j+1)%l1;
}
s=strcmp(c,b);
if(s==0)
return 1;
}
return(9);
}
</pre><pre title="CodeMonkey93165" input="yes">abcd
bcda</pre>

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

better use KMP algorithm here for finding substring

- Anonymous July 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use a circular queue , dequeue a element and enqueue the same element in to the queue , if we do the same for n times we can generate all the circular permutations of the given string.

- Krishna July 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can't we just check if the first letter of the first string appears in the second string .. if it does then compare the succeeding letters in both strings using modulo arithmetic for the second string.. i guess it should be O(n) ... please correct me if i'm wrong.

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

what if the first letter appears more than once in the second string. then i guess it wont be 0(n)

- Anonymous July 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string.h>
using namespace std;
bool iscircular(const char* input1, const char* input2)
{
int len1 = strlen(input1);
int len2 = strlen(input2);
char* temp = new char[len1+len2+1];
strcpy(temp,input1);
strcat(temp,input2);
char* p = strstr(temp,input2);
cout<<*p<<endl;
if(p!=NULL)
return 1;
else
return 0;
}
int main()
{
char input1[] = "abcd";
char input2[] = "cdab";
bool result = iscircular(input1, input2);
return 1;
}

- Anonymous August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

just a small error:
should be:
strcpy(temp,input2);
strcat(temp,input2);

- Anonymous August 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome

- Anonymous February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I wrote a version without using any strlen or strcmp. I think I am covering all the cases, but please let me know if there is any input that breaks it

bool isCircularPermutation(char str1[], char str2[])
{
int xstr1 =0;
int xstr2= 0;
int idxfound = -1;
while (str2[xstr2])
{
if (idxfound == -1 && str1[0] == str2[xstr2])
{
idxfound = xstr2;
}
if (idxfound != -1)
{
if (str1[xstr1++] != str2[xstr2])
{
idxfound = -1;
xstr1 = 0;
}
}
xstr2++;
}
if (idxfound != 1)
{
for (xstr2 = 0; xstr2<idxfound; xstr2++)
{
if (str1[xstr1] == '\0' || str1[xstr1] != str2[xstr2])
{
return false;
}
xstr1++;
}
}
else
{
return false;
}
if (str1[xstr1] != '\0')
{
return false;
}

return true;
}

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

1. Create a suffix tree and a trie from the first word.
2. See whether both have same length.
3. Match the second word with the suffix tree as much as we can.
4. If the remaining second word is in prefix tree then it is circular
Eg:
cdab
cd will match one of the suffixes. The remaining is ab which will be in prefix tree. Hence it is cyclic.
So this we can do in O(n)

- DS God September 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s1 = "abcdaad";
    string s2 = "cdabaaad";
    
    string conc = s1 + s1;
    cout << conc << endl;
    if ( s1.length() == s2.length() && conc.find(s2) !=  string::npos )
       cout << s1 << " and " << s2 << " are circular anagrams" << endl;
    else
        cout << " not circular anagrams";
    
 cin.get();
 return 0;
    
}

- insaneish September 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
char str1[20], str2[20];
int i, x, y, comp;

printf("Enter The String str1: ");
gets(str1);

printf("Enter The String str2: ");
gets(str2);

x = strlen(str1);
y = strlen(str2);

if(x != y)
{
printf("strings are different!!\n");
}
else
{
for(i=1; i<x; i++)
{
comp = strcmp(str1, str2, x);

if(comp == 0)
{
printf("strings are same!!\n");
exit(0);
}
else
{
left_shift_string(str2);
}
}

printf("strings are different!!\n");
}
}

left_shift_string(char *ptr)
{
char start, *temp;

start = *ptr;
temp = ptr;

while(*ptr != '\0')
{
*ptr = *(ptr+1);
ptr++;
}
ptr--;
*ptr = start;

printf("shifted string = %s\n", temp);
}

- G Naveen Kumar May 11, 2014 | 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