Bloomberg LP Interview Question for Financial Software Developers






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

XIV

Inline Function

Preprocessor Macro – good for declaring constnts eg. #define PI 3.14
Provide textual substitution
Each time the macro name is encountered with arguments, the arguments used in its definition are replaced by the actual arguments found.

1

It is a function provided by C++.
It is declared by using the keyword inline before the function protoype.

It is a a preprocessor directive provided by C.
It is declared by using the preprocessor directive #define before the actual statement.

2

Expressions passed as arguments to inline functions are evaluated once.

In some cases, expressions passed as arguments to macros can be evaluated more than once.
Every time you use an argument in a macro, that argument is evaluated.

#define max(a,b) (a>b?a:b)

void main()

{

int a = 0;

int b = 1;

int c = max(a++, b++);

cout << a << endl << b << endl;

}

The intention is to have the program print 1 and 2, but because the macro is expanded to:

int c = a++ > b++ ? a++ : b++;

b will be incremented twice, and the program prints 1 and 3. //try

3

They are parsed by the compiler

They are expanded by the preprocessor at pre-compile time.

4

Inline functions follow all the protocols of type safety enforced on normal functions.
Argument types are checked, and necessary conversions are performed correctly.
The compiler performs return type checking, function signature before putting inline function into symbol table.
They can be overloaded to perform the right kind of operation for the right kind of data.

No such thing, hence, macros are more error prone as compared to inline functions. the The parameters are not typed (the macro works for any objects of arithmetic type).
No error checking is done during compilation.
Eg. you can pass strings to a macro that does some integer arithmetic

#define MAX(a, b) ((a < b) ? b : a)



int main( void)

{

cout << "Maximum of 10 and 20 is " << MAX("20", "10") << endl;

return 0;

}

5

Can be used for debugging a program as they are expanded at compile time and a break point caa be placed at the inline function adefinition and step into the method for debugging step by step.

Can not be used for debugging as they are expanded at pre-compile time.

6

Inline member functioncs can access the class’s member data.

The preprocessor has no permission to access member data of a class and are thus useless even if used within a class. Thus they can not even be used as member functions.
Eg.

#define VAL (X::i) // Error

class X {

int i;

public:
---};

7



Paranthesis need to be handled carefully as missing a pair or putting an extra pair may prouce undesirable results.

8

Inline functions may or may not be expanded by the compiler. It is the compiler’s decision whether to expand the function inline or not.

Macros are always expanded.

9

It can be defined inside or outside the class.

It can not be defined insode the class.

10

Inline functions pass arguments by value, just like regular functions do. If the argument is an expression such as 4.5 +7.5 then the function passes the value of the expression 12 in this case.

Expressions passed into macros are not always evaluated before entering the macro body.

Macros don’t pass by value.

Thus , #define square(x) x*x

:

b=square(4.5+7.5)

This will be replaced by : b=4.5+7.5*4.5+7.5

11

Since type checking can be used in inline functions, theis problem can be avoided.

Expressions may expand inside the macro so that their evaluation precedence is different from what you expect.

Eg.

#define FLOOR(x,b) x>=b?0:1

Now, if expressions are used for the arguments

if(FLOOR(a&0x0f,0x07)) // ...

the macro will expand to

if(a&0x0f>=0x07?0:1)

The precedence of & is lower than that of >=, so the macro evaluation will surprise you.

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

Why static is not used to define global/local buffer?

Since the buffer lives through out the life of the program.

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

A buffer is nothing but a temporary storage for data. So, the values of buffer keeps on changing. But, if we are defining it as a static, we can assign the value only once. And the buffer will no longer support it's main objective of temporary storage as it will permanently store the value. Hence, ....... buffer can't b static

- :P February 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Values can be assigned only once to static? Are u sure? then do we even have const?

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

Why static is not used to define global/local buffer?

The static buffer overrun occurs when the stack of the buffer is overwritten by the data larger than the buffer size.

- papaya January 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Macros are expanded by the preprocessor while inline functions get parsed by the compiler, inline functions also check for type safety, macros potentially have many pitfalls

- dagnihot March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int fibonacci (int x)
{

if (x < 3)
{
return x;
else
{
//using recursion
return fibonacci(x-1) + fibonacci(x-2);
}
}

- Shan December 25, 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