Bloomberg LP Interview Question for Financial Software Developers






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

Establish a hash. The complexity is O(n).
Int findFirstNonrepeatedChar(char* str)
{
If(str == NULL) return -1;
Int hash[MAX_CHAR];
Int I;
For(i=0; str[i]!=’\0’;i++)
{
    Hash[str[i]]++;
}
For(i=0;str[i]!=’\0’;i++)
{
    If(hash[str[i]] == 1)
    {
        Return str[i];
    }
}
            }

- chenming831 June 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cheming831, your algorithm will not work in the following case:
daccbb

It will return 'a', but the correct answer is 'd'.

- orangetime23 May 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cheming831, sorry, it will work, I misunderstood it.

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

can u explain a little?
a number in array of char ?

- bala June 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sry, my bad. I was thinking all the chars could be represented with their acs numbers, therefore I used number.

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

Find the first unique char in the given array of char. e.g. abccdee. Ans c not e

- mast June 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, plz ignore the prev msg. my bad. I had different question in mind.
Find the first repetitive char in the given array of char. e.g. abccdee. Ans c not e

- mast June 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <stdio.h>

main()
{
char str[]= "abccdedd";
int i;

i=1;
while(str[i] ^ str[i-1]) i++;
printf("First repeatitive char: %c\n", str[i]);
}

Using ^ (XOR) operation rather than '==' comparison as it will give a better performance.

- nitin June 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nitin It doesn't work. It only returns the first charecter which appears adjacently more than once.

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

first non-reapeated character in a char array.
e.g. eabeccdebfa
Ans : d

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

If you can use hash_set, insert each char as you enumerate through the array and erase it as it appears more than once(if it exists in the hash_set). After one iteration the 1st in hash_set is your answer. If you can't use hash_set brute force takes O(n^2)

- Yi June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you mean that when you encounter 'b' once you add it to the hashset, and when you encounter it again as you go through the array you remove it? what if there are 3 'b's? won't that just add it again to the hashset even though it's not unique?

if space was not a problem you could have an int array the size of all ascii characters (128?), as you go through the array (N), you increment how many times characters appear ('a' is at index 97, 'b' at 98 etc) - then you go through your ascii array once (128) and return the first index that is := 1. complexity N + 128?

- heretic June 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not give me the first unique character in the word, it will give me the first unique alphabet.

Eg if we have awddaace , your algo will give me c but the first unique is w

- Sharjeel June 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

indeed you are correct! thanks for reading through.

I guess what you could do instead of going through the ascii array, is go through the word array taking each letter in turn then checking the ascii array to see how many times the letter comes up, this should give you w as the first unique letter.

- heretic June 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a simpler solution

void firstunique(char *arr, int size)
{
for (int i = 0;i<size;i++)
{
if(charscount(arr,size,arr[i])==1)
cout<<"\nFirst unique char is :"<<arr[i]<<endl;
}
}

int charscount(char *arr, int size, char tofind)
{
int count=0;
for(int m = 0; m<size; m++)
{
if (arr[m] == tofind)
count++;
}
return count;
}

- Waqas Ahmed June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

fjj

- Anonymous June 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey72808" class="run-this">void firstunique(char *arr, int size)
{
for (int i = 0;i<size;i++)
{
if(charscount(arr,size,arr[i])==1)
cout<<"\nFirst unique char is :"<<arr[i]<<endl;
}
}

int charscount(char *arr, int size, char tofind)
{
int count=0;
for(int m = 0; m<size; m++)
{
if (arr[m] == tofind)
count++;
}
return count;
}</pre><pre title="CodeMonkey72808" input="yes">char str_temp[] = "abcdefabcdegf";

firstunique(str_temp, sizeof(str_temp)-1);
</pre>

- wahmed June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My answer is to use Hashmap with Char as key and int as value. Linkedset could be a good option than Hashmap.

