Bloomberg LP Interview Question for Software Engineer / Developers






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

They asked my this questions also, Any answer? Please....

- Dong February 07, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yeah I'd like to know this too, if it was:

return main();

it would call the function again no? But with it just being:

return main;

I'm not so sure.

- GC February 07, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return main;

Would need to be casted to return the address of the function main.

If it was:
return main();

It should call the main function again and again until the stack overflows. Each time main() is called, it pushes the program counter onto the stack. I don't know the size of the address it pushes, but I don't think any other variables are pushes on the stack after each call. I could be wrong though.

- TheChronoTrigger February 07, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main {

return main;
}

will lead to an error as main() must return an integer and in this case main does not specify anything.

- Aditya February 09, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Main cannot call itself and the compiler will issue an error if you call main within main. Main is an entry point to the program, not a user-defined func.

- Jack February 09, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried it ones. main is the regular function. You can call it recursivly.

- MS August 25, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it throws an error alright... return type expected is int.

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

Some compilers may throw an error, but Visual C++ will only issue a warning. Also tried it on my Linux box with gcc and it seems to compile and run as expected.

- TheChronoTrigger February 10, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well turns out that it is legal in C and not in C++. So both Jack and ChronoTrigger are right. Also turns out that it is not that lame a question :)

- vatson February 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So whats the answer then? What happens in either case. C/C++

- CG February 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vatson.. Where did you read that it is not legal in C++? I might be wrong, but I believe it is legal in C++ as well. The operating system might call all sorts of stuff before it gets to main, but I believe main is just a regular function.

CG... if the code was:
int main(){

return main;

}

It would not compile as it is misleading what main is. If you cast main to an int, it would return the address of the main function as an int.

int main(){

return (int)main;

}
would return something like 4427357.



It the code was:
int main(){

return main();
}

It would recursively keep calling main(). The stack would eventually overflow because each time we call main(), we push the program counter onto the stack.

Some psuedo assembly to illustrate the point.
0000: NOP // main function.. NOP = no operation
0001: Mov 40, 01h; // Some random instruction (meaningless)
0003: Call 0000h; // call the main subroutine again
0007: RTS // End the subroutine (we never get here)

Each time we get to the position 0003 we call the function at 0000. You need to know were you will be after the function finishes, so right before you call the function, you push the address 0007 on the stack. Since each call to the function at 0000 will call another function, you never get to return and the stack will continue to grow with all those 0007's you keep pushing.

- TheChronoTrigger February 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But would you ever use it? That's why though maybe not lame, it is a really sort of obscure question.

- Reggie February 14, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

True we might not use it...but the idea behind such questions is usually the conceptual understanding. It gives a clear idea whether you know what goes on behind the scenes and how compilers work.

Just to add ...Chronotrigger....it is illegeal in C++ and is categorized as undefined behavior. So even though you got it compiled successfully, you never know how its going to act.

- vatson February 14, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is legal both in C and C++.
If looks like a "how to implement" the virtual function in C.
hehe.

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

g++:

g++ test.cpp -ansi -o test.o
test.cpp: In function `int main()':
test.cpp:6: error: invalid conversion from `int (*)()' to `int'
make: *** [all] Error 1


gcc:


gcc test.c -ansi -o test.o
test.c: In function `main':
test.c:3: warning: return makes integer from pointer without a cast
test.o
make: *** [all] Error 52

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

Well i ran this code on my linux box and i get a seg fault. Which makes sense since it'll go into an infinite loop (keep putting the PC onto the stack and call main() again) until it runs out of allocated memory for the program.

- Zaphod April 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I hear lot of answers here..
But the correct answer is it is gonna compile fine and when you run this one it will run for a few seconds until the stack runs out of memory. Each time you call main, it should have the return address which is 4 bytes in the stack. So eventually its gonna eat all the 4 bytes that the OS provides and once it runs out of the memory it is gonna give you segmentation fault because it is illegally using some address for the next function call once it runs out of memory.


To see interesting things you could print a message saying

main()
{
printf("I am in the main \n");
main();
}
This will give you how exactly the first few seconds are gonna run by printing the same sentence again and again.
Good question although not that tough.

Hope this helps.

- hope this helps April 07, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You will get this because main is referred as pointer to function
A function with return type "int" may not return a value of type "int(*)(void)"

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

It would compile successfully,however after few seconds it would stop working.Typically flashing a message saying program name.exe file has stopped working/crashed.

- Ano October 14, 2009 | 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