## Oracle Interview Question for Software Engineer / Developers

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

i think xoring of all integers values of characters should be 0 if they are anagram.

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

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

i don't think so... aabbccdd and ffgghhii will give xor as 0

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

``````// Write a method to decide if two strings are anagrams or not.
// Complexity O ( 2n + k)  avg, since s1 and s2 should contains the same length otherwise It will return false immediate
public static boolean isAnagram(char s1[], char s2[]){
if(s1.length != s2.length) return false;
int letters [] = new int[256];
for( int i = 0; i < s1.length; i++){
letters[toUpperCase(s1[i])]++;
}
for( int j = 0; j < s2.length; j++){
letters[toUpperCase(s2[j])]--;
if(letters[toUpperCase(s2[j])] < 0) return false;
}
for(int x = 0; x < letters.length; x++){
if(letters[x] != 0 ) return false;
}
return true;
}``````

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

What if you character set is not ASCII (256) but UNICODE (2^16 or something)? The complexity of the above solution would be enormous.

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

I would be worse if you try to save each letter and iterate one by one. the complexity will be >= O (n square )

I dont see a better solution. do you ?

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

Just sort the 2 strings and compare, O(nlogn)

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

``````public static boolean isAnagram(String word, String anagram) {
if(word.length() != anagram.length())
return false;
char[] chars = word.toCharArray();
for(char c : chars)
{
int index = anagram.indexOf(c);
if(index != -1){
anagram = anagram.substring(0,index) +
anagram.substring(index +1, anagram.length());
}
else {
return false;
}
}
return anagram.isEmpty();
}``````

Time Complexity: O(n)

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

public static boolean IsAnagram(String str1, String str2){
if(str1.length() != str2.length()){
return false;
}
char[] charArr1 = str1.toLowerCase().toCharArray();
char[] charArr2 = str2.toLowerCase().toCharArray();

Character c;
int hashCodeValue1 =0;
int hashCodeValue2 =0;
for(int i=0;i<charArr1.length;i++){
c = charArr1[i];
hashCodeValue1+=c.hashCode();
}
for(int i=0;i<charArr2.length;i++){
c = charArr2[i];
hashCodeValue2+=c.hashCode();
}

if(hashCodeValue1 == hashCodeValue2)
return true;

return false;
}

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

i don't know how to code yet, but here are the steps i think are needed:
1) take both strings in two seperate variables
2) compare length of variables if yes then goto next step
3) reverse any string, then compare with the other string. if yes then anagram

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.