Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

int hasrepeatedSeq(String s)
{
int n = s.length();
for(int x=0;x<n-1;x++)
{
int y = x+1;
while(y+y-x < n)
{
if(s.substring(x,y).equals(s.substring(y,y+y-x)))
return x;
else
y++;
}
}
return -1;
}

- dell March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the condition of the while loop should be y+y-x <= n, otherwise, it will never reach the last element of the string.

- Luigi April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was the question to write a function to return longest substring( and not sequence ) which is repeated in a string?
If the question is just what stated above, wont it be write a function to return that the string have all distinct characters.
Example: apple, this string also contains a sequence of length 1 that is 'p' and is repeated.

- bytecode March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let me reiterate the question :
It was to find out if a string contains an adjacent repeated sequence of characters.
For Ex1 : 123123 : this contains a repeated adjacent sequence which is 123
Ex2 : 123xy123 : this does not contain a repeated adjacent sequence so answer would be false.
For the example mentioned by you , yes it would have a repeated sequence which is p

- popoff March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can be solved using two pointers.
Here is the solution:


int count=0;
*ptr1=*ptr2=str; //pointing 1st character
while(ptr2 != '\0')
{
count++;
ptr2++; //increase 2nd pointer
if(ptr1 == ptr2) //probability of repetition
{
while(count--)
{
if(ptr1 != ptr2)
break;
else
ptr1++;
}
if(count == 0)
return TRUE;
}

}

- shashank vishnoi March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

doesn't work if i/p is 123123, 0123123

- swathi March 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node
{
int data;
struct node *next;
}node;

void fun(char *str)
{
node *a[256] = {NULL},*temp = NULL,*parr = NULL,*temp1 = NULL;
int i=0,j,k;
while(i<strlen(str))
{
temp = malloc(sizeof(node));
temp->data =i;
temp ->next =NULL;
if(a[(str[i])] == NULL)
a[(str[i])] = temp;
else
{
temp1 = a[(str[i])];
while(temp1!=NULL)
{
parr = temp;
j = temp1->data;

k = i;
while(j<i)
{
if(str[j] == str[k])
{
j++;
k++;
}
else
break;
}
if(j==i)
printf("string index %d to index %d is reapeted \n",temp1->data,i-1);
temp1 = temp1->next;
}
parr->next = temp;
}

i++;
}
}
int main()
{
char *str = malloc(100);
printf("Enter the string : ");
scanf("%s",str);
fun(str);
}

- raunak.gupta29 March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its the automata creation part of KMP algorithm

- blah blah March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please tell how it can be transformed into that?

- nitin.transformer June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question is vague.

- Anonyomous March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool bHasAdjacentRepeated( string s)
{
for ( int i = 2 ; i <= s.length()/2 ; i++)
for ( int p = 0; p <= s.length()-i*2; p++)
{
string s1 = s.substr(p,i);
string s2 = s.substr(p+i,i);

if ( s.substr(p,i) == s.substr(p+i,i))
return true;
}
return false;
}

- moiskim March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isRepeated(String s) {
        int len = s.length();
        int n = 2;
        while(n<len/2+1){
            if(len%n!=0){ n++; continue; }
            int count = len/n;
            for (int i=0; i<count; i++){
                char  ch = s.charAt(i);
                int t = i+count;
                boolean broken = false;
                while (t<len){
                    if(s.charAt(t)!=ch){
                        broken = true;
                        break;
                    }
                    t += count;
                }
                if(broken)
                    break;

                if(!broken && i+1==count)
                    return true;
            }
            n++;
        }
        return false;
    }

- elbek June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuning this the q
It was to find out if a string contains an adjacent repeated sequence of characters.
For Ex1 : 123123 : this contains a repeated adjacent sequence which is 123
Ex2 : 123xy123 : this does not contain a repeated adjacent sequence so answer would be false.

My solution
1. Read each item.
2. If it is not found in the linked list add to a linked list as next node
3. If found check the next item in linked list with the next item in the input.
4. Repeat till you reach the end of the current linked list and if there is a match for each nodevalue to the next input items return true
5. If there is a mismatch in step 4 replace the mismatched item from the input in the list at the mismatched position and repeat from step 1 till end.

- Anonymous July 08, 2012 | 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