Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

use KMP string Matching.. for solution: ideone.com/t2nPA

- cobra August 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;
#include<string.h>
main()
{
char str[100];
cin>>str;
char c;
fflush(stdin);
c=getchar();
int i,len;
len=strlen(str);
i=0;
while(c!='\n')
{
if(c==str[i])
{
i++;
if(i==len)
{
cout<<"matched";
break;
}
}
else if(c==str[0])
{
i=1;
}
else
{
i=0;
}
c=getchar();
}
if(c=='\n')
cout<<"Did Not Match";
}

- rhljain08 August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code doesn't work if string is ababc and character stream is abababc

You need to have a second counter j for holding the index of the 2nd occurrence of a

- Krishan October 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

rabin-karp algorithm

- algos August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make a finite automation from the string to be searched. Process each character from the stream and update the state of the automation. If at any time we are on the accepting state, declare the result. [ Cormen Chapter 32, Section 32.3]

We can't use Rabin-Karp as the question says we can't store the stream ( which I take it as we are not even allowed to store the string of size equal to the string to find. In Rabin-Karp, once we find the match, we need to verify whether it actually matches ).

- ap August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Knuth–Morris–Pratt algorithm
prepocess the str so tat we dont need to store the stream

- Vincent August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is no repetition of the first element of the string, the algorithm should be pretty easy. Otherwise, we need to keep an array of matching positions, the size of which is the times of repetition of the first element.

- Jeff August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this for SDET?

- cancerian111 August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let len be the number of chars in str. Store the incoming characters of the stream in a queue of len elements. Every time you accept a new char from the stream, append the new character to the one end and dequeue a character from the opposite end. Check if the character sequence stored in the queue is equal to str. If so, then set a global bool variable to true.

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

@Anonymous and @leet one problem is that string does'nt have attribute length. its a method in String str.length(); not str.lengtth;

- Puja August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code is in C

Function returns 0 on failure and 1 on success

int match_str(char str[])
{
char ch,*temp=str;
int len,count=0;
for(len=0;str[len];len++);
while(scanf("%c",&ch))
{
if(ch=='\n')
break;
if(*temp!=ch && count)
{
temp=str;count=0;
}
if(*temp==ch)
{
temp++;count++;
}
if(count==len-1)
return 1;
}
return 0;
}

- sc August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
#include<string.h>
main()
{
char str[100];
cin>>str;
char c;
fflush(stdin);
c=getchar();
int i,len;
len=strlen(str);
i=0;
while(c!='\n')
{
if(c==str[i])
{
i++;
if(i==len)
{
cout<<"matched";
break;
}
}
else if(c==str[0])
{
i=1;
}
else
{
i=0;
}
c=getchar();
}
if(c=='\n')
cout<<"Did Not Match";
}

- Anonymous August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
int j = 0;
int result = -1;
for(int i=0; string[i] && find[j]; ++i)
{
if(string[i] != find[j]){ j=0;}
if(string[i] == find[j]){
if(j == 0){ result = i; }
j+= 1;
}
}

return find[j] == 0 ? result : -1;
}}

- Anonymous October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

you do 1 thing.first find the size of string str.then search that lenght @ 1 time .means read 1 character match that with the character in str.if same move forward else discard that character and move next.

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

-1. You cannot find the length of a stream.

- deadman August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dead bakchod..m not taking about the stream.its about the small string.

- code4life August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

boolean MatchString(Stream stream, String str)
   {
      boolean done= false;
      int i = 0;
      while(!done)
       {
            char c = stream.readNextChar();
            if(str.charAt(i) == c )
            {
                if(i == (str.length-1))
                 {
                     return true;
                 }
                 i++;                
            } 
            else
           {
               i = 0;
            }
        }
   }

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

stream aaaab pattern aab ,your algo will return false?

- grave August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous: a small mistake in your algo..

boolean MatchString(Stream stream, String str)
   {
      boolean done= false;
      int i = 0;
      while(!done)
       {
            char c = stream.readNextChar();
            if(str.charAt(i) == c )
            {
                if(i == (str.length-1))
                 {
                     return true;
                 }
                 i++;                
            } 
            else if(c == str.charAt(0))
                        i=1;
            else
           {
               i = 0;
            }
        }
   }

@leet and @anonymous: when the loop ends???
can i write

if( c == '\0')
    return false;

if i am wrong what is the condition for the loop to terminate if the stream is stopped..

- cobra August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cobra and leet: I guess its perfect now and only possible improvement could be spawning a new thread that invokes this function MatchString after reading each character in the stream. The complexity could go very high as a result of spawning of the multiple threads.

- Wolf August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@wolf: what about the case:
Stream: abcabcade
str: abcade

- Jeff August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess we cant do any thing about it. Only possible way to avoid it is use threads for each input. from the stream

- Wolf August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if stream is "abaaab" and we are looking for "aab", the above matchString() does not seem to work. Please correct me if i am wrong. I tried in eclipse, and it failed...

- VRAM August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@VRAM: ya man.. it wont work.. u have to use KMP string matching algorithm.. for the solution.. see my comment at the top

- cobra August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@VRAM you might have overlooked else if(c==charAt(0)) step..
this code works for your string aab also..pls check

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

@Anonymous - I have seen that line of code. Even then, it will not work - it expects i to be 2 instead of 1 (because of repeated aaa..). Please check and update if it worked in your test env.

- VRAM August 09, 2012 | Flag


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