Amazon Interview Question for Technical Support Engineers


Country: India
Interview Type: Written Test




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

def prefix(st, text, str_len):
    i = 0
    while st[i] == text[i] and str_len:
        str_len -= 1
        if str_len:
            i += 1

    if not str_len:
        return True
    else:
        return ord(st[i]) - ord(text[i])


def strstr(st, text):
    str_len = len(st)
    for i in range(len(text) - len(st)+1):
        if type(prefix(st, text[i:], str_len)) == bool:
            return i

    return -1

- kirotawa January 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If the program is going to be used only once. What the tip suggests is good enough. Taking O(nm) time, where n is the length of the longer string and m is the length of the shorter one.

If it is going to be used many times for checking different substrings, first build the surffix tree of the long string. Then checking a substring of length m only takes O(m) time.

- chriscow January 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getSubStringStartingPosition(char[] st1, char[] st2)
	{
		int pos=0;
		boolean flag=false;
		
		int k=0;
		for (int i=0; i<=st1.length-1; ++i)
		{
			if ((k <= st2.length-1) && (st1[i]==st2[k]))
			{
				//System.out.println("found.."+st2[k]);
				if (!flag)
				{
					pos=i;
				}
					
				flag=true;
				k++;
			}
			else
			{
				System.out.println("not found.."+st1[i]);
				if (flag && k <=st2.length-1)
				{
					System.out.println("found an extra character in the middle..");
					pos=-1;
				}
			}
		}
		return pos;
	}

- Implementation in java January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{

var indexOf = function(search, target){

for(var k = 0; k < search.length; k++){

if(search[k] = target[0]){

var found = true;

for(var j = 0; j < target.length; j++){

found = found & (search[k + j] == target[j]);

}

if(found){

return k;

}

}

}

return -1;


}

console.log(indexOf('this works','works'));
console.log(indexOf('AAAB','AAB'));

}}

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

public String findThisInString(String string1, String string2)
	{
		int index = string1.indexOf(string2);
		return String.format("Found '%s' at index: %d", string2, index);
	}

- Wally January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String findThisInString(String string1, String string2)
	{
		int index = string1.indexOf(string2);
		return String.format("Found '%s' at index: %d", string2, index);
	}

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

KMP (easy if you know that)

vector<int> BuildPrefixFunction(const string& s) {
  vector<int> result(s.length(), 0);
  for (int i = 1; i < s.length(); ++i) {
    int j = result[i-1];
    while (j && s[i] != s[j]) j = result[j-1];
    if (s[i] == s[j]) ++j;
    result[i] = j;
  }
  return result;
}

Z function (Google that)

But would prefer hash here. Polynomial hashes would work perfectly here.

- Alex February 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

KMP (easy if you know that)

vector<int> BuildPrefixFunction(const string& s) {
  vector<int> result(s.length(), 0);
  for (int i = 1; i < s.length(); ++i) {
    int j = result[i-1];
    while (j && s[i] != s[j]) j = result[j-1];
    if (s[i] == s[j]) ++j;
    result[i] = j;
  }
  return result;
}

Z function (Google that)

But would prefer hash here. Polynomial hashes would work perfectly here.

- Alex February 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#define MAX 80
int find();
int main()
{
int n;
n=find();
if (n!=0)
printf("\n First occurence of first string is at %d position of second string", n);
else
printf("\n Not found!");
return(0);

}

int find()
{
char str2[MAX],str1[MAX],*str3;
int len2,i,n=0;
printf("Enter the first string:\n");
gets(str1);
printf("Enter the second string:\n");
gets(str2);
len2=strlen(str2);

for (i=0;i<len2;i++)
{
if (str2[i]==str1[0])
{
str3=strstr(str2,str1);
printf("\n Found...:\t %s", str3);
n=i;
break;
}
else
n=0;
}
return (n);
}

- in C November 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here i am giving it in shell script with the help of awk

Var1=<String1>
Var2=<String2>

echo $Var2 |awk -v Var=$Var1 '{print index($0,Var)}'

- kiran December 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int indexOf(String a, String b) {
if (a == null || a.isEmpty() || b == null || b.isEmpty())
return -1;
if (a.length() > b.length())
return -1;
int max = b.length() - a.length() + 1;
int bx = 0;
while (bx < max) {
if (a.charAt(0) != b.charAt(bx)) {
bx++;
while (bx< max && a.charAt(0) != b.charAt(bx)) {
bx++;
}
} else {
boolean found = true;
for (int i = 1; i < a.length() && bx + i < b.length(); i++) {
if (a.charAt(i) != b.charAt(bx + i)) {
found = false;
break;
}
}
if (found)
return bx;
bx++;
}
}
return -1;
}

- Mr. P November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int indexOf(String a, String b) {
		if (a == null || a.isEmpty() || b == null || b.isEmpty())
			return -1;
		if (a.length() > b.length())
			return -1;
		int max = b.length() - a.length() + 1;
		int bx = 0;
		while (bx < max) {
			if (a.charAt(0) != b.charAt(bx)) {
				bx++;
				while (bx< max && a.charAt(0) != b.charAt(bx)) {
					bx++;
				}
			} else {
				boolean found = true;
				for (int i = 1; i < a.length() && bx + i < b.length(); i++) {
					if (a.charAt(i) != b.charAt(bx + i)) {
						found = false;
						break;
					}
				}
				if (found)
					return bx;
				bx++;
			}
		}
		return -1;
	}

- Mr. P November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

he easy C++ way to do this is to use std::string::find:

std::string s = "apple";
std::string sub = "ple";
auto index = s.find(sub); //std::string::npos if not found
The way for C would be strstr:

const char *s = "apple";
const char *sub = "ple";
const char *pos = strstr(s, sub); //NULL if not found

- Indian January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Worst case Complexity : O(n)

public class Practice65 {
	public static void main(String args[]){
		String str1 = "abcdefcde";
		String str2 = "de";
		int j=0,i=0;
		while(i<str1.length()){
			if(str2.charAt(j) == str1.charAt(i)){
				j++;
				i++;
				if(j == str2.length()){
					System.out.println(i - str2.length());
					break;
				}
			}else{
				j=0;
				i++;
			}
		}
	}
}

- Coder January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You algorithm doesn't work.

Try to search
AAB in AAAB

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, I forgot to consider that case. The following code will work for upper case as well.

public class Practice65 {
	public static void main(String args[]){
		String str1 = "AAAABBAABBAAABAB";
		String str2 = "AABBAAA";
		int j=0,i=0;
		while(i<str1.length()){
			if(str2.charAt(j) == str1.charAt(i)){
				j++;
				i++;
				if(j == str2.length()){
					System.out.println(i - str2.length());
					break;
				}
			}else{
				i = i - j;  // Add this piece of code.
				j=0;
				i++;
				
			}
		}
	}
}

- Coder January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Once you add that line, the complexity becomes O(mn) with m being the length of the 2nd string. Basically, at i-th position of str1, you check

if (str1.charAt(i+j) == str2.charAt(j))

for j=0 to str2.length()-1. If it passes, you get the answer. But if it fails, you move to the (i+1)-position and start all over again.

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Haha. Can you write the code? You have about 20 min for that (time limit for a such question).

It is easier to code Rabin Karp with the same average complexity. It could be done for such time constraints.

- Anonymous January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.


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