- Karthik December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//tested code
int main ()
{
int a [26][2]; // [26] - possible_alpahabets
// [first index] - occurence count;
// [second index] - nth character in array.

//set the elements to 0;
for (int row= 0; row < 26; row++)
{
for (int col =0 ; col < 2; col++)
{
a [row] [col] = 0;
}
}


char str [] = {"eabexccdebfa"}; // 'x' should be the ans. we have 'd' and 'f' but not first of unique.
int length = (int) strlen (str);


//find the ascii value of every char

for (int iter = 0; iter < length ; iter++)
{
int ch = str [iter];
//to make the value fall within array of 26 characters.
int val = ch - 97; //97 - ascii value of 'a'.
a [val][0] += 1; //incrementing count of every character
a [val][1] = iter; //registering the nth character.
}

int asc = length;
char mych = '\0';
for (int two = 0; two < 26;two++)
{
//eliminating characters that has occurred more than once.
if (a [two][0] == 1 )
{
//we are looking for first unique value.
//lowest of the nth character registered using second index.
if ( asc > a[two][1])
{
asc = a[two][1];
mych = str [asc];
}
}
}
if (asc != length) //handling if there were no unique characters.
cout << mych << endl;
return 0;
}

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

My solution:
#include <iostream>
#include <string.h>
using namespace std;
typedef struct{
int time;
int pos;
}Htable;

int
FindUniqueChar(char Array[]){
Htable _hTable[256];
for(int i=0; i < 256; i++){
_hTable[i].time = 0;
_hTable[i].pos =-1;
}
for(unsigned i=0; i< strlen(Array); i++){
unsigned pos = static_cast<unsigned> (Array[i]);
if(_hTable[pos].pos< 0){
_hTable[pos].pos = i;
}
_hTable[pos].time ++;
}
int min_pos = -1;
for(int i=0; i < 256; i++){
if(_hTable[i].time == 1){
if(min_pos == -1){
min_pos = i;
}
else{
if(_hTable[i].pos < _hTable[min_pos].pos){
min_pos = i;
}
}
}
}
if(min_pos== -1){
return -1;
}
else{
return _hTable[min_pos].pos;
}
}

int
main(){
char A[] = "345483";
int pos = FindUniqueChar(A);
cout<<pos<<endl;
return 0;
}

- Linkan Chen (Seeking for Full-time Software 917-362-0023) October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have passed VS test

- Linkan Chen (Seeking for Full-time Software Position 917-362-0023) October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct htable 
{
       int pos;
       int count;
};

int main()
{
    struct htable hash[26];
    struct htable ans;
    
    int i = 0;
    char str[] = "abcdefghbcdef";    
    int pos = 500000; //some large number
    
    for (i=0; i< 26; i++)
    {
        memset(&hash[i],0, sizeof(struct htable));
    }
    
    for(i=0; i < sizeof(str); i++)
    {
           if (!hash[(int)(str[i] - 'a')].pos)
           {
                                  hash[(int)(str[i]-'a')].pos = i;
                                  hash[(int)(str[i] - 'a')].count++;
                                  continue;
           }
           
           else hash[(int)(str[i] - 'a')].count++;
           
    }  
    
    
    for (i = 0; i<26; i++)
    {
          if( hash[i].count == 1) 
          if (ans.pos > hash[i].pos) ans.pos = hash[i].pos;
          continue;
        
    }
    
    printf("%c", str[ans.pos]);
    
    
    getch();

}

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

I came across a similar question to this one - it used integers instead of chars but the logic works just the same. I wrote my code in Java but you can port it easily enough.

public static char findFirstUnique(char[] sequence)
	{
		HashMap<String, Integer> map = new HashMap<String,Integer>();
		for(char letter : sequence)
		{
		if(map.containsKey(String.valueOf(letter)))
				map.put(String.valueOf(letter),map.get(String.valueOf(letter))+1);
		else
			map.put(String.valueOf(letter), 1);
		}
		for( char letter: sequence)
		{
			
			if(map.get(String.valueOf(letter))==1)
				return letter;
			
		}
		return '-';
	}

Code is tested and works had to use String because Maps in Java don't allow for primitive types.

- Connor January 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

FYI, you can do HashMap<Character, Integer>();
Character is the object wrapper to char primitive types.

- AlexKYY October 09, 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