Amazon Interview Question for Software Engineer / Developers






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

#include <iostream>
using namespace std;

int main()
{
char *t = "This is lib";
char *p = "HSC";

char *s, *soln;
soln = NULL;

s = p;
int count = strlen(p);
cout<< " the strlen is " << count;

while(*t != '\0')
{
if(*t == *p)
{
t++;
p++;
if(*p == '\0')
{
soln = t - count;
}
}
else
{
p = s;
t++;
}
}
if(soln == NULL)
{
cout << "pattern not present";
}
else
cout<<"pattern found at " << soln;

return 0;

}

Please comment on my logic n implementation

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

This doesn't work for the example. t= ttiger and p = tiger

- Ranganath April 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Any comments on improving my implementation are welcome!

char* my_strstr(const char* s1, const char* s2)
{
char* p1 = const_cast<char*>(s1);
char* p2 = const_cast<char*>(s2);
int len = 0;

while(*p1)
{
if(*p1 == *p2)
{
p2++;
len++;
}
else
{
if(*p2 == '\0')
{
return p1-len;
}
}

p1++;
}

if(*p1==*p2)
return p1-len;

return NULL;
}

- dullboy April 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main ()
{

  char * str1 = "mmmmmmmmmgood" ;
  char * str2 = "mmmgood" ;


  char * p =  str1 ;
  char * p2 = str2 ;
  char * start = NULL ;

  while ( *p != '\0' && *p2 != '\0')
  {
      if( *p == *p2){
         p2++ ;
         if( start == NULL) start = p ;
         p++ ;
      }
      else if ( start != NULL){
         //reset
         p2 = str2 ;
         start = NULL ;
     }
     else
        p++ ;
  }


  printf ("%s = " , start) ;

}

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

int strcmp(const char *a, const char *b)
{
    assert(a && b); 
    while (*a == *b) {
        if (!*a) {
            return 0;
        }
        a++; b++;
     }
     return (*a < *b) ? 1 : -1;
}

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

char *my_strstr(const char *str1, const char *str2)
{
char *cp = (char *)str1;
char *s1, *s2;

if(!*str2)
return (char *)str1;

while(*cp)
{
s1 = cp;
s2 = (char *)str2;

while(*s1 && *s2 && !(*s1 - *s2))
{
s1++;
s2++;
}
if(!*s2)
return(cp);
cp++;
}
return NULL;
}

- Dheeraj June 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *my_strstr(const char *str1, const char *str2)
{
	char *cp = (char *)str1;
	char *s1, *s2;

	if(!*str2)
		return (char *)str1;

	while(*cp)
	{
		s1 = cp;
		s2 = (char *)str2;

		while(*s1 && *s2 && !(*s1 - *s2))
		{
			s1++;
			s2++;
		}
		if(!*s2)
			return(cp);
		cp++;
	}
	return NULL;	
}

- Dheeraj- repost June 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

General comment regarding some of these code snippets -- what's with the casting of const char * to a char *?? You'll need to understand the difference between char * const (or const char * const) and const char * to pass any interview I give. In none of these cases the input string is, or should be, modified. Leave the const be!

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

Here is the java brute force solution

// Return: Starting position of n within h or -1 if n don't occur within h
// h = main String        eg: needle
// n = search string      eg: eed
int strstr(String h, String n)
{
   if (n.length > h.length)
   {
       return -1;
   }
   
   // O(n^2) soln
   // Lets save some time instead of iterating through the whole h.
   // If h is 8 chars, and n is 5 characters, don't need to check 4th character.
   for (int i = 0; i <= (h.length - n.length) ; i++) 
   {
       int j = 0;
       for (j < n.length; j++)
       {
           if (h.charAt(i + j) != n.charAt(j))
           {
               break;
           }           
       }
       if (j == n.length)
       {
           return i;
       }       
   }
}

- Anonymous January 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *strstr(const char *s1, const char *s2) {
  const char *a = s1, *b = s2;
  for (;;) 
    if (!*b)          return (char *)s1;
    else if (!*a)          return NULL;
    else if (*a++ != *b++) {a = ++s1; b = s2;}
}

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

int strstr(String h, String n)
{
if (n.length > h.length)
{
return -1;
}

// O(n^2) soln
// Lets save some time instead of iterating through the whole h.
// If h is 8 chars, and n is 5 characters, don't need to check 4th character.
for (int i = 0; i <= (h.length - n.length) ; i++)
{
int j = 0;
for (j < n.length; j++)
{
if (h.charAt(i + j) != n.charAt(j))
{
break;
}
}
if (j == n.length)
{
return i;
}
}
}

- Anonymous November 09, 2017 | 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