Amazon Interview Question for Quality Assurance Engineers


Country: United States
Interview Type: Phone Interview




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

Seems to be above is o(n^2). But we can write the code in simpler manner. 
Iterate each and every characters from the first string and store it in hash table. After that take each character from second string and search it is available in has table. If it is available it is common character.

- Anil Kumar Kallakuri May 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Javascript solution

var s1 = "string";
var s2 ="strong";

// o(m+n)
function common() {
  var dict = {};
  
  for(var i=0; i < s1.length; i++) {
    dict[s1.charAt(i)] = 1;
  }
 
  var commonChars =[]; // this is optional if space is not an issue
  for(var i=0; i < s2.length; i++) {
    if( dict[s2.charAt(i)] == 1) {
      commonChars.push(s2.charAt(i)); // this is optional we can simply print
    }
  }
  
  dict = commonChars.join(""); //o(n)
  console.log(dict);
}

common();

- rocks May 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class CommonStringCC {
	
	
	public static void CommonAlp(String s, String s1){
		char[] a = s.toCharArray();
		char[] b = s1.toCharArray();
		
		for(int i=0;i<a.length;i++){
			for(int j=0;j<b.length;j++){
				if(a[i] == b[j]){
					System.out.print(a[i]);
				}
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Strings : ");
		String c1 = in.next();
		in.nextLine();
		String c2 = in.nextLine();
		
		CommonAlp(c1,c2);
	}

}

- Sameer May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void alphabetCount(String str1, String str2)
	{
		String str3=str1.concat(str2);
		char[] c=str3.toCharArray();
		LinkedHashSet<Character> l= new LinkedHashSet<Character>();
		for(char c1:c)
		{
			l.add(c1);
		}
		
		Iterator<Character> l1 = l.iterator();
		while(l1.hasNext())
		{
			System.out.print(l1.next());
		}
		 
	}
}

- Rahul Singh May 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1- read first string1 and and put its characters in HashSet
2-read characters of string2 , this hashset contains any of these characters print them.

O(n) n: max chars in any string 1 or 2

- Dan May 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

read string

- Dan May 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
		Character c = null; String str3 ="";
		LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
		for(int i = 0; i < s1.length(); i++) {
			hs.add(s1.charAt(i));
		}
		for(int i = 0; i < s2.length(); i ++) {
			c = s2.charAt(i);
			if(hs.contains(c))
				str3 = str3 + c;
			}
		return str3;

}

- google May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
Character c = null; String str3 ="";
LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
for(int i = 0; i < s1.length(); i++) {
hs.add(s1.charAt(i));
}
for(int i = 0; i < s2.length(); i ++) {
c = s2.charAt(i);
if(hs.contains(c))
str3 = str3 + c;
}
return str3;
}

- google May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
		Character c = null; String str3 ="";
		LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
		for(int i = 0; i < s1.length(); i++) {
			hs.add(s1.charAt(i));
		}
		for(int i = 0; i < s2.length(); i ++) {
			c = s2.charAt(i);
			if(hs.contains(c))
				str3 = str3 + c;
			}
		return str3;
	}

- google May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
		Character c = null; String str3 ="";
		LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
		for(int i = 0; i < s1.length(); i++) {
			hs.add(s1.charAt(i));
		}
		for(int i = 0; i < s2.length(); i ++) {
			c = s2.charAt(i);
			if(hs.contains(c))
				str3 = str3 + c;
			}
		return str3;
	}

- This_is_it May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
		Character c = null; String str3 ="";
		LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
		for(int i = 0; i < s1.length(); i++) {
			hs.add(s1.charAt(i));
		}
		for(int i = 0; i < s2.length(); i ++) {
			c = s2.charAt(i);
			if(hs.contains(c))
				str3 = str3 + c;
			}
		return str3;

}

- anim May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ String common(String s1, String s2) { Character c = null; String str3 =""; LinkedHashSet<Character> hs = new LinkedHashSet<Character>(); for(int i = 0; i < s1.length(); i++) { hs.add(s1.charAt(i)); } for(int i = 0; i < s2.length(); i ++) { c = s2.charAt(i); if(hs.contains(c)) str3 = str3 + c; } return str3; } }} - Abhi May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String common(String s1, String s2) {
Character c = null; String str3 ="";
LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
for(int i = 0; i < s1.length(); i++) {
hs.add(s1.charAt(i));
}
for(int i = 0; i < s2.length(); i ++) {
c = s2.charAt(i);
if(hs.contains(c))
str3 = str3 + c;
}
return str3;
}

