Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Sort the two strings and compare the characters. If any1 character doesnt match these are anagrams else they are not

- Ankur October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But u'd also have to remove spaces ..For ex:

String 1: Tom Marvolo Riddle

String 2: I am Lord Voldemort

we'd have to remove spaces to compare.


Solution 2: We could assign prime numbers to alphabets (a=1,b=2, c=3..)
and find corresponding product of each string. If they match.. then strings are anagrams.

- Anonymous November 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Anonymous and Ankur what will be the complexity of this program...?

- Nikhil` December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nikhil: I guess the fastest sort (quick sort) would be O(nlogn)

- dnair89 January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

traverse through the array and call a hashkey() funtion for each charecters which will return an unique key. The sums of these keys can be maintained for both strings , if the sums match in the end , then , they are anagrams . timecomplexity....o(n)

- MC October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

tht will fail if: eg;
ur harsh function return smthng like this:
a=4
b=1
c=3
they are all unique, but str1=a and str2=bc,
then as per your algo it will result in analgram which is not true

- Anonymous October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then we have to use hash table only or is there any other way.??

- MC October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean anagram(String s, String t){
		if (s.length() != t.length())
			return false;
		int[] letters = new int[256];
		int unique_num_s = 0;
		int completed_num_s = 0;
		
		for(int i = 0; i < s.length(); ++i){
			if (letters[s.charAt(i)] == 0){
				unique_num_s++;
			}
			letters[s.charAt(i)]++;
		}
		for (int i = 0; i < t.length(); ++i){
			int c = t.charAt(i);
			if (letters[c] == 0)
				return false;
			letters[c]--;
			if (letters[c] == 0){
				completed_num_s++;
				if(unique_num_s == completed_num_s){
					return i == (t.length() - 1);
				}
			}
		}
		return false;
	}

- Anonymous October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The main idea is checking if the two strings have identical count for each unique char

- Anonymous October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code doesnot take into account that capital and small letters are to be treated same. so Time and Mite are not anagrams according to your code.

In addition it also counts ascii 32 i.e. spaces. If number if spaces aren't then its not anagram!

- uber_geek December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//check if two Unicode strings are anagrams or not
class HashTable
{
public:
HashTable();
~HashTable();

int Put(WCHAR key, unsigned value);
int Get(WCHAR key, unsigned &value);
int Increment(WCHAR key, unsigned &value);
int Decrement(WCHAR key, unsigned &value);
};
int isAnagrams(LPWSTR pwszS1, LPWSTR pwszS2, BOOL &result)
{
int rv = 0;
HashTable *ht = new HashTable();
unsigned value = 0;
unsigned uniqueChars = 0;
unsigned completedChars = 0;
unsigned i = 0;
unsigned size = 0;

result = FALSE;

if (ht == NULL)
{
rv = -1;
goto exit;
}

if (pwszS1 == NULL)
{
if (pwszS2 == NULL)
{
result = TRUE;
}
goto exit;
}

size = wcslen(pwszS1);
if (size != wcslen(pwszS2))
{
goto exit;
}

for (i=0; i<size; i++)
{
if (ht->Increment(pwszS1[i], value))
{
rv = -1;
goto exit;
}
if (value == 1)
{
uniqueChars++;
}
}

for (i=0; i<size; i++)
{
if (ht->Get(pwszS2[i], value))
{
rv = -1;
goto exit;
}

if (value==0)
{
result = FALSE;
goto exit;
}
if (value==1)
{
completedChars++;
if (completedChars == uniqueChars)
{
result = i==size-1;
goto exit;
}
}

if (ht->Put(pwszS2[i], value-1))
{
rv = -1;
goto exit;
}
}

exit:
if (ht)
{
delete ht;
}
return rv;
}

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

strcat the first string with itself and check whether the second string is a substring of the first.

space complexity O(n)

- Anonymous October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And this checks for a permutation. Not an anagram

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

3 loops each with order N

1. char hash[128] memset all to 0
2. if lengths are not same, just break and print not anagrams
3. go into loop whenever u encounter an element increment the particular index by 1

go through the index if anything mod 2 is not equal to 0, then they re not angrams !

simple

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

<pre lang="" line="1" title="CodeMonkey28380" class="run-this">
class Solution1 {
public boolean areAnagrams(String a, String b) {
int la = a.length();
int lb = b.length();
if(la == 0) return lb == 0;
else if(lb == 0) return la == 0;
else if (la != lb) return false;
for(int i = 0; i < la; ++i) {
if(a.charAt(i) != b.charAt(lb - (i + 1))) {
return false;
}
}
return true;
}

public static void main(String[] args) {
Solution1 instance = new Solution1();
System.out.println(instance.areAnagrams("", ""));
System.out.println(instance.areAnagrams("", "A"));
System.out.println(instance.areAnagrams("A", ""));
System.out.println(instance.areAnagrams("ABC", "CBA"));
System.out.println(instance.areAnagrams("ABC", "ABC"));
System.out.println(instance.areAnagrams("ABC", "BA"));
}
}
</pre><pre title="CodeMonkey28380" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey42021" class="run-this">// Sorry about that
import java.util.HashMap;
import java.util.Map;


class Solution1 {

Map<Character, Integer> countChars(String aString) {
Map<Character, Integer> counter = new HashMap<Character, Integer>();
for(int i = 0; i < aString.length(); ++i) {
char curChar = aString.charAt(i);
Integer count = counter.get(curChar);
if(count == null) {
count = new Integer(1);
} else {
count++;
}
counter.put(curChar, count);
}
return counter;
}

public boolean areAnagrams(String a, String b) {
int la = a.length();
int lb = b.length();
if(la == 0) return lb == 0;
else if(lb == 0) return la == 0;
else if (la != lb) return false;
return countChars(a).equals(countChars(b));
}

public static void main(String[] args) {
Solution1 instance = new Solution1();
System.out.println(instance.areAnagrams("", ""));
System.out.println(instance.areAnagrams("", "A"));
System.out.println(instance.areAnagrams("A", ""));
System.out.println(instance.areAnagrams("ABC", "CBA"));
System.out.println(instance.areAnagrams("ABC", "ABC"));
System.out.println(instance.areAnagrams("ABC", "ABCC"));
System.out.println(instance.areAnagrams("ABC", "BA"));
}
}
</pre><pre title="CodeMonkey42021" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void anagram()
{
char s[100],t[100];
int len;
printf("Enter first string\n");
scanf("%s",s);
printf("Enter second string\n");
scanf("%s",t);
len=strlen(s);
if(len==strlen(t))
{
int res=0,i;
for(i=0;i<len;i++)
{
res=res+s[i];
res=res-t[i];
}
if(res==0)
printf("ANAGRAM\n");
else
printf("Not anagram\n");
}
else
printf("Not anagram\n");

}

- PUCSD December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above algo is wrong as the value of res may become 0 even when they are not anagrams ex check with adgh achh ..it will still return 0..we cant just check the sum ..

- kunal May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So couple of strategies here
1. have an array of int from 1-256 and increment the count of each bit for the corresponding hit
2. Convert them to a upper or lowerbase generate hash for the string - ofcourse the hash algo should be strong
3. Do a sort of the string and compare.

- mvishnu2005 February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a bitset:

public static boolean areAnagram(String a, String b){

boolean areAnagram = false;

if(a.length()==b.length()){

int count = 0;

char[] aArray = a.toCharArray();
char[] bArray = b.toCharArray();

boolean[] bitSet = new boolean[256];

for(int i=0;i<aArray.length;i++){

bitSet[aArray[i]] = true;
}

for(int i=0;i<bArray.length;i++){

if(bitSet[bArray[i]]==true)
count++;
}

if(count==a.length())
areAnagram = true;
}

return areAnagram;
}

- Andrei Marfievici May 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmz, I didn`t really test it, I have to count the appearances of each char and validate.

