Amazon Interview Question for Software Engineer in Tests






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

Anagrams are well defined and by that definition AABBC and CBBAA are anagrams.

Please define the question properly.

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

Why are last two strings not anagrams ?

- stupid August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think the point of the question is to find only anagrams that are not palindromes

- Anonymous August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please check the compilation errors. The logic is to take a Map Data structure, Keys will be the characters in Strings and value will be the frequency of characters. In the first run i will populate the map and in the second run i will depopulate it. If the map is empty then the strings are empty.

public boolean checkAnagram(String a, String b)
{
if(a.length()!= b.length())
return false;
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i = 0; i<a.length();i++)//Sorry for not using enahanced for loop.
{
if(!(hm.containsKey(a.charAt[i])))
hm.put(a.charAt[i],1); Putting the frequency as 1 for the first time
else
{
int val = hm.get(a.charAt[i]);// Not casting to Integer as it is automatically done by Java
hm.put(a.charAt[i],++val)// Increased the frequency by 1
}
}// close the first for loop
for (int i= 0; i <b.length();i++)
{
if(hm.containsKey(b.charAt[i])
{
int val = hm.get(b.charAt[i])
if (val == 1)
hm.remove(b.charAt[i])
else
hm.put(b.charAt[i],--val)
}
}// close the 2nd for loop
return hm.isEmpty()
}// Method close

- Nishank Rajvanshi August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the two string and check if they are equal. We can use a variant of count sort if the characters are ASCII. Btw, last two are anagrams (by general definition of anagram)

- bytestorm August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Store the count of each charcter in S1 into a seperate Hashtable
2. Iterate through each character of s2 and decrement the count of the corresponding key, once the count=1 remove that element. Also check if that key exists in that hastable, if not its not an anagram.
Orde is O(n)

-- Psuedo code explained on Nishank Rajvanshi Program

- San August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's simple, I guess wats intended is that the relative distance or displacement of characters should remain the same. So the soln for this would be to append the same string in itself and now start checking if the original string is present in this String or not.

- ishantagarwal1986 August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and by "orignial String" I mean the other string, which we have NOT appended.

- ishantagarwal1986 August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is the meaning of Anagram?

- Paresh August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you rearrange the letters of the word using every letter exactly once.
Basically combinatoric permutation

- AJ August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

abe paresh rawal, just google it!!!

- Anonymous September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsAnagram(char* s1, char* s2) {
char hash[256] = { 0 };
if (strlen(s1) != strlen(s2)) return false;
for(int i=0;i<strlen(s1);i++)
hash[s1[i]]++;
for(int i=0;i<strlen(s2);i++) {
if(s1[i] == s2[i] ||
--hash[s2[i]] < 0) return false;
}
return true;
}

- CareerCup August 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

srikanth, i didn't understand why you put the condition if(s1[i] == s2[i] || --hash[s2[i]] < 0). If you take ABCA and CBAA, first condition fails at s1[1] == s2[1] which returns false saying that both are not anagrams, which is not correct. If you take AABBCC and BCA, your condition never become false. So it returns true, which is not correct.

- Ravi August 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Reference Wiki: An anagram is .... the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

So ABCA and CBAA are not anagrams since letter B and last letter A are not rearranged. As mentioned in the question AABBC and CBBAA are not anagrams as third B is not changed.

And regarding AABBCC and BCA, if (strlen(s1) != strlen(s2)) return false; should takecare.

- CareerCup August 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Srikanth for clarifying. I misunderstood the problem. If the interviwer put a contriant on space complexity. Is the follwing solutions works? But it is a little bit more time complexity O(nlogn)

bool IsAnagram(char* s1, char* s2) 
{
    int length1 = strlen(s1);
    if (length1 != strlen(s2)) return false;
    int length2 = length1 * 2;
    s1 = strcat(s1,s2);
    for(int i=0;i<length2;i++) 
    {
        int count = 0;
        for(int j=0;j<length2; j++)
        {
            if(s1[i] == s1[j])
            {
                if(j == i+length1-1) return false;
                else 
                {
                    count++;
                }
                if(j < i) // It means jth character already counted and no need to do again, reducing time complexity
                {
                    count = 0;
                    break;
                }
            }
        }
        if(count%2 != 0) return false;
    }
    return true;
}

- Ravi August 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import Counter,OrderedDict
def anagramcheck(str1,str2):

if OrderedDict(Counter(str1))==OrderedDict(Counter(str2)):
return True
else:
return False

answer=anagramcheck('AABaaBC','CBBAAaa')
print answer

- Shubha October 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import Counter,OrderedDict
def anagramcheck(str1,str2):

    if OrderedDict(Counter(str1))==OrderedDict(Counter(str2)):
        return True
    else:
        return False

answer=anagramcheck('AABaaBC','CBBAAaa')
print answer

- Shubha October 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import Counter,OrderedDict
def anagramcheck(str1,str2):

    if OrderedDict(Counter(str1))==OrderedDict(Counter(str2)):
        return True
    else:
        return False

answer=anagramcheck('AABaaBC','CBBAAaa')
print answer

- Shubha October 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. Store the count of each charcter in S1 into a seperate
2. Iterate through each character of s2 and decrement the count of the corresponding key, once the count=1 remove that element. Also check if that key exists in that hastable, if not its not an anagram.
Orde is O(n)

-- Psuedo code explained on Nishank Rajvanshi Program

- San August 21, 2011 | 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