- Abhi May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CommonAlphabet {

/**
* @param args
*/
public static void main(String[] args) {
Assert.assertEquals("strng", commonChar("string", "strong"));
Assert.assertEquals(null, commonChar("", "strong"));
}

public static String commonChar(String str, String str1) {
if (str == null || str1 == null || str.isEmpty() || str1.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
Set<Character> set = new HashSet<>();
for (char ch : str.toCharArray()) {
set.add(ch);
}
for (char ch : str1.toCharArray()) {
if (set.contains(ch)) {
sb.append(ch);
}
}
return sb.toString();
}
}

- manojdandy May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int compare(unsigned char *s1,unsigned char *s2,unsigned char *ret)
{
    int len1=strlen(s1);
    int len2=strlen(s2);
    int len,i,k=0;
    /*get short string*/
    if(len1>len2) len= len2;
    else          len= len1;
    /*Check if it is 0*/
    if(len!=0)
    {

        for(i=0; i<len; i++)
        {
            if(s1[i] == s2[i])
                ret[k++]=s1[i];
        }
    }
return 0;
}

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

If only a-zA-Z as possible input characters are given, you can store the representation of one string in a long value and do a binary comparison if the current character exists. The solution runs in O(n) time and uses O(1) space.

private final int ASCII_DEC_MASK = 64;

    public String findCommonChars(String string1, String string2) {
        long string1Representation = createRepresentation(string1);
        StringBuilder commonChars = new StringBuilder(string2.length());

        for (char current : string2.toCharArray()) {
            long validate = 1 << current - ASCII_DEC_MASK;
            if ((validate & string1Representation) == validate) {
                commonChars.append(current);
            }
        }
        return commonChars.toString();
    }

    private long createRepresentation(String input) {
        long result = 0;
        for (char current : input.toCharArray()) {
            result += 1 << current - ASCII_DEC_MASK;
        }
        return result;
    }

- Scavi May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void getCommonChars(){
		String s1 = "String";
		String s2 = "Strong";
		String s3 = s1+s2;
		
		char[] sChar = s3.toCharArray();
		
		Map<Character,Integer> map = new HashMap<Character, Integer>();
		for(int i=0; i < sChar.length; i++){
			
			if(map.containsKey(sChar[i])){
				map.put(sChar[i], map.get(sChar[i])+1);
			}else{
				map.put(sChar[i], 0);
			}
		}
		System.out.println(map.toString());
	}

And just fetch the keys whose values are more than 0.

- Pankaj Gorte May 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

>>> def str(string1, string2):
intersect = set(string1) & set(string2)
return ''.join(intersect)

>>> str('string', 'strong')
'srtgn'

OR

>>> >>> def str(s1, s2):
a = ''
if len(s1) < len(s2):
for i in s2:
if i in s1:
a = '%s%s'%(a, i)
elif len(s1) > len(s2) or len(s1) == len(s2):
for i in s1:
if i in s2:
a = '%s%s'%(a, i)
return a

>>> str('string', 'strong')
'strng'

- Madhu May 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

PYTHON::

>>> def str(string1, string2):
intersect = set(string1) & set(string2)
return ''.join(intersect)

>>> str('string', 'strong')
'srtgn'

OR

>>> def str(s1, s2):
a = ''
if len(s1) < len(s2):
for i in s2:
if i in s1:
a = '%s%s'%(a, i)
elif len(s1) > len(s2) or len(s1) == len(s2):
for i in s1:
if i in s2:
a = '%s%s'%(a, i)
return a

>>> str('string', 'strong')
'strng'

- Madhu Mohan May 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def common(word1,word2):
a = []
for i in word1:
for j in word2:
if i==j:
if i not in a:
a.append(i)
break
return a


if __name__=='__main__':

print (common('string','strong'))

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

c#:

public Void FindCommonCharactersInStringAndPrint()
{
string text1 = Console.ReadLine();
string text2 = Console.ReadLine();

Dictionary<char, int> charCount = new Dictionary<char, int>();

foreach (char character in text1)
{
if (!charCount.ContainsKey(character))
{
charCount.Add(character, 1);
}
}

foreach (char character in text2)
{
if (charCount.ContainsKey(character))
{
charCount[character]=0;
}
}


foreach (char key in charCount.Keys)
{
if (charCount[key] == 0)
{
Console.Write(key);
}
}


}

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

c#:

public Void FindCommonCharactersInStringAndPrint()
{
string text1 = Console.ReadLine();
string text2 = Console.ReadLine();

Dictionary<char, int> charCount = new Dictionary<char, int>();

foreach (char character in text1)
{
if (!charCount.ContainsKey(character))
{
charCount.Add(character, 1);
}
}

foreach (char character in text2)
{
if (charCount.ContainsKey(character))
{
charCount[character]=0;
}
}


foreach (char key in charCount.Keys)
{
if (charCount[key] == 0)
{
Console.Write(key);
}
}


}

- Allwin July 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Anonymous July 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def commom_letters_string(s1, s2):
    new_str = ''
    cnt_comm_let_same_pos= 0
    cnt_comm_let = 0
    for ch, ch1 in zip(s1,s2):
            if ch == ch1:
                new_str = new_str + ch
                cnt_comm_let_same_pos = cnt_comm_let_same_pos + 1
            if ch in s2:
                cnt_comm_let = cnt_comm_let + 1
    return cnt_comm_let_same_pos,cnt_comm_let,new_str


s1 = raw_input("Enter first string: ")
s2 = raw_input("Enter second string: ")
ret = commom_letters_string(s1, s2)
print "The count of common letters at same position={} and commom letters count={}" .format(ret[0], ret[1])

- Sreenivas July 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringComp {

public static void main(String[] args) {

String a="String";
String b="Strong";

if(a.equals(b))
{
System.out.println("Both strings are same");
}
else
{
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)==(b.charAt(i)))
{
System.out.println(a.charAt(i));
}
}

}
}

