Amazon Interview Question for Quality Assurance Engineers


Country: India
Interview Type: Written Test




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

Always remember that whenever a string question is thrown at you, just remember this "A string is noting but an array of character that ranges from 0-256 chars(assuming all the char are alphabets)"

Now to the question,
you can sort the first and sort the second using library function,
and compare each char by char
Time Complexity -> O(n*logn)
## If len(str2) != len(s1) -> return false, strings of uneven lengths can never be anagrams

- Gaurav May 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A byte, 8 bits, can assume values from 0 to 255. Therefore having 256 unique possible values. Not "0-256" as mentioned

- Felipe May 28, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can just compute the hash function if they have the same characters they will result in the same hash value. which can be done in O(n)

- brahma August 13, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
class Anagrams {

  static boolean isAnagram(String a, String b) {
        char c[] = a.toLowerCase().toCharArray();
        char d[] = b.toLowerCase().toCharArray();
        Arrays.sort(c);
        Arrays.sort(d);
        if(Arrays.equals(c,d)){
            return true;
        }
        return false;
    }



  public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
    System.out.println("Enter first String: ");
    String a = scan.next();
    System.out.println("Enter seconf jumbled String");
    String b = scan.next();
    scan.close();
    boolean ret = isAnagram(a, b);
    System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
    }
}

- Sameer May 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this would increase the time complexity to O(nlogn).

- Naren May 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For checking that two strings are an anagram of each other, we can use an unordered set in c++ and insert all the characters of the first string and then iterate over the second string and keep checking that the characters in second string must be present in the set if not then both strings are not anagrams of each other else they are.

Implementation:

#include<bits/stdc++.h>
using namespace std;
bool findanagram(string s1, string s2){
unordered_set<char> s;
int n1 = s1.length();
int n2 = s2.length();
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
for(int i = 0; i < n1; i++)
s.insert(s1[i]);
for(int j = 0; j < n2; j++){
if(s.find(s2[j]) == s.end())
return false;
}
return true;
}
int main()
{
string str1 = "LISTEN";
string str2 = "silent";
cout<<findanagram(str1, str2)<<endl;

return 0;
}

- swapnilkant11 May 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isAnagram(String s, String t) {
        if (s == null || t == null || s.length() != t.length()) {
            return false;
        }
        char[] map = new char[256];
        for (char c : s.toCharArray()) {
            map[c]++;
        }

        for (char c : t.toCharArray()) {
            map[c]--;
            if (map[c] < 0) {
                return false;
            }
        }
        return true;
    }

- naren May 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we have to use byte[] instead char[].

- venkataramanchary October 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static boolean isAnagram(String str1,String str2){
        if(str1.length()==0||str2.length()==0||str1.length()!=str2.length()){
            return  false;
        }
        byte[] map = new byte[256];
        for(char c:str1.toCharArray()){
            map[c]++;
        }
        for(char c:str2.toCharArray()){
            map[c]--;
            if(map[c]<0){
                return false;
            }
        }
        return true;
    }

- venkataramanchary October 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Solution in Javascript

function anagramCheck(str,strg) {
   var arr =[];
   var arr2 = [];

   a = str.toLowerCase();
   b = strg.toLowerCase();

   arr = a.split('').sort();
   arr2 = b.split('').sort();

 if (arr.length === arr2.length) {
    for (i=0;i<arr.length; i++) {
      if (arr[i] !==arr2[i]) {
          return false;
      }
    }
    return true;
 } else {
     return false;
 }
}

- harshsheetal18 June 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<vector>
#include<algorithm>
#include<string>
#include<set>

using namespace std;
typedef vector <int> VI;
int main()
{
//code
int t;
cin >> t;
while (t--)
{
string s,st;
int f = 0;
cin >> s;
cin >> st;
VI v1(26, 0);
VI v2(26, 0);
for (int i = 0; i < s.length(); i++)
{
v1[s[i] - 'a']++;
v2[st[i] - 'a']++;
}
for (int i = 0; i < 26; i++)
{
if (v1[i] != v2[i])
{
f = 1;
break;
}
}
if (f == 1)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}

- ravircit August 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def isAnagram(s1,s2):
	if len(s1)!=len(s2):
		return False
	d={}
	for x in s1:
		if x not in d:
			d[x]=1
		else:
			d[x]+=1
	for x in s2:
		if x not in d or d[x]==0:
			return False
		else:
			d[x]-=1
	return True

- tuhinmaji314 October 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isAnagram(String str1,String str2){
        if(str1.length()==0||str2.length()==0||str1.length()!=str2.length()){
            return  false;
        }
        byte[] map = new byte[256];
        for(char c:str1.toCharArray()){
            map[c]++;
        }
        for(char c:str2.toCharArray()){
            map[c]--;
            if(map[c]<0){
                return false;
            }
        }
        return true;
    }

- Anonymous October 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isAnagram(String str1,String str2){
        if(str1.length()==0||str2.length()==0||str1.length()!=str2.length()){
            return  false;
        }
        byte[] map = new byte[256];
        for(char c:str1.toCharArray()){
            map[c]++;
        }
        for(char c:str2.toCharArray()){
            map[c]--;
            if(map[c]<0){
                return false;
            }
        }
        return true;
    }

- venkat.paloju October 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import collections
str1 = 'LISTEN'
str2 = 'SILENT'
str3 = 'SILENE'

def is_anagram(str1,str2):
if len(str1)== len(str2):
if collections.Counter(str1) == collections.Counter(str2):
return True
else:
return False
return False

print(is_anagram(str1,str2))
print(is_anagram(str1,str3))

- Anonymous April 07, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isAnagram(String anagramStr, String verifyStr) {

boolean anagram = false;

String reverse = "";
char []strChar = verifyStr.toCharArray();
char revChar;
int j = strChar.length-1;

for(int i = 0; i<= strChar.length-1; i++) {

revChar = strChar[j];
j--;

reverse += revChar;
}
System.out.println("Reverse String :"+reverse);
if(anagramStr.equalsIgnoreCase(reverse)) {
anagram = true;
}

return anagram;

}

- srinivasan.sindhu1 August 24, 2020 | 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