Bloomberg LP Interview Question for Software Engineer / Developers






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

void chareq(char *s1, char *s2)
{
  int alph[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  int n1=0,n2=0;
  int i;
  for(i=0;i<strlen(s1);++i)
    ++alph[s1[i]-'a'];
  for(i=0;i<strlen(s1);++i)
  {
    if(s1[i]==s2[i]) { --alph[s1[i]-'a'];++n1;}
  }
  for(i=0;i<strlen(s2);++i)
  {
    if(alph[s2[i]-'a']!=0 ){ --alph[s2[i]-'a'];++n2;}
  }
  printf("%d %d\n",n1,n2);
}

- codeyman February 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Removed the redundant loop..

void chareq(char *s1, char *s2)
{
  int alph[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  int n1=0,n2=0;
  int i;

  for(i=0;i<strlen(s1);++i)
  {
    if(s1[i]==s2[i])  ++n1;
    else ++alph[s1[i]-'a'];
  }
  for(i=0;i<strlen(s2);++i)
  {
    if(alph[s2[i]-'a']!=0 ){   --alph[s2[i]-'a'];++n2;}
  }
  printf("%d %d\n",n1,n2);
}

- codeyman February 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think one more check is needed
with if(alph[s2[i]-'a']!=0 && Guess?????????)

- Ravi Kant Pandey May 28, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

I know a modern 'smart' compiler would automatically avoid this problem, but for an interview it may matter.

for(i=0;i<strlen(s1);++i)

strlen takes O(n)
The for loop also takes O(n)
If strlen is computed for each comparison of the for loop, then the solution has become O(n^2) instead of O(n)

And the simple fix:

int length = strlen(s1);
for(i=0;i<length;++i)

- edwardotis October 23, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void scan(string s1,string s2)
{
map<char, int> a;
int i1=0,i2=0;
int i=0;
for(;i<s2.size();i++)
a[s2[i]]+=1;
for(i=0;i<s1.size();i++)
{
if(i<s2.size()&&s1[i]==s2[i])
i1++;
if(i<s2.size()&&s1[i]!=s2[i])
if(a[s1[i]]>0)
i2++;

}
cout<<i1<<i2;
}

- J.C. April 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

I like the hashmap solution because it can handle lowercase and uppercase ANSI letters or any other characters. The present alph[] solution would attempt to index the array out of bounds on uppercase letters.

(I think this would be a question to ask the interviewer while you design the solution. Q. Should the solution be case sensitive or not?)

There was a bug in the hashmap solution. It kept counting all instances of a mismatched letter in string2, even when there were no more corresponding mismatched instances of the letter in string1.

It failed on this test by counting both b's in string2:
scan( "aba", "bab" );
Expected 0 2
Returned 0 3

Corrected solution below:
--a[ s1[i] ];//decrement used chars in string 1

void scan(string s1,string s2)
{
	map<char, int> a;
	int i1=0,i2=0;
	int i=0;
	for(;i<s2.size();i++)
		a[s2[i]]+=1;

	for(i=0;i<s1.size();i++)
	{
		if(i<s2.size()&&s1[i]==s2[i])
			i1++;

		if(i<s2.size()&&s1[i]!=s2[i])
			if(a[s1[i]]>0)
			{
				i2++;
				--a[ s1[i] ];//bug fix
			}
	}

	std::cout << s1 << " " << s2 << " -> " << i1 << " " << i2 << "\n";
}

- edwardotis October 23, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

above code gives a wrong result with the input s1="bab", s2="abc"

The result should be 0, 2 but it gives 0, 3.

- owl93 February 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We use hashmaps

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

use merge sort at level one for n1

i=0;j=0;

for k = p to r: p and r are start and end of array both combined
if L[i] == R[j]
count ++
else
i++ , j++

for n2 do same after sorting both arrays

- ani August 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for the alph[] solution.

Good Solution , easy to follow.
If we consider the best case scenario
then We can have an optimization.
do a check before the 2nd for loop
if n1 == max(strlen(s1), strlen(s2))
then no need to do the second for loop.

- anon October 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree I more check is required.
The codeymon code assumes that both strings contain the same chars , may be not in order.
But there needs to be a check in the second for before incrementing n2,if the char in s2 is present somewhere in s1.
If the char isnt present in s1 then n2 should not be incremented.

- anon October 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wicked Technique!

- Ronin February 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This one works.

void stringMatch(char *str1, char *str2, int *n1, int *n2)
{
int charSet[256];
memset(charSet, 0, sizeof(charSet));
int len1 = strlen(str1);
int len2 = strlen(str2);
int i;
for (i = 0; i < len2; i++)
charSet[str2[i]] = 1;
for (i = 0; i < len1; i++)
{
if (i<len2 && str1[i] == str2[i])
(*n1)++;
else if (i<len2 && str1[i] != str2[i])
{
if (charSet[str1[i]])
(*n2)++;
charSet[str1[i]] = 0;
}
}
}

- Ying February 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n2 is wrong. Here is the code that works. (Ref. WIKI: longest common substring)

int stringMatch(char *s1, char *s2, int *n1, int *n2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int i,j;
char* pre=(char*)malloc (len2*sizeof(char));
char* cur=(char*)malloc (len2*sizeof(char));
char* swap;
if(!pre || !cur) {printf("Memory allocation error.\n"); return -1;}

memset(pre, 0, sizeof(pre));
memset(cur,0,sizeof(cur));
*n1=*n2=0;

for(i=0;i<len1;i++)
{
if(s1[i]==s2[i])
(*n1)++;
for(j=0;j<len2;j++)
{
if(s1[i]!=s2[j])
{
cur[j]=0;
}
else
{
if(i==0 || j==0)
cur[j]=1;
else
cur[j]=1+pre[j-1];
}
if(*n2<cur[j])
*n2=cur[j];
}
swap=cur;
cur=pre;
pre=swap;
}
free(pre);
free(cur);
return 0;
}

- will October 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<int> char_match(const string s1, const string s2){
unordered_map<char, int> ch2int1, ch2int2;
int k(0);

for (int i = 0; i!= s1.size(); i++) {
ch2int1[s1[i] ]++;
if (s1[i] == s2[i] && i < s2.size()) {
k++;
}
}

for (int i = 0; i!= s2.size(); i++) {
ch2int2[s2[i] ]++;
}

int t(0);
for (unordered_map<char, int>::iterator miter = ch2int1.begin(); miter != ch2int1.end(); miter++) {
if (miter->second > ch2int2[miter->first]) {
t += ch2int2[miter->first];
} else{
t+= miter->second;
}

}
vector<int> ivec;
ivec.push_back(k);
ivec.push_back(t-k);
return ivec;
}

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

vector<int> char_match(const string s1, const string s2){
unordered_map<char, int> ch2int1, ch2int2;
int k(0);

for (int i = 0; i!= s1.size(); i++) {
ch2int1[s1[i] ]++;
if (s1[i] == s2[i] && i < s2.size()) {
k++;
}
}

for (int i = 0; i!= s2.size(); i++) {
ch2int2[s2[i] ]++;
}

int t(0);
for (unordered_map<char, int>::iterator miter = ch2int1.begin(); miter != ch2int1.end(); miter++) {
if (miter->second > ch2int2[miter->first]) {
t += ch2int2[miter->first];
} else{
t+= miter->second;
}

}
vector<int> ivec;
ivec.push_back(k);
ivec.push_back(t-k);
return ivec;
}

- Anonymous December 02, 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