## 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.``````

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();``````

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

How to get the uncommon letters ?

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);
}

}``````

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();
for(char c1:c)
{
}

Iterator<Character> l1 = l.iterator();
while(l1.hasNext())
{
System.out.print(l1.next());
}

}
}``````

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

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

``read string``

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

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

}

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

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

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

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

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

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

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

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

}

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; } }}
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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()) {
}
for (char ch : str1.toCharArray()) {
if (set.contains(ch)) {
sb.append(ch);
}
}
return sb.toString();
}
}

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;
}``````

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;
}``````

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.``````

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'

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'

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'))

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))
{
}
}

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);
}
}

}

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))
{
}
}

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);
}
}

}

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

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])``````

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));
}
}

}
}

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]);
}

}

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]);
}

}``````

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]);``````

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]);

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]);``````

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){
}
for(char ss : c2){
if(m.contains(ss)){
sb = sb.append(ss);
}
}

System.out.println(sb);
}
}``````

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)

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

public static String FindCommonCharcInString(String A1, String A2)
{
int str1Len = A1.length(), str2Len = A2.length();
String OutPutString = "";
if(str1Len == str2Len)
{
System.out.println("The String are of Same Length");

for(int i=0; i< str1Len; i++)
{
if(A1.charAt(i)==A2.charAt(i))
{
OutPutString += A1.charAt(i);
}
}

}
else
{
System.out.println("The String are of Not Same Length");

int GetTheMinStringLength = GetTheLeastLength(str1Len, str2Len);

for(int i=0; i< GetTheMinStringLength; i++)
{
if(A1.charAt(i)==A2.charAt(i))
{
OutPutString += A1.charAt(i);
}
}
}

return OutPutString;

}

public static int GetTheLeastLength(int A1, int A2)
{
if(A1 > A2) { return A2; }
else { return A1; }
}

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ {{{public static String FindCommonCharcInString(String A1, String A2) { int str1Len = A1.length(), str2Len = A2.length(); String OutPutString = ""; if(str1Len == str2Len) { System.out.println("The String are of Same Length"); for(int i=0; i< str1Len; i++) { if(A1.charAt(i)==A2.charAt(i)) { OutPutString += A1.charAt(i); } } } else { System.out.println("The String are of Not Same Length"); int GetTheMinStringLength = GetTheLeastLength(str1Len, str2Len); for(int i=0; i< GetTheMinStringLength; i++) { if(A1.charAt(i)==A2.charAt(i)) { OutPutString += A1.charAt(i); } } } return OutPutString; } public static int GetTheLeastLength(int A1, int A2) { if(A1 > A2) { return A2; } else { return A1; } } }}}
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
char b[]="bidaa";
int l1=strlen(a);
int l2=strlen(b);
char *c=NULL;
if(l1>l2)
c=(char *)malloc(sizeof(char)*l1);
else
c=(char *)malloc(sizeof(char)*l2);

int i,j,count=0;

for(i=0;a[i];i++)
{
for(j=0;b[j];j++)
{
if(a[i]==b[j])
{
c[count++]= a[i];
b[j]='*';
j=0;
break;
}
else
continue;
}
}
c[count]='\0';
printf("%d\n%s\n",count,c);
return 0;
}

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

int main() {
char b[]="bidaa";
int l1=strlen(a);
int l2=strlen(b);
char *c=NULL;
if(l1>l2)
c=(char *)malloc(sizeof(char)*l1);
else
c=(char *)malloc(sizeof(char)*l2);

int i,j,count=0;

for(i=0;a[i];i++)
{
for(j=0;b[j];j++)
{
if(a[i]==b[j])
{
c[count++]= a[i];
b[j]='*';
j=0;
break;
}
else
continue;
}
}
c[count]='\0';
printf("%d\n%s\n",count,c);
return 0;
}

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

``````#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
char b[]="bidaa";
int l1=strlen(a);
int l2=strlen(b);
char *c=NULL;
if(l1>l2)
c=(char *)malloc(sizeof(char)*l1);
else
c=(char *)malloc(sizeof(char)*l2);

int i,j,count=0;

for(i=0;a[i];i++)
{
for(j=0;b[j];j++)
{
if(a[i]==b[j])
{
c[count++]= a[i];
b[j]='*';
j=0;
break;
}
else
continue;
}
}
c[count]='\0';
printf("%d\n%s\n",count,c);
return 0;
}``````

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

``````function comAlphString(str, strg) {
strgCm = '';
const len = str.length >= strg.length ? str.length:strg.length;
for (i=0; i<len; i++) {
for (j=0; j<len; j++) {
if (str.charAt(i) == strg.charAt(j)) {
strgCm = strgCm + str.charAt(i);
}
}
}
return strgCm;
}
console.log(comAlphString('string','strong'));``````

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.

### 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.