Bloomberg LP Interview Question for Financial Software Developers






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

A is the correct answer.
B: greater<>() takes 0 arguments. It has a member function operator() that takes two arguments, which returns true if its first argument compares greater than the second one using operator>, and false otherwise. The greater class does not have a constructor taking one argument.
D: remove_if is not a member function of vector class.
E: remove_if_greater does not exist in vector class.
Why choose A not C:
bind2nd(greater<int>(),50) and bind1st(less_equal<int>(), 50) are the unary function objects constructed from the binary function objects greater<int>() and less_equal<int>() by binding its 2nd/1st parameters to 50. So the above function objects can be considered as:
bind2nd(greater<int>(),50) "greater than 50?" (true if > 50)
bind1st(less_equal<int>(), 50) "greater than or equal to 50?" (true if 50<=)

Function
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred):
Applies pred to the elements in the range [first,last), and removes those for which it does not return false from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range. So the elements between the new end and the old end should not be valid.
So,
std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)) will remove the elements >50 in items, and return a new end of items. And erase() will remove the elements between the new end and the old end.

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

it's a

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

Oops..!
OOPS sucks! Its official now.
Its (f) :)

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

It is D. We need to bind 50 with greater so that it will take 50 to compare it with vectory arguments.

- Maankutti September 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

D is wrong. You can't use remove_if only. it must combine with vector::erase() to remove the elements from the vector.

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

http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.2/structstd_1_1greater.html

The answer is b.

- Muyalkutti September 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Options A and C can be safely eliminated from possible choices because 'erase' cannot take 3 parameters.

Option E can also be safely eliminated because there is no function such as vector::remove_if_greater().

Option D can also be safely eliminated because there is no function such as vector::remove_if(). It is in fact std::remove_if()

Option B is the only choice left which should be true, although I have not been able to compile it.

Had there been an option F, it would have been:
f.std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)));

Hence, option F is the tested correct thing which I have tested myself and it works 100%

- Fairy September 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correction: Options A and C can be safely eliminated from possible choices because 'erase' cannot work on a comparison criteria, it only works on range criteria.

- Fairy September 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A is the answer.This is the compiled code.

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
unsigned int i;
vector<unsigned int> items;

for (i=50; i<=60; i++) items.push_back(i);
items.erase(std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)), items.end());
//std::remove_if(items.begin(), items.end(), std::greater<int>(50));
//items.erase(std::remove_if(items.begin(), items.end(), std::bindlst(std::less_equal<int>(), 50)), items.end());
//items.remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50));
//items.remove_if_greater(items.begin(), items.end(), 50);
cout << "Vector contains:";
for (i=0; i<items.size(); i++)
cout << " " << items[i]<< endl;

return 0;
}

- Ano September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

only A is correct, c is incorrect

- Ganesh October 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a and c both are correct answers. Try the following code by 1st uncommenting a and see the result, then uncomment c and you'll see the same result as that of a.

@ano.... you have written the 'l' alphabet instead of digit '1' in std::bind1st (its read as bind first).

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>

void main()
{
 	std::vector<int> a;
	a.push_back(10);
	a.push_back(90);
	a.push_back(60);
	a.push_back(40);
	a.push_back(35);
	a.push_back(100);

	std::cout << "\n Original vector: ";
	for(size_t x=0; x < a.size(); x++)
		std::cout << "\n " << a[x];

	// a - correct
	//a.erase(std::remove_if(a.begin(),a.end(),std::bind2nd(std::greater<int>(),50)),a.end());

	// b - wrong
	//std::remove_if(a.begin(), a.end(), std::greater<int>(50));

	// c - correct
	//a.erase(std::remove_if(a.begin(), a.end(), std::bind1st(std::less_equal<int>(), 50)), a.end());

	// d - wrong
	//a.remove_if(a.begin(), a.end(), std::bind2nd(std::greater<int>(), 50)));

	// e - wrong
	//a.remove_if_greater(a.begin(), a.end(), 50);
	
	std::cout << "\n\n New vector: ";
    for(size_t x=0; x < a.size(); x++)
		std::cout << "\n " << a[x];

	std::cout << "\n";
}

- Rajika Tandon November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I select (c).

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

vector doesn't have remove_if. vector's erase doesn't take 3 arguments. It just takes one or two iterators. So, the unanimous winner is (b).

- Helper December 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is A
Refer this
//sgi.com/tech/stl/remove_if.html

- Sunil December 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(b) is incorrect
because remove_if does NOT physically removes items. it just re-arranges them and points to first one which has the predicate = true;

10, 55, 30, 40 -> 10, 30, 40 "ITR" 50

To remove items from the vector you must still call erase.

(c) seems does quite opposite to what is required.

(d) and (e) just incorrrect because there is no such member functions in std::vector

- C_plus_plus_novice December 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes, the answer is A

- chsof7 February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just a typo correction in the answer from Yonggang:
"bind1st(less_equal<int>(), 50) "less than or equal to 50?" (true if 50<=) "

- georgeshmelev March 25, 2013 | 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