Google Interview Question for Software Engineer in Tests






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

oh,sorry i got the point,thanks

- aryan.bit April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

finding anagrams is m! so order is going to be O(n*m!). lets say n is 2000. m is 20. Order in your solution is def.ly not O(n). I gave an O(n) solution to the interviewer which was correct. but, he seemed to hint at a better solution (mine was taking 2*n operations in the worst case). I will post my solution tomorrow.

- Anony April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anagram_count = 0
SB = Sorted version of B
Create C that contains 1 of every character in B

for ( i = 0 .. length(A) )
{
D = A.substring( i thru ( i + length(B) );
for ( j = 0 .. length(D) )
{
// Anagram test for A[i] thru A[i + length(B)]
if ( C.indexOf( A[ j ] ) == -1 )
{
//A[j] is not in B, so skip ahead to next char in A
i = j + 1;
break;
}
}
Sort D;
if ( compare( D, SB ) == 0 )
{
anagram_count++;
}
}
return anagram_count;

- nd_100 April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anagram_count = 0
SB = Sorted version of B

Reposting as I corrected minor loop skip...

function get_anagram_count( A, B)
{
Create C that contains 1 of every character in B

for ( i = 0 .. length(A) )
{
D = A.substring( i thru ( i + length(B) );
skip = false;
for ( j = 0 .. length(D) )
{
// Anagram test for A[i] thru A[i + length(B)]
if ( C.indexOf( A[ j ] ) == -1 )
{
//A[j] is not in B, so skip ahead to next char in A
i = j + 1;
skip = true;
break;
}
}

if ( skip )
{
next;
}
Sort D;
if ( compare( D, SB ) == 0 )
{
anagram_count++;
}
}
return anagram_count;
}

- nd_100 April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[26]
for i=0 to m-1{
a[A[i]-'a']++
a[B[i]-'a']--
}
for i=0 to 25{
if all a[i]==0
return true
}
for i=m to n-1{
a[B[i]-'a']--
a[B[i-m]-'a']++
for i=0 to 25{
if all a[i]==0
return true
}
}
return false

- Sathya April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void f(int A[], int aSize, int B[], int bSize)
{
   int bCharTotal = 0;
   int aCharTotal = 0;
   if(bSize > aSize)
   {
      printf("Array A is smaller than B, anagram not possible");
      return;
   }
   for(int i = 0; i < bSize; i++)
   {
      aCharTotal += A[i];
      bCharTotal += B[i];
   }
   

   for(int i = bSize - 1; i < aSize; i++)
   {
      if(aCharTotal == bCharTotal)//anagram possible
      {
         int temp = aCharTotal;
         for(int j = 0; j < bSize; j++)
            temp -= b[i];
         if(temp == 0)
         {
            printf("anagram found!\n");
            printf("Substring on A from %d to %d is the anagram", i - bSize, i);
            return;
         }

      }
      //Shift the window total right by one
      aCharTotal -= A[i-bSize];  //Remove the first character on the Total
      if(i+1 < aSize) aCharTotal += A[i];  //add the next character on A array to the total
   }
   printf("No anagram found!\n");
}

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

Hi Mat,
Can you explain logic behind this for loop
for(int j = 0; j < bSize; j++)
temp -= b[i];

Thanks

- Dreamer April 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

When I saw the solutions here I got one doubt. Does the question says that
1. for a given B find a substring in A which is anagram of B?
Or it says
2. for a given B find a substring in A which contains anagram of B?
e.x.

1.
A=ghefdckjcaaadebclpq
B=aabedcca
here in A one of the substring is "caaadebc" which is anagram of B

2. 
A=efcadckjaghadeblpcq
B=aabedcca
Here here in A one of the substring is "cadckjaghadeblpc" which contains anagram of B.

Solution posted above works for 2 only.
@Sathya, Mat, Anony please correct me if I m wrong.

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

What I understand about the problem is
1. Find an anagram of B: eg. mary --> anagram is army
2. Then find a substring of the anagram in A
--> find whether army in A or not

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

@Above all: Could u please explain your logics??

- Boom April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this question a variation of sliding window which has all characters of B in A? If we can have unwanted characters in the window, i.e. the window size is > size of B then it's same as the sliding window problem.

Otherwise, we can have an additional check to ensure the window size = size of B.

- Bumblebee April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

put the string B into a hash table.
Now slide through the array A and check for each character to be present/absent ffrom the hash table. If present continue to next character and if absent start again from next character....thats linear.

IS IT CORRECT?

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

string A="hiiamam"
string B="army"
ur solution will return positive for amam(4 times in hash)
vs army (4 times in hash)

- Don April 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it depends on how u build the hash function...to check for anagrams ur string B must be sorted first and then any hash function would work...similarly in a window of length(B) in string A, first sort the window and then compute the hash function.

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

You can come with hash function such that anagrams will have same hashcode.

e.g. String contains only ascii characters then hash function could be adding ascii values of all the characters. Now pickup first 'm' characters from A and then compute hash function.
1. If hashcode matches with that of B, sort the substring (window of length B) and compare with sorted version
This saves you sorting each window of length (B).
2. If hashcode does not match you can compute next hashcode very easily, just substract ascii value of the first character in the window and add ascii value for the next character.

worst case running time O( n + ( n log m))
1. Each window can be sorted in O( m log m) time.
2. There would be n/m such windows. Hence total time would be O(n/m * m log m) = O( n log m)
3. Total time to traverse entire string A is O(n)

- AlertCoder February 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo:
1. Sort B
2. maintain a window of length m and slide it along A
3. for each window do:
3.1 Sort the window O(mlogm)
3.2 Compare the sorted window with sorted B
3.3 If equal anagram found

T(n) = O(n*mlogm) but since m<<n T(n)~O(n)

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

Nice approach
Using extra space it can be done without altering the input strings.
One Optimization to yr solution is that once we sorted the window of size "m" first time, after that no need to sort,we can call min heapify for the next entry.
so the time is (n-1)logm + mlogm = (n+m-1)logm instead of nmlogm.

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

i dont get the min heapify thing.please elaborate a bit.
and how this comes out to be (n-1)logm

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

@Tulley: mind to clarify your idea?

Complexity could be reduced to O(nm) easily by using the fact that when the window moves we can maintain the sorted window in O(m) complexity each time (1 item is dropped out window, and 1 is added).

- anon April 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how?suppose B="raam",A="ramaabcdefg"
now sorted B="aarm"
and sorted A for length 4 is aarm so both matches but no substring in A is aarm.plz let me know if i misunderstood the question

- aryan.bit April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 votes

its aamr

- aryan.bit April 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suggesting Approach to Maintaining count of chars:

create hash-table for each char in m str
if m = "ajsaask"
h["a"] = 3, h["j"] = 1, a["s"] = 2, a["k"] = 1

now for each window
check for the count for each same size window (size - m = 7)

- Rahul D May 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since m is very small, we can calculate the anagrams and hash them.

We could then slide the window and hash each time. If the hashes match, it's a substring

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

Step 1: Since m is small, find the sum, call it SumB.
Step 2: Take first m elements from Array A, find sum, call it SumA
Step 3: Compare SumA and SumB. If equal, check if substring from A is an anagram of B.
Step 4: Otherwise slide window by 1 char, and repeat Step 2.

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

We can solve this in O(n). Use sliding window algorithm, check if the window size is same as the size of (b).

- RiTZ April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
public class AnagramSubString {
public static void containsAnagram(char a[], char b[]){
int m = b.length;
int n = a.length;
Arrays.sort(b);
for(int i = 0; i < n-m; i++){
char window[] = Arrays.copyOfRange(a, i, i+m);
Arrays.sort(window);
if(Arrays.equals(window, b)){
System.out.println("SubString Anagram at index "+i);
return;
}
}
System.out.println("Not Found");
}

public static void main(String args[]){

containsAnagram("123cabdnj265kml".toCharArray(), "abcdjn".toCharArray());
}
}

- Badal November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;

public class AnagramSubstring {

static void initializeHashMap(HashMap<Character,Integer> strMap,String str2)
{
for(int k=0;k<str2.length();k++)
{
if(strMap.containsKey(str2.charAt(k)))
{
int temp = strMap.get(str2.charAt(k));
temp++;
strMap.put(str2.charAt(k), temp);
}
else
{
strMap.put(str2.charAt(k), 1);
}
}
}
static boolean checkAnagramSubstring(String str1,String str2)
{
HashMap<Character,Integer> strMap = new HashMap<Character,Integer>();

int str2_index=0,start=0;
for(int str1_index=0;str1_index<str1.length();str1_index++)
{
if(strMap.containsKey(str1.charAt(str1_index))&& strMap.get(str1.charAt(str1_index))>0)
{
int temp = strMap.get(str1.charAt(str1_index));
temp--;
strMap.put(str1.charAt(str1_index), temp);

if(str2_index==0)
{
start=str1_index;
}
str2_index++;
if(str2_index==str2.length())
{
return true;
}
}
else
{ if(str2_index!=0)
{
str1_index=start;
}
str2_index=0;
initializeHashMap(strMap,str2);
}
}

return false;
}
public static void main(String[] args) {
String str1="hellowoorldhowareyou";
String str2="oolrdw";
if(checkAnagramSubstring(str1,str2))
{
System.out.println("Present");
}
else
{
System.out.println("Not Present");
}
}

}

- amu December 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1) Store all characters and their counts in string B into a hashtable.
(2) Set numZeros to 0 initially
(3) Iterate through the first |B| characters of A. For each character, check if it's in the hashtable. If not, ignore it. If yes, decrement the count. If the count is 0, increment numZeros. If numZeroes == #keys in the hashtable, then we have found a substring that's an anagram of B.
(4) For each subsequent character in A, do the same as in (3) above. Also, we need to "subtract" the character that no longer belongs in the current substring. Again, check if that character is in the hashtable. If not, ignore it. If yes, increment the count. If the count is now 1, which means it's previously 0, decrement numZeros. Repeat step (4) until we reached end of array or have numZeros == #keys in the hashtable.

- Sunny January 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use rolling hash technique. First compute the hash of the small technique such that it same anagram would give same hash in a hashing algorithm. Such hashing may be simply multiplying the integer representation of each character. Then use rolling hash to find the substring.

- Riyad Parvez July 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unordered_map<char, int> mb;
unordered_map<char, int> ma;
int window_size(b.size()); 
for (int i=0; i < b.size(); i++)
                mb[b[i]]++;
int cnt(0);
for (int i=0; i < b.size(); i++)
              if (m[a[i]]) { ma[a[i]]++; cnt++; }
for (int i=0; i <= a.size()-b.size(); i++)
{
         if (cnt == b.size()) return true;
         else {
                       if (ma[a[i]]) { ma[a[i]]--; cnt--; } 
                       if (i+b.size() < a.size()) {
                                 if (mb[a[i+b.size()]]) { ma[a[i+b.size()]]++; cnt++; } 
                       }
         }
         return false;
}

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

unordered_map<char, int> mb;
unordered_map<char, int> ma;
int window_size(b.size()); 
for (int i=0; i < b.size(); i++)
                mb[b[i]]++;
int cnt(0);
for (int i=0; i < b.size(); i++)
              if (m[a[i]]) { ma[a[i]]++; cnt++; }
for (int i=0; i <= a.size()-b.size(); i++)
{
         if (cnt == b.size()) return true;
         else {
                       if (ma[a[i]]) { ma[a[i]]--; cnt--; } 
                       if (i+b.size() < a.size()) {
                                 if (mb[a[i+b.size()]]) { ma[a[i+b.size()]]++; cnt++; } 
                       }
         }
         return false;
}

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

question checks if you sliding window concept similar to rabin-karp string matching algorithm. Its particularly very useful if you window size

//  a is the larger string, b is smaller and we need to check if anagram(b) exists as some substring. In anagram questions, think terms of matching the hash-tables.
bool isAnagaramExist(string& a, string& b)
{
unordered_set<char> sb;
unordered_map<char, int> ma;
int n(a.size()), m(b.size()), cnt(0);
for (int i =0 ; i < m; i++)
               sb.insert(b[i]): 
for (int i=0; i < m; i++)
       if (sb.find(a[i]) != sb.end()) { ma[a[i]]++; cnt++; } 
for (int s=0; s <= n-m; i++)
{
            if (cnt == m && ma.size() == sb.size()) return true ; // hash-tables are same 
            if (sb.find(a[i]) != sb.end) {
                       cnt--;
                       if (--ma[a[s]] == 0)
                                  ma.erase(a[s]);  // if its becomes zero, remove from the hash-table maintained by sliding string A. 
            } 
            if (s+m < n && sb.find(a[s+m]) != sb.end()) {
                        ma[a[s+m]]++;
                        cnt++;
            }
        return false;
}
}

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

public static boolean ana_check(String a, String b)
	{
	    int n=a.length();
	    int m=b.length();
	    boolean k;
	    for(int i=0;i<=(n-m);i++){
	        k= anagram((a.substring(i,i+m)),b);
	        if(k)
	        	return true;
	}
		return false;
	}
	public static boolean anagram(String s, String t) {
	        // Strings of unequal lengths can't be anagrams
	        if(s.length() != t.length()) {
	            return false;
	        }

	        // They're anagrams if both produce the same 'frequency map'
	        return frequencyMap(s).equals(frequencyMap(t));
	    }

	    private static Map<Character, Integer> frequencyMap(String str) {
	        Map<Character, Integer> map = new HashMap<Character, Integer>();
	        for(char c : str.toLowerCase().toCharArray()) {
	            Integer frequency = map.get(c);
	            map.put(c, frequency == null ? 1 : frequency+1);
	        }
	        return map;
	    }

This solution runs at O(n*m) however as its is explicitly stated m<<n then the runtime is O(n).

- Abhiroop Sarkar December 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Modified Rabin Karp, we can solve this problem in O(n)
Algorithm :
1. Calculate the sum of the characters (rolling hash) of string B = O(m)
2. Calculate rolling hash for the 1st window of size m for string A = O(m)
3. Progressively, calculate the rolling hash from m+1 till n-1 for string A = O(n-m)
4. At any point, if the rolling hash of string B matches the rolling hash of window under test, then we have found an anagram

Time Complexity = O(m+m+n-m) = O(n+m) = O(n) since n>>m
Space Complexity = O(1)

- SlickHackz December 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Find out all the anagrams and check array A for this substring. Checking one combination takes O(n). Since, number of anagrams rates to be not comparable to n, the complexity is O(n).

- Code Saviour April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I doubt this guy has any idea about time complexity at all!

- so funny April 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sort both the arrays and apply KMP

- Geek January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@so funny: Read the question again.

m is much much much smaller compared to n. This is a perfectly valid solution.

- Anonymous August 19, 2013 | 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