Amazon Interview Question for Software Engineer / Developers


Team: Amazon Prime
Country: United States
Interview Type: Phone Interview




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

considering we allow pattern string like a*b, a.*b., etc,
character '.' means matching any character once, a star ('*') means matching previous character 0 ~ infinite times. (a* means matching '','a','aa','aaa',...; and .* means matching any string, including empty string)

we can solve this problem using dp:
1) parse pattern: pattern string 'ab*c' parsed to {{'a',false},{'b',true},{'c',false}}, the second bool value in each pair indicates whether the corresponding character is followed by a star ('*')
1) consider matching string of length m with parsed pattern of length n, we use f(i, j) to denote the matching result of string.substr(i) and pattern subsequence starting at j. f(i,j)=1 means a match while f(i,j)=0 means mismatch
2) we have the base case f(m,n) = true, since empty string matches empty pattern sequence.
3) for all 0 =< i < m, we have f(i,n) = false, since non-empty string will always mismatches empty pattern
4) for all 0=< j < n, we have f(m, j) = true if and only if pattern[j] to pattern[n-1] are all with stars.
5) for other cases, we can match from the end to head, we have: f(i, j) =
a) pattern[j] is with star:
a1) if pattern[j]=='.' || pattern[j]==str[i]: f(i,j) = f(i,j+1) || f(i+1,j+1) //we ignore current pattern character or use it to match current character in string
a2) otherwise, f(i,j) = f(i,j+1) //we can only ignore current pattern character to get a match
b) pattern[j] is without star:
b1) if pattern[j]=='.' || pattern[j]==str[i]: f(i,j) = f(i+1,j+1)
b2) otherwise f(i,j) = false
6) for all 0=< i < m, if f(i, 0) = true, it means substring starting from position i and ending at the tail of the string matches the whole pattern
7) previous steps will find occurances of matching substrings ending with the end of given string

8)since we want to find all occurances (ending at all possible positions), we simply match string.substr(0, len) with the whole pattern, for all 0 < len <= string.length().

9) time complexity O((m^2) *n), space complexity O(m*n)

- llq February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Overall looks pretty good, thanks for this solution! Just a couple of questions:

In a1, shouldn't you also consider all i's that have pattern[j] == str[i]? Such as

k = i + 1
f(i, j) = f(i, j + 1)
while k < m and (pattern[j] == '.' or pattern[j] == str[i]):
f(i, j) = f(i, j) or f(k, j + 1)
k = k + 1

Which makes time complexity O(m^2 * n) without finding all occurrences.

and in 8, I'm not sure why 0 < len <= str.length() gives you all possible positions, I believe this value is within the order of O(m^2), i.e., all 0 <= i <= j < m for str[i:j], so for getting this particular answer it would require you O(m^4 * n) time, right?

Finally, I may be wrong on this, but I believe that for this problem you can do fine with just recursion and backtracking (no DP), since no sub-solution is visited more than once.

- meh February 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Answer:
abbbacctlkjlkcccaaab
abbbacctlkjlkcccaaabb
abbbacctlkjlkcccaaabbb

I think it is not possible to implement it in a 45 minutes interview.
Maybe you only need to provide an idea as Murali Mohan answered.

- Anonymous February 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

I think it is possible to implement it in a 45 minute interview.
Maybe you need to improve your skills.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Answer:
abbbacctlkjlkcccaaab
abbbacctlkjlkcccaaabb
abbbacctlkjlkcccaaabbb

I think it is not possible to implement it in a 45 minutes interview.
Maybe you only need to provide an idea as Murali Mohan answered.

- Anonymous February 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

I think it is possible to implement it in a 45 minute interview.
Maybe you need to improve your skills.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I did spend some time about putting idea of DFA/NDFA to use but couldn't materialize in reasonable time.

Key idea that I used is -
Init =>
=====
Treat different parts of pattern as small(sub)Strings

For one match =>
===========
Fix (i)th subString into InputString and then go on to fix for all remaining subString(s) into remaining of InputString

Terminating conditions =>
===================
if we'r matching last subString => print the Match (we know position of all subStrings)

To find all matches =>
Find-next-Match of (i)th string and call match for remaining of InputString

Below is the pseudo-code I used :

Init //; this will put all patterns in an array/vector gsubString - in our example of size 3 gSubStrigns[]= {ab, c, b}

FindMatches(int subStringNo, string stringToMatch)
 {
	currPos = 0;
	currSubString = gSubStrings[subStringNo]
	while(currPos = FindNextMatch(currSubString, stringToMatch, currPos)	
	{
		if(subStringNo == gSubStrings.size - 1) 
		{
			PrintMatchedPattern;
		}else 
		{
			FindMatches(subString + 1, stringToMatch + currPos + currSubString.len ) // Match rest of the patterns in remaining string
		}
		curPos++		
	}
}

One performance enhancement that is left out is, when we know rest of sub-strings won't be able to match, due to length considerations, we don't call Find_Next_Match. Please note, when we say length - its not just sum of rest of substrings, we have to take pattern structure in consideration.

- Arun February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

To find patterns using regular expressions, one needs to be build an NFA and it's equivalent DFA.

- Murali Mohan February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your answer completely misses the point. They look at how you communicate your answer. It's not building NFA which is equivalent DFA which makes no sense at all. Use CS acroynms like a SDE and not BS or SOB.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I feel obliged to give you another advice: they also look at people who can control their emotion and stay on the topic when arguing for your position. That's why you are not a professional developer. Every single word you said is very own opinion and nothing based on truth. Every single statement Anonymous said is based on facts.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Let us start with this one.

Murali said, "NFA and its equivalent DFA".

You/Anon wrote, "NFA which is equivalent DFA makes no sense at all".

Talk about ignorance.

Either you are completely clueless, or a drooling idiot.

I hope it is the earlier, but you are showing signs of the latter.

- Irritable Bowel Syndrome. February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You are so right. Someone who mis-typed a sentence is completely clueless. "I hope it is the earlier" does not make sense at all. It should be "I hope it is the FORMER". If you don't know how to say something correctly, dont' say it. Your comment, again, misses the point. You don't help the users of this website, at all.

- Irked by Ignorance February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your NFA/DFA approach completely misses the point of the problem. It is not necessary to prove NFA/DFA cannot be used to solve this problem to see that. Why? Do you understand what dynamic programming is? To your point, a convoluted NFA/DFA apporach is always less desirable than an elegant DP solution.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your response shows that you don't know how to interview people or interviewed by people. Why would I have the time to understand all your possible set of solutions? In the real world, your boss is only interested in your best solution. So clearly you don't understand this. Also, your comment again doesn't help anyone who is using this website.

- Anonymous February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote

your answer is missing the point, they want to see how you devise an algorithm, not just using regex

- Anonymous February 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@enthusiast: lol

- ssikka25 February 06, 2014 | 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