Amazon Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: Phone Interview
import java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
String x ="String";
String y = "strong";
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < x.length(); i++) {
if(y.indexOf(x.charAt(i))!=-1){
set.add(x.charAt(i));
}
}
char[] xxx = new char[set.size()];
int i =0;
for (char xx : set) {
xxx[i] = xx;
i++;
}
String xxxx = new String(xxx);
System.out.println(xxxx);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String x ="String";
String y = "Strong";
Set<Character> set = new LinkedHashSet<Character>();
for (int i = 0; i < x.length(); i++) {
System.out.println(y.indexOf(x.charAt(i)));
if(y.indexOf(x.charAt(i))!=-1){
set.add(x.charAt(i));
}
}
Iterator<Character> it = set.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
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]);
}
public class PrintCommonString {
public static void main(String[] args) {
// set the first Strings
String s1= "strings strings";
String s2= "strongs strongs" ;
// change string to character Array
char[] arrayS1=s1.toCharArray();
char [] arrayS2= s2.toCharArray();
// compare String one by one and print the common on
for (int i=0; i<arrayS1.length;i++) {
if (arrayS1[i]==arrayS2[i]) {
System.out.print(arrayS1[i]);
}
}
}
}
1. Both valid strings
2. single characters
3. string1 with lower case, string2 with uppercase with same alphabets
e.g. "string","STRING"
4. Both empty strings
6. Both are nulls
7. Both with blank spaces
8. Both with special chars like \t,\n
9. Performance testing to check the performance of the program by increasing the characters in both the strings
10. Load testing-->check how the program is impacted with increased number of users
11. Stress testing
12. Endurance testing-->continuously sending the strings for long duration
13. Localization testing(if required)-->to check if the program supports localization
Here is my take in C#:
string inpStr1 = "String";
string inpStr2 = "Strong";
string newStr = "";
for (int i1 = 0; i1 < inpStr1.Length; i1++)
{
for (int i2 = 0; i2 < inpStr2.Length; i2++)
{
if (inpStr1[i1].ToString() == inpStr2[i2].ToString())
{
newStr += inpStr1[i1].ToString();
}
}
}
Console.WriteLine("Output string: " + newStr);
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);
}
}
public static void main(String args[]){
RemoveNonDupCharBetween2Strings r = new RemoveNonDupCharBetween2Strings();
r.removeNonDup("string", "strong");
r.removeNonDup("", "");
r.removeNonDup(" ", " ");
r.removeNonDup(null, null);
r.removeNonDup(null, "1234");
r.removeNonDup("12346", "12345");
r.removeNonDup("sstring", "sstrong");
r.removeNonDup("sstrign", "sstrgno");
}
1.Check if both null
2.Check if ("","strong")
3.Check if("string","")
4.Check if capital sensitive
5.Check if ("a","a")
6.Check if two long string
7.Check if duplicate character
8.Check if specific characters
9.Check if (" "," ")
10.Check if(null,null)
11.Check if ("",null)
12.Check if both numbers
Test Cases :
1. Check for happy paths
2. Check for one null and other string
3. Check for one string and other null value
4. Check for both null
5. Check for both empty strings
6. Check for case sensitivity
7. Check for whitespaces
8. Check with different lenghts
9. Check for matching chars at different positions
10. Check for special characters
11. Check for numbers in strings
12. Check for strings with spaces in between for eg. Email me when done and Email you when not done
13. Check for extremely long strings.
14. Check for endurance testing on this
public static void main(String[] args) {
String str1 = "Strong";
String str2 = "String";
if(str1 == null || str1.length() == 0 || str2 == null || str2.length() == 0){
System.out.println("String/s null");
}
String res = "";
for(int i=0 ; i< str1.length() ; i++){
if (str1.charAt(i)==str2.charAt(i)){
res= res + str1.charAt(i);
}
else {
continue;
}
}
System.out.println(res);
}
public static void main(String[] args) {
String str1 = "Strong";
String str2 = "String";
if(str1 == null || str1.length() == 0 || str2 == null || str2.length() == 0){
System.out.println("String/s null");
}
String res = "";
for(int i=0 ; i< str1.length() ; i++){
if (str1.charAt(i)==str2.charAt(i)){
res= res + str1.charAt(i);
}
else {
continue;
}
}
System.out.println(res);
}
1. f (string, strong) // happy path
- guilhebl May 04, 20172. f (null, null) // both null
3. f ("", "") // both empty strings
4. f (" ", " ") // both empty strings with spaces - blank strings
5. f ("abc", "zxy") // no matches
6. f ("!@#$", "1@#!@") // special chars
7. f (null, "somestring") // 1st param null other not
8. f ("somestring", null) // 2nd param null other not
9. Test with very large inputs
10. f ("azzzzzzzz", "zzzzzzza") // mathcing in different positions
In general terms these are some sample test cases to begin with, but
other tests that could be included as necessary.