- andrei.marfievici May 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution for this one.

public class AnagramCheck {
    //remove whitespaces, ignore case, merge sort and check?
    public static boolean checkAnagram(String input1, String input2)
    {

        if (input1.length() == input2.length())
        {
            //remove whitespaces
            input1 = input1.replaceAll("\\s", "");
            input2 = input2.replaceAll("\\s", "");
            input1 = input1.toLowerCase();
            input2 = input2.toLowerCase();
         char[] charArray1 = input1.toCharArray();
         char[] charArray2 = input2.toCharArray();
        Arrays.sort(charArray1); //dual-pivot quicksort O(n log n)
        Arrays.sort(charArray2); //dual-pivot quicksort O(n log n)
            //O(2nlogn) at this point
            boolean temp = false;
            //one pass of n needed since lengths should be equal so total O(3n log n)
            for (int i = 0; i < charArray1.length; i++)
            {
                  if (charArray1[i] == charArray2[i])
                  {
                      temp = true;
                  } else {
                      temp = false;
                  }
            }
            return temp;
        }
        return false;
    }
    public static void main(String[] args)
    {
        String input1 = "MAry";
        String input2 = "Army";
        System.out.println(checkAnagram(input1, input2));
    }
}

- Anonymous November 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Approach 2> Time O(2n) Space O(1)
Maintain a 26 size array, each character represents a character
For every occurence of a character in String 1 increment the count at the index position by 1
For every occurence of a character in String 2 decrement the count at the index position by 1
After traversing through all the characters check if the sum of values at all the index positions is 0. in O(1) time

- Gaurav April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This C# simple code check for anagrams

public static void Main(string[] args)
{
string str1 = "maada";
string str2 = "aamda";
int sum1 = 0;
int sum2 = 0;

str1 = str1.Replace(" ", "");
str2 = str2.Replace(" ", "");

foreach (char alp in str1)
            {
                sum1 = sum1 + (int)alp;
            }
            foreach (char alp in str2)
            {
                sum2 = sum2 + (int)alp;
            }

            if(sum1==sum2)
            {
                Console.WriteLine("strings are anagrams");
                Console.ReadLine();
            }
        }

- Purushothama December 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

BOOL is Anagram(char *str1, char *str2, unsigned size)
{
for (int i = 0; i < size/2; i++)
{
if (str1[i] != str2[size-i])
return FALSE;
}
return TRUE;
}

- Anonymous October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this checks for a palindrome, not an anagram.

- noop November 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Simple take two arrays and keep on incrementing the index values corresponding to the ASCII values of the characters in two strings, if arrays match completely the strings are anagrams.

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

Use one array first string increase counter and second string decrease counter at final all index must be 0 or return false

- Murat Karakus October 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi ishantagarwal and murat..
can u guys pls explain your solutions.. with xample ..
I am not able to understand it..

- mgr October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

let str1="abcb" str2="bbca"
so hash str1
i.e. hash[a]=1,hash[b]=2,hash[c]=1
now traverse thru str2 and decrement the count in hash[]
i.e.
b read => hash[b]-- => hash[b]=1;
b read => hash[b]-- => hash[b]=0;
b read => hash[c]-- => hash[c]=0;
b read => hash[a]-- => hash[a]=0;
now check hash[]
if all values in hash are 0 ,then they are anagrams;

- anonymous June 26, 2012 | Flag


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