Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

standard solution :
use dynamic programming with

M[i][j] = {1+M[i-1][j-1]                 if A[i] ==B[j]}
             {max(M[i][j-1],M[i-1][j])  otherwise}

- sonesh February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Can you please write the entire code

- suvrokroy February 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is the algorithm

Let string 1 is A[nx1] and string 2 is B[mx1]
Now take M[n+1,m+1];
for i = 0 to n
   M[i][0] = 0;
for j = 0 to m
   M[0][j] = 0;
for i = 1 to n
    for j  = 1 to m
         if A[i-1] = B[j-1]
             M[i][j] = 1 + M[i-1][j-1];
         else
             M[i][j] = Max(M[i-1][j],M[i][j-1])
output M[n][m];

- sonesh March 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

You can solve this problem in two ways. Use a recursive approach or a memoization technique in dynamic programming.

First, technique discusses the recursive approach, here's the code.

int lcs(char *x, char *y, int m, int n)
{
if(m==0 || n==0)
return 0;
if(x[m-1] == y[n-1])
return 1 + lcs(x,y,m-1,n-1);
else
return max(lcs(x,y,m,n-1), lcs(x,y,m-1,n));
}

Second approach discusses the memoization method of DP. Here's the code

int lcs(char *x, char *y, int m, int n)
{
int lcs[m+1][n+1];

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i==0 || j==0)
lcs[i][j] = 0;
else if(x[i-1] == y[j-1])
lcs[i][j] = lcs[i-1][j-1] + 1;
else
lcs[i][j] = max(lcs[i][j-1], lcs[i-1][j]);
}
}
return lcs[m][n];
}

- nikinjain February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there's a minor flaw in your first approach.
it should be

if(x[m] == y[n])

- fuckGFW March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Suffix tree for String A. For each letter in String B, see how deep it can walk in that tree.

- shoudaw February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

standard solution :
use dynamic programming with

M[i][j] = {1+M[i-1][j-1]                 if A[i] ==B[j]}
             {max(M[i][j-1],M[i-1][j])  otherwise}

- sonesh February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sonesh, are you assuming that subsequences can be non-contiguous? If the two strings are "X****Y***Z" and "X______Y__Z", I think your method will return 3, which is valid under the non-contiguous interpretation.

- showell30@yahoo.com February 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, that's why they ask sub sequence, If they want the contiguous then they must have asked substring.

- sonesh March 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Dynamic programming algorithm can give only the length of LCS, if the problem is to get the substring itself - Hirschberg recursive algorithms is to be used (quadratic time linear space), or naive recursive method with string buffer (exponential time complexity)

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

onestopinterviewprep.blogspot.com/2014/03/longest-common-subsequence.html

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

can you explain the Question in details

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

Who can write the suffix tree algorithm over the phone?

- Lyubomir February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My interviewer asked me to edit a google docs file he opened so he can see me code!

- bunnybare February 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

M[i][j]={1+M[i-1][j-1] if A[i]==B[j]}
{max(M[i][j-1],M[i-1][j],M[i-1][j-1]) otherwise}

- abhishek kumar February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this like having 2 sets of chars and looking for the intersection of these two sets?

After finding the intersection do the dynamic programing as in "finding the longest subsequence"

- Kamy February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dynamic programming approach
time complexity:O(len(a)*len(b))

int LCS(string& a,string& b)
{
	int m = a.length();
	int n = b.length():
	vector<vector<int>>dp (m+1,vector<int>(n+1,0));
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(a[i-1] == b[j-1])
				dp[i][j] = 1 + dp[i-1][j-1];
			else 
				dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
		}
	}
	return dp[m][n];
}

- duckling February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class subsequence {

public void method(String a, String b)
{
String s1 = a;
String s2 = b;

int arr1[] = new int[255];
for(int i =0; i< s1.length(); i++){
arr1[s1.charAt(i)] = arr1[s1.charAt(i)] + 1;
}

for(int i=0; i<s2.length(); i++){
if(arr1[s2.charAt(i)]!=0){
arr1[s2.charAt(i)]--;
System.out.println(s2.charAt(i));
}
}
}

public static void main(String[] args) {
int[] arr = new int[256];
subsequence s = new subsequence();
//s.method({'k', 'r','i'}, )
s.method("abc", "abcd");

}

}

- krish February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int lcs(char *x, char *y, int m, int n)
{
if(m==0 || n==0)
return 0;
if(x[m-1] == y[n-1])
return 1+lcs(x,y, m-1, n-1);
else
return max(lcs(x,y,m,n-1), lcs(x,y,m-1,n));
}

- Nikin Kumar Jain February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.careercup;

