Intel Interview Question






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

sort them and then compare.

- lingrong April 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;

   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }

   return 1;
}

- Anonymous June 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_anagrams(char * s1, char * s2)
{
if (s1 == NULL || s2 == NULL) {
return false;
}
map<char, int> count;
while(*s1 != '\0' && *s2 != '\0') {
count[*s1++]++;
count[*s2++]--;
}
if (*s1 != '\0' || *s2 != '\0') {
return false;
}
map<char, int>::iterator iter = count.begin();
for(; iter != count.end(); iter++) {
if (iter->second != 0) {
return false;
}
}
return true;
}

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

bool is_anagrams(char * s1, char * s2)
{
if (s1 == NULL || s2 == NULL)
{
return false;
}

typedef map<char, int> CharMap;

CharMap count;

int nLen1 = 0;
while (*s1 != '\0')
{
if (*s1!=' '/* or unwanted symbol like '''*/)
{
CharMap::iterator iter1 = count.find(*s1);
if (iter1 != count.end())
iter1->second++;
else
count[*s1] = 1;

nLen1++;
}

s1++;
}

int nLen2 = 0;
while (*s2 != '\0')
{
if (*s2!=' ' /* or unwanted symbol like '''*/)
{
CharMap::iterator iter2 = count.find(*s2);
if (iter2 != count.end())
iter2->second--;
else
return false;

nLen2++;
}

s2++;
}

if (nLen1 != nLen2)
return false;

CharMap::iterator iter = count.begin();
for(; iter != count.end(); iter++)
{
if (iter->second != 0)
{
return false;
}
}

return true;
}

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

#include<stdio.h>
#include<string.h>

int main()
{
char s1[] = "lease";
char s2[] = "easle";
int bitmap[256] = {0};
int len1, len2, i=0;
len1 = strlen(s1);
len2 = strlen(s2);
if(len1 != len2)
{
printf("Not anagrams");
return 0;
}
while(s1[i])
{
bitmap[s1[i]]++;
i++;
}

i =0;
while(s2[i])
{
if((bitmap[s2[i]]) == 0)
{
printf("Not anagram");
return 0;
}
else
bitmap[s2[i]]--;
i++;
}
printf("They are anagrams");
return 1;
}

- Anonymous May 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perfect logic @lingrong

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

Is that sarcasm? Cause you know it's not right...
s1 = "abc"
s2 = "bac"
sort each and compare them any way you want and it's not correct.

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

oh snap... it IS right. Fail on my part.

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

I am thinking of logic add the ascii value of two strings if both are equal print its anagram else not. note: convert all to char to lower case and one more is leave any special char in the middle dont add that.

- Ramachandran May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
class Anagram{
	public static void main(String arg[]){
		String s1= "laid";
		String s2 = "dial";
		int[] arr = new int[26];
		char[] c1 = s1.toCharArray();
		char[] c2 = s2.toCharArray();
		for(int i=0;i<s1.length();i++){
			arr[s1.indexOf(c1[i])]++;
		}
		for(int i=0;i<s2.length();i++){
			arr[s2.indexOf(c1[i])]--;	
		}
		for(int i=0;i<26;i++){
			if(arr[i]!=0){
				System.out.println("Not an Anagram "+i);		
				return;
			}
		}
		System.out.println("Anagram");
	}
}

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

To check whether the two strings are anagrams,there is no need to sort or anything,It can be done in order of 1.Just XOR all the chars present in both the strings.If it is zero,then both are anagrams ...else not....simple...

- uday August 30, 2013 | 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