Bloomberg LP Interview Question for Software Engineer / Developers






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

Also for the smart dude upstairs:

My computer is 1yr old, and has Windows Vista, 2GB RAM,

NO STONE AGES!!!!

- Raul Gonzales March 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

VS2008 Microsoft compiler give:

Sizeof(a) = 2
Sizeof(b) = 10
Sizeof(c) = 4
Sizeof(d) = 4

- elligno May 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

IF initialised string is empty it seems size should be 1 and if it has a space it should be 2
Please correct me if i am wrong.

- hary February 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually I was quite surprised at the answer I got for this one. I would have thought that the value returned would be the size of (char *) the pointer. However, I compiled the following program:

#include <stdio.h>

main () {
char a[] = " ";
char b[10];

printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
}

The resulting output was as follows:

Sizeof(a) = 2
Sizeof(b) = 10


What can I say but... go figure

- Wandering programmer February 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for array a, it contains two char, one is null , the other is '\0'
for array b, it only has ten characters' memory space.

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

The question was not:
How many characters are in the array?
it was:
What will sizeof(<array name>)?

In the example program I gave, sizeof(a) returns the value 2 while sizeof(b) returns the value 10

- Wandering programmer March 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each char is 1 byte
so the first one is 2 byte; the second one is 10 byte

the trick here is you should know the first array allocates two characters space

- Anonymous March 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dfmcla...
if we define char a[], then 'a' would store the address of the string....but 'a' itself is an array...hence, sizeof(a) would give size of this array..
now if we define char* a=new char[10], then, sizeof(a) would give size of a char pointer(same size as int and float pointer)...coz here 'a' is a char pointer....

- Neerav March 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dfmcla...
if we define char a[], then 'a' would store the address of the string....but 'a' itself is an array...hence, sizeof(a) would give size of this array..
now if we define char* a=new char[10], then, sizeof(a) would give size of a char pointer(same size as int and float pointer)...coz here 'a' is a char pointer....

- Neerav March 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sizeof(a) will be 4 bytes i.e size of pointer.

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

this ans is wrong. sizeof(a) is 1 ... note that 'a' is a character array, not character pointer.

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

Okay I think only pointer will be returning the sizeof(char *). Arrays will be just returning the size of the array. check the below code.

#include <stdio.h>

main () {
char a[] = " ";
char b[10];
char *c;
char *d = b;

printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
printf ("Sizeof(c) = %d\n", sizeof(c) );
printf ("Sizeof(d) = %d\n", sizeof(d) );
}

Sizeof(a) = 2
Sizeof(b) = 10
Sizeof(c) = 4
Sizeof(d) = 4

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

The length of array is calculated as sizeof(a)/sizeof(a[0]);

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

char a[] = " "; // note there is a space between the double quotes
is interpreted by the compiler to be
char a[] = {' ', '\0'};
so sizeof(a) is 2 since there is 2 char in the array.

that is a special notation that work just for char. Obviously you cannot do
int i[] = " "; // will not compile

the right way is to do
int i[] = { 1, 2, 3 };

when you initialize a c style string the compiler will add a \0 character at the end.

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

agree

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

Actually, in case of:

char a[]='';

it compiles with sizeof(a) = 1.

- sergey.a.kabanov January 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char a[] = " ";

If there is no space between quotes, then sizeof (a) is 1 byte. the reason is array store '\0' at end of every string.

e.g.
char b[] = "Keyur";
printf ("Sizeof(b) = %d\n", sizeof(b) );

it will print 6. total 5 chars from "Keyur" and '\0'

- keyurpatel80 May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

only bloomberg would like to ask this kind of question. They just need robot

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

totally agree. this type of question doesnt say anything about well a person can program.

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

You guys failed on Bloomberg interview? This shows ur understanding of pointers and memory allocations which in turn helps in programming...

- Mahesh February 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mahesh you are hired as expert c programmer !!, only Bloomberg uses FORTRAN, C pointers when everyone else is moving ahead

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

well, linux still uses c
The problem is not which language is the best. The problem is in which language you are the best.

- Anonymous February 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

In char a[] = " ";
'a' is an empty string i.e., points to '\0' which is defined as integer 0
hence it is sizeof('\0') which is sizeof(0) which is sizeof(int) = 2

- SM March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry but there needs to be some correction in your statement above.
sizeof(int).. is mostly 4 (for 4 byte).. and again.. depends on your machine platform.

If sizeof(int) is 2 for your computer, hope you get a new computer because thats from the old ages. sizeof(\0) also is not sizeof(0) because zero is an integer which should be 4.

The answer for sizeof(a) is true ..because for the declaration -> char a[] = " ", the answer is whatever is in the double quotes + NULL char.

For sizeof(p) where declaration is char *a, it will always be 4 for 32-bit machines or 8 for 64-bit machines, because the asterisk is really a pointer where as the [] symbol denotes and array with.

In order words there is a difference in sizeof for
char a[] = "hello";
and
char *p = "hello";
where
sizeof(a) = 6
sizeof(p) = 4

Read "Pointers on C" book if you want to know more as it depicts a diagram. You will not know your C well if you don't read that book.

- bwc March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello guys,
I don't think any of you is right!!!!
For the simple reason that you are arguing, and mahesh is too arrogant to explain why things are as they are!
I am new to C but study very hard!
No one pointed out that the answer depends on compiler.
I was surprised to see, my compiler "Miracle C" gives the following output after the program is ran:

program(same as bala's code here):

#include <stdio.h>


main () {
char a[] = " ";
char b[10];
char *c;
char *d = b;



printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
printf ("Sizeof(c) = %d\n", sizeof(c) );
printf ("Sizeof(d) = %d\n", sizeof(d) );
getchar();

}

output:

Sizeof(a) = 0
Sizeof(a) = 10
Sizeof(a) = 2
Sizeof(a) = 2

Now, since mahesh is such an expert, can he explain the above!!!!
Also Miracle C will show:

char arr[]="Hello";

sizeof(arr) will show to be 0;

OK experts, please enlighten me, because I do not know why, but this is the output I get!!!!!!!!!
Thank you!

- Raul Gonzales March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude! change ur compiler to GCC, otherwise u wont get any interviews..Miracle C seems to be so far from standards! also sizeof(pointer) is 2 for your computer shows that it is 16 bit.

- Anonymous April 19, 2010 | 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