public class StringSubSequence {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringSubSequence stringSubSequence = new StringSubSequence();
		System.out.println(stringSubSequence.subsequence("abcdefgh", "cdefg"));
		//System.out.println(stringSubSequence.subsequence("abcdfgh", "asdfdfabcdf"));
	}

	
	public String subsequence(String string1, String string2){
		StringBuffer subsequence = new StringBuffer("");
		

		String shortString= null;
		String longString = null;
		if(string1.length() > string2.length()){
			shortString = string2;
			longString = string1;
		}
		else{
			shortString = string1;
			longString = string2;			
		}
		
		int shortStringLength = shortString.length();
		int longStringLength = longString.length();
		
		int shortStringCounter =0;
		int longStringCounter =0;		
		
		StringBuffer tempsb = new StringBuffer();
		while(shortStringCounter < shortStringLength){
			
			longStringCounter = 0;
			while(longStringCounter < longStringLength){
				if(shortString.charAt(shortStringCounter) == longString.charAt(longStringCounter)){
					
					tempsb = new StringBuffer();
					int compare1Counter = shortStringCounter;
					int compare2Counter = longStringCounter;
					do{
						tempsb.append(shortString.charAt(compare1Counter));
						compare1Counter++;
						compare2Counter++;
						
					}while(compare1Counter < shortString.length() && compare2Counter < longString.length() && (shortString.charAt(compare1Counter) == longString.charAt(compare2Counter)));
					
					if(tempsb.length() > subsequence.length()){
						subsequence = tempsb;
					}
					
				}
				longStringCounter++;
			}
			shortStringCounter++;
		}
		
		return subsequence.toString();
	}
}

- Dinesh February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findLongestSubsequence() {
		
		
		String str="",max="";
		String str1="lkjhgfds",str2="oiuyhgftr";
		char [] arr1 = str1.toCharArray();
		char [] arr2 = str2.toCharArray();
		


		
		for (int i = 0; i < arr1.length; i++) {
			
			for (int j = 0; j < arr2.length; j++) {
				
				if(arr1[i]==arr2[j]){
					
					str="";
					int k=0;
					while((i+k)<arr1.length && (j+k)<arr2.length){
						
						if(arr1[i+k]!=arr2[j+k])
							break;
						str=str+arr1[i+k];
						k++;
					}
					if(max.length()<str.length())
						max=str;
					
				}
				
			}
			
		}
		
		System.out.println("===="+max);
		
		
	}

- suvrokroy February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;


public class StringSubsequence {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] = new int[256];
		String s1 = new String();
		String s2 = new String();
		Scanner scan = new Scanner(System.in);
		s1=scan.next();
		s2=scan.next();
		int s1size = s1.length();
		int i;
		for(i=0;i<s1size ;i++){
			a[(int)s1.charAt(i)]++;
		}
		int s2size = s2.length();
		for(i=0;i<s2size;i++){
			if(a[(int)s2.charAt(i)] != 0){
				a[(int)s2.charAt(i)]--;
				System.out.println(s2.charAt(i));
			}
		}
		
	}

}

Time Complexity : O(m) + O(n)

- Anonymous February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice work man but its not working..
it is calculating the sub string but it must be continues according to your logic.

- ADi March 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what about my solution? - " suvrokroy "

- suvrokroy March 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is the common string for "amnefka" and "aefg"?

- glk March 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"ef" I think

- suvrokroy March 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution

package com;

public class LongestSubSequence {

	private static int startIndex = 0;
	private static int length = 0;
	static boolean foundStartIndex = false;

	public static void main(String args[]) {

		char[] firstStr = "AABBCDEF".toCharArray();
		char[] secondStr = "AADEF".toCharArray();

		for (int i = 0; i < firstStr.length; i++) {
			int intrimindex = 0;
			int intrimlength1 = 0;
			 foundStartIndex = false;
			int counter = i;
			for (int j = 0; j < secondStr.length; j++) {

				if (firstStr[counter] == secondStr[j]) {
					if (!foundStartIndex) {
						intrimindex = setStartIndex(i);
					}
					intrimlength1++;
					if (counter + 1 <= firstStr.length-1)
						counter++;
					else {
						break;
					}
					
				} else {
					if (foundStartIndex) {
						foundStartIndex = false;
						if (intrimlength1 > length) {
							swapValues(intrimlength1, intrimindex);
						}
					}
				}
			}
			if (intrimlength1 > length)
				swapValues(intrimlength1, intrimindex);

		}
		System.out.println("AABBCDEF".substring(startIndex, startIndex + length
				));

	}

	private static int setStartIndex(int i) {
		int intrimindex;
		
		intrimindex = i;
		foundStartIndex = true;
		return intrimindex;
	}

	private static void swapValues(int length1, int index) {
		length = length1;
		startIndex = index;
	}
}

- Anand Soni March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can someone tell me why my solution is stupid? i am using contains(), is that not allowed or does that make it much too slow?

public static String returnSubsequence(String one, String two)
{
HashMap<String,Integer> map = new HashMap<String,Integer>();
String sub = "";
int maxLength =0;
String maxSub = "";
for(int i = 0; i<one.length();i++)
{
sub += Character.toString(one.charAt(i));
// System.out.println(sub);
if(two.contains(sub) && sub.length() >= maxLength)
{
maxSub = sub;
maxLength = sub.length();
}
else
{
sub = Character.toString(sub.charAt(sub.length()-1));
}
}
//for()
return maxSub;
}

- kev March 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kev: subsequence and substring are two different things.

- Anonymous March 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can someone tell me why my solution is stupid? i am using contains(), is that not allowed or does that make it much too slow?

public static String returnSubsequence(String one, String two)
{
HashMap<String,Integer> map = new HashMap<String,Integer>();
String sub = "";
int maxLength =0;
String maxSub = "";
for(int i = 0; i<one.length();i++)
{
sub += Character.toString(one.charAt(i));
// System.out.println(sub);
if(two.contains(sub) && sub.length() >= maxLength)
{
maxSub = sub;
maxLength = sub.length();
}
else
{
sub = Character.toString(sub.charAt(sub.length()-1));
}
}
//for()
return maxSub;
}

- kev March 08, 2013 | 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