- Geetha Balakrishnan July 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
// TODO Auto-generated method stub

String A = "string";
String B = "strong";
char count[] = new char[50];
char[] c = A.toCharArray();
char[] d = B.toCharArray();

int len = c.length;
int leng = d.length;
// System.out.println(len);

for (int i = 0; i < len; i++) {
for (int j = 0; j < leng; j++) {
if (c[i] == d[j])

{
count[i] = c[i];

}
}
System.out.print(count[i]);
}

}

- Prabhu July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		// TODO Auto-generated method stub

		String A = "string";
		String B = "strong";
		char count[] = new char[50];
		char[] c = A.toCharArray();
		char[] d = B.toCharArray();

		int len = c.length;
		int leng = d.length;
		// System.out.println(len);

		for (int i = 0; i < len; i++) {
			for (int j = 0; j < leng; j++) {
				if (c[i] == d[j])

				{
					count[i] = c[i];

				}
			}
			System.out.print(count[i]);
		}

	}

- Prabhu July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {// TODO Auto-generated method stub
String A = "string";
String B = "strong";
char count[] = new char[50];
char[] c = A.toCharArray();
char[] d = B.toCharArray();
int len = c.length;
int leng = d.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < leng; j++) {
if (c[i] == d[j]){
count[i] = c[i];}}
System.out.print(count[i]);

- Prabhu July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {// TODO Auto-generated method stub
String A = "string";
String B = "strong";
char count[] = new char[50];
char[] c = A.toCharArray();
char[] d = B.toCharArray();
int len = c.length;
int leng = d.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < leng; j++) {
if (c[i] == d[j]){
count[i] = c[i];}}
System.out.print(count[i]);

- Prabhu July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CommonAlpha  

	public static void main(String[] args)  
		// TODO Auto-generated method stub

		String A = "string";
		String B = "strong";
		char count[] = new char[50];
		char[] c = A.toCharArray();
		char[] d = B.toCharArray();

		int len = c.length;
		int leng = d.length;
		// System.out.println(len);

		for (int i = 0; i < len; i++)  
			for (int j = 0; j < leng; j++)  
				if (c[i] == d[j])

				 
					count[i] = c[i];

				 
			 
			System.out.print(count[i]);

- Prabhu July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void removeNonDup(String s1, String s2){
		if(s1==null || s2 == null){
			System.out.println("string cant be null");
		}
		else{
		char[] c1 = s1.toCharArray();
		char[] c2 = s2.toCharArray();
		StringBuffer sb = new StringBuffer();
		HashSet<Character> m = new HashSet<Character>();
		for(char ch : c1){
				m.add(ch);
		}
		for(char ss : c2){
			if(m.contains(ss)){
				sb = sb.append(ss);
			}
		}
	
		System.out.println(sb);
		}
	}

- anonymousNg November 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python:
a = 'string'
b = 'strong'
c = []
for i in range(0,len(a)):
if a[i] in b:
c.append(a[i])
print "".join(c)

- Anonymous August 10, 2018 | 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