Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
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.
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
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;
}
}
#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);
}
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;
}
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.
int hasrepeatedSeq(String s)
- dell March 23, 2012{
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;
}