Bloomberg LP Interview Question for Financial Software Developers






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

Here there is no way of telling the value of i because i has not been initialized.

Static keyword preserves the value of variable across function call.

Yes if the program is multithreaded then before return i++ each thread should obtain a lock return i

- Anonymous April 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think since it is static variable, i is initialized to 0.
Hence, the first time foo is called, 0 is returned and the second time, 1 is returned.

- Anonymous April 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Static keyword (file static or function static) have the following properties:
+ Initialized only once at runtime (to zero)
+ Placed in the data section of the program i.e. memory location is fixed for the life of the program
+ Retain the value for the entire life time of the program.

Function static variables are only visible inside the function they are declared in; File static variables are only visible inside the file they are declared in.

Contrast that with non-static Local variables. Local variables have the following properties
+ Not initialized, unless explicitly coded
+ Variable itself disappears once the block context disappears i.e. no retaining value business
+ Storage is allocated on the stack afresh each time a new block context is created.

For your question,
+ value of i after 2 iterations is 2, but the returned value for the second call to foo is 1. This is because of the post-increment operator.
+ All global variables need protection from multiple contexts. So, a lock is required for the load-modify phase of the variable i.

- sk_seeker April 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi You are absolutely correct, But i is a static variable so it's value will persist across the function calls, in case multiple threads of foo are executing then the changes made by one thread will be reflected in another thread so as u stated here lock is must.

- mishra.dinesh April 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Static Hence its initialised to 0 . we must all see that its declared inside the function . So , the first time its called , its set to 1 . the second time also it should be reinitialised to 0 and value should be 1 again ..
if it was something like

int static i ;

int foo
{
return i++;
}
this would be 2 in the second iteration .

In this case

int foo
{

int static i ;
return i++
}

The reinitialising will definetely happen !!

correct me if i am wrong

- Mallika April 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Mallika

Here reinitialization will not happen in second case also, the reason is static variables are stored in data section which is outside the stack of the executing function and hence it's value will persist until the program is terminated, When a function is called a new stack frame is created and all non static variables are reinitialized as per their types.

- Dinesh April 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi ,

So , in this case i got what u sed .. so the answer is 1 or 2? bcos of the post increment thing? doest it return first and then increment ?

- Mallika August 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dinesh is right, try this

#include<iostream>
using namespace std;

int foo() {
	static int i;
	return ++i;
}

int main() {
	cout << foo() << endl;
	cout << foo() << endl;
	return 0;
}

The answer is
1
2

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

I think you made a mistake with respect to what is asked. You used ++i. The code in the question uses i++ which would make it print
0
1

- Raghuram Onti Srinivasan February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code is not thread safe.
Since local Static Variables are not thread safe.  For example 
int foo()
{
static int i = SomeF();
return i++;
}

this function is internally converted as 
int foo()
{
static bool i_init = false;
static int i;   // Ths is uninitialized
 if (!i_init) {
   i_init = true;
    i = SomeF();
 }
 return i++
}

Above code has race condition. So not thread safe.
Keeping this function's body inside Critical section can  solve thsi problem.

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

final value of i is 2.
static variables are only initialized only once (default is 0) during compilation of program. and as they are incremented or changed inside the functions their values remains throughout the lifetime of the program. Static are saved inside the data segment like globals but have limited scope if defined inside the function.

- anonymous January 02, 2010 | 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