## Microsoft Interview Question for Software Engineer / Developers

• 0

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

The conversion is done in the following way:
1. first the sign is checked. the first char says if the number is negative or possitive
2. we left with 7 chars. we take 6 times 5 bit and than the last 2 bit (or one bit in case of negative number) and code them ibn the following way:
if the 5 bits value is 0 than the char will be "0" when represented on 7 bits because the ascii value is 48 and does not need more than 7 bits. correpondingly for 1..9, than when the value of 5 bits is 10 we will will assign "a" for the char. possible, cause the ascii value is 97 and does not need more than 7 bits...continue till 2^5-1 = 31 which will be coded to "v"

Please, let me know if something is wrong with that code
Eila:)

func
{
// first str is for the sign
int strtIdx;
if (number < 0)
{
strtIdx = 30
str='0'
}
else
{
strtIdx = 31
str='0'
}

// get the number binary representation
binNum = ConvertNumToBinary(number)

// assigning the 7 left str's
for (int i=1; i<8; i++)
{
str[i] = convert5bitsToCode(strtIdx,binNum);
} // for
} // func
char convert5bitsToCode(int idx, binNum)
{
//take 5 bits from index. for ex. 1st time 31..27
// in case of positive
int code = get5Bits(idx, binNum);
switch code
{
0: return '0'; break;
1: return '0'; break;
....
9: return '9'; break;
10: return 'a'; break;
11: return 'b'; break;
...
31: return 'u'; break;
else: error
}//switch
} // convert5bitsToCode

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

Did not understand entirely what the question wants?

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

convert 32 bit number to Hexadecimal.

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

I'm assuming a number system with base 36, 'coz I've 36 distinct characters (0-9, a-z) to represent the numbers from 0-35.
Also to accomodate positive, negative and null I'll use left most 3 characters (I'll explain them later) thus I'm left with 8-3=5 characters to present all 32bit integers. Before I explain more lets see few examples:
-------------------------------------------------------------------
Decimal Numbers = String Representation
-------------------------------------------------------------------
0 0000 0000
1 0000 0001
.. ...
9 0000 0009
10 0000 000a
11 0000 000b
... ...
35 0000 000z
36 0000 0010 = (36^1 *1 + 36^0 *0)
....
99 0000 002r = (36^1 *2 + 36^0 *27)
and so on.

Now to accomodate negative numbers I assume leftmost 2 characters will be 10
, in order to avoid ambiguity, I aslo assume leftmost character is zero if its a positive integer.
Example: decimal (-99) <=> new system (1000 002r)

I couldn't understand the rquirement about null from this question. Still to
represent a null value for an integer I can assume left most three characters will be 110. This is why I mentioned earlier that I'm left with 5 characters to represent the absolute value of the intergers with 32 bits.

Note: ascii / unicode values for 0-9, a-z are same and they occupy contigious locations in the ascii table. Thus we can use a transformation to make them
represent intended integer value. For example
int_value_in_my_system (char x) = ascii_value(char x) - ascii_value(zero)
*********************************************************************
Eila - can u explain in more detail with some examples may be:-)

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

32 bits = 4 bytes. 8 char string = 8 bytes. What is the advantage of doing this?

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

I think that solution should be somthing like this.

``````void Encode(char* buf, unsigned int num){
int i=0;
while(num){
buf[i++]=get_letter(num%36);
num/=36;
}
buf[i]=0;
reverse(buf, buf+i);
}

char get_letter(int val){
if(val<=9)
return '0'+val;
return 'a'+val;
}``````

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

I think (including the null character, so 7 bits of data) means the string is ending with '\0', so u have only 7 bytes to use(I think 7 bits is a typo).
every 5 bits give 2^5=32, we have 36 different charactors to indicate it.

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

6 chars are available in case of signed numbers (1 char for sign and 1 for '\0')
base 36 number is sufficient as 2^31=2147483648, 36^6=2176782336. If the number is unsigned also it works 2^32=4294967295, 36^7=78364164096

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

``````//replace every space with "%20" with unlimited memory
public static String convert1(String s){
String news="";
int len=s.length();
for(int i=0;i<len;i++){
if(s.charAt(i)==' '){
news+="%20";
}
else
news+=s.charAt(i);
}
return news;
}
//replace contiguous spaces with only one "%20" with unlimited memory
public static String convert2(String s){
String news="";
int len=s.length();
int count=0;
for(int i=0;i<len;i++){
while(i<len&&s.charAt(i)==' '){
count++;
i++;
}
if(count!=0){
news+="%20";
count=0;
i--;
}
else{
news+=s.charAt(i);
}
}
return news;
}``````

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

Please ignore the previous post. I posted it to the wrong problem.

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

for unsigned:
2^32 = 4 294 967 296 (range: from 0 to 2^32-1), 35^7 = 64 339 296 875. So use 35 num+letters and 7 characters. Another character for '\0'
for signed:
(range: from -2^(n-1) to 2^(n-1)-1
1 character for indicating positive or negative, 1 character for '\0'
2^31 = 2 147 483 648
because 34^6=1 544 804 416, not enough to cover the range. so use 36^6= 2 176 782 336, enough to cover the range, note to indicate a letter can have to meanings: when it is in the beginning, representing positive or negative but when it is not in the beginning, representing a numeric value.

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.

### 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.