Bloomberg LP Interview Question
Financial Software Developersvoid cipher(char* str)
{
unsigned int table[26] = {0};
const int len = strlen(str);
const unsigned char a = char('a');
const unsigned char e = char('e');
for (int i = 0; i < len; i++)
{
int val = str[i] - a;
table[val] += 1;
}
const ptrdiff_t off = max_element(table, table+26) - table ;
const size_t pos = e - a;
int distance = 0;
if ( off == pos)
;
else if ( off < pos)
distance = pos - off;
else
distance = pos - off + 26;
for (int i = 0; i < len; ++i)
{
str[i] = (((str[i] - a) + distance ) % 26) + a;
}
}
#include <stdio.h>
int main()
{
char str[] = "bloomberg";
int arr[26];
char *start = str;
char temp;
int i, max = 0;
int difffrome;
char tempChar;
while(*start)
{
temp = *start++;
arr[temp-'a']++;
}
//max char
for(i=1; i<26; i++)
{
if(arr[i]>arr[max])
max = i;
}
difffrome = 'e'-('a'+max);
start = str;
while(*start)
{
tempChar = *start+difffrome;
if(tempChar > 'z')
{
int diff = tempChar - 'z';
*start++ = 'a' + diff;
}
else if(tempChar < 'a')
{
int diff = 'a' - tempChar;
*start++ = 'z' - diff;
}
else
*start++ = tempChar;
}
}
void cipher( char *str )
{
char *start = str ;
int charArr[26] = {0} ;
int val ;
int i, maxIndex, max ;
int shiftDis ;
// Find the highest occruing char
while( *start )
{
val = *start - 'a' ; printf( " val is %d\n", val) ;
charArr[val] += 1 ;
*start++ ;
}
max = 0;
maxIndex = 0 ;
for( i = 0 ; i<26; i++ )
{
if ( charArr[i] > max )
{
max = charArr[i] ;
maxIndex = i ;
}
}
//Calculate the offset
shiftDis = 'e' - ( 'a' + maxIndex ) ;
start = str ;
while( *start )
{
val = *start - 'a' + shiftDis ;
if ( val >=0 ) *start = 'a' + val % 26 ;
else *start = 'a' + val + 26 ;
*start++ ;
}
}
#include<stdio.h>
int main(){
char* str = "bloomberg";
int i;
char maxChar;
int freq[26];
for(i=0; str[i] != '\0'; i++){
freq[str[i] - 'a']++;
}
int max = 0;
for(i=0; i<26; i++){
if(freq[i] > max) {
max = freq[i];
maxChar = 'a' + freq[i];
}
}
for(i=0; str[i] != '\0'; i++)
{
printf("%c", ((str[i] - maxChar + 'e' - 1) % 'z'));
}
return 0;
}
- getjar.com/todotasklist my android app December 10, 2011