Bloomberg LP Interview Question for Financial Software Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
12
of 18 vote

The behaviour of this type of self-assignment is undefined and dependent on the compiler implementation. So, the result may be either 0, or 10, or even a not-compilable code. The undefined behaviour happens due to missing sequence point in the evaluation of expression "count = count++;"
You can read it @en.cppreference.com/w/cpp/language/eval_order

- ashot madatyan May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void fun(){
int count = 0;
for(int i=0; i < 10; ++i)
count = count++;

std::cout << count;
}
int main(){
fun();
}
I tried with g++ with -Wall option. I get 0.
compiler tells:
warning: operation on ‘count’ may be undefined [-Wsequence-point]
count = count++;
^

Thus undefined.

- algo_guy October 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

Undefined behavior!

- Rajendra May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

As far as I know that code is equivalent to

int count = 0;
for(int i=0; i < 10 ; ++i){ count = count; count = count + 1;}
std::cout << count;

so I expect to see 10 on std::cout;

- Albino December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I'm a java guy but here goes. Since there are no braces to identify the block of code in the for loop, the count = count++; statement is the only statement part of the for loop. So count goes from 1 (count being initially o, count++ is 1) to 10 (the last value for count is 9, so count++ is 10) The output of the code is 10.

- Leon Bland May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

G++ compiler gives output as 10. However I am not sure why people are calling the behavior as undefined

- crystal.rishi2 October 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int count = 0;
40117e: c7 44 24 0c 00 00 00 movl $0x0,0xc(%esp)
401185: 00
int res = 0;
401186: c7 44 24 08 00 00 00 movl $0x0,0x8(%esp)
40118d: 00
res = count++;
40118e: 8b 44 24 0c mov 0xc(%esp),%eax
401192: 89 44 24 08 mov %eax,0x8(%esp)
401196: 83 44 24 0c 01 addl $0x1,0xc(%esp)
res = ++count;
40119b: 83 44 24 0c 01 addl $0x1,0xc(%esp)
4011a0: 8b 44 24 0c mov 0xc(%esp),%eax
4011a4: 89 44 24 08 mov %eax,0x8(%esp)
count = count++;
4011a8: 83 44 24 0c 01 addl $0x1,0xc(%esp)
count = ++count;
4011ad: 83 44 24 0c 01 addl $0x1,0xc(%esp)

My friend and I did some research about this question with asm. We found for res = count++, the asm does that:
temp = count;
res = count;
count = count +1;
if we say count = count ++, compiler will optimize this 3 one line into one count = count +1;
I think the tricky reason that confuses ppl comes from how we implement operator ++(int) overload for class. We would copy the object, increase the object, then return the old obj for return and assignment. But for primary type, compiler copy, assign and then increase. That's a good question to understand the primary type's difference from the operator overload for class.

- Anonymous November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

10

- Mohamed Gaber May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not 0?

if I do

int i=0, j;

j = i++;

j will be 0 rather than 1

- Itcecsa May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

try i = i++; you will find the surprise

- haha November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is really a very good question. Thanks for the post.

- Pratik Soni May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

ouput will be 10
we can write count=count++; as two steps i.e.
count=count;
count=count+1;

- maverick May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think the answer should be 10

- narendra agrawal May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I compiled both

count=count++

and

count =++count

with gcc version 4.1.2 both result the same output as 10.

- Subhransu June 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, Can anyone please provide me a link from where i can buy or download gcc 4.1.2 version compiler

Thanks
Anand

- Anand January 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

gives 10 in VC++2008

- rajib.mostafiz June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Answer should be 10.

- Pradeep June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

It is correct, in each loop after self assignment it's value get incremented by one, and next time the updated value gets self assigned and gets incremented.

- sourav June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think its coz of precedence. Since ++(post increment) have higher precedence than =(assignment) operator..thus for the first iteration, its picks value 0 increment it to 1 and then assigns to count and it goes on.

- manishvaidya June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

case( I )

#include <iostream>
using namespace std;

void main()
{
int count = 0;
for(int i=0; i < 10; ++i){
count = count++;
}

cout<<"Output of this code is: "<<endl;
std::cout << count <<endl;


}


root [0] .L main.C
root [1] main()
Output of this code is:
0

case ( II )

#include <iostream>
using namespace std;

void main()
{
int count = 0;
for(int i=0; i < 10; ++i){
count = ++count;
}

cout<<"Output of this code is: "<<endl;
std::cout << count <<endl;


}


root [0] .L main.C
root [1] main()
Output of this code is:
10

- Joanna8848 January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int i, x;

i = 2;
x = ++i;
// now i = 3, x = 3

i = 2;
x = i++;
// now i = 3, x = 2

'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.

- Joanna8848 January 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am using g++ 4.1.2, it gives 10

- xicheng March 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Output will be 10

- Swarupa August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count++ returns unincremented value, so it should be zero. however, it's probably undefined behaviour and Xcode gives warning "multiple unsequenced modifications", same program, but with ++count compiles without warning and gives 10.

- andrey November 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for ( int count = 0; count <= 10; count++ ) { }

- Anonymous October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main() {
char *str
"12345";
printf("%c %c %c", *str, *(++str), *(++str));
head
}

- Anonymous May 30, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int counter(int i) {
static int count=0;
count=count + i;
return(count);
}
main() {
int i, j;
for(i=0;i<=5;i++) j=counter(i);
printf("%d", j);
}

- Anonymous May 30, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

Answer will be zero,
Because count=count++,
1step: count is zero so..count =0;
2step:count get incremented by 1,
3step:but it does not have 3 step in loop so again count is zero .
so final output is zero.
If count=++count then o/p will be 10.

- Anonymous May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Answer will be 0. Since value is first used then incremented. So count=0 is tempoarily stored somewhere then incremented so count=1 but in assignment of count=count++ count is again replaced by 0. count would be 10 if ++count is used.

- Rishi May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

c++ standard doesnt allow writing same variable twice in an expression. It is undefined behavior or compiler error

- kraj October 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote

check the question again..

- jayram singh September 17, 2012 | 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