Bloomberg LP Interview Question for Software Engineer / Developers


Team: Price history
Country: United States
Interview Type: In-Person




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

just making compiler compliant. no meaning.

#include <iostream>
using namespace std;
class test
{

int male;
int female;
string why;
public:
test& operator[](string a)
{
if (a == "male")
++male;
else
++female;
return *this;
}
test& operator=(string a)
{
why=a;
return *this;
}
};


int main()
{
test a;
a["male"]["female"] = "super";

return 0;
}

- suresh August 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <map>

using namespace std;


class mmap : public map<string, map<string, string> >
{
public:
    map<string, string>& operator [] (const string& arg)
    {
        if (this->find(arg) == this->end()) {
            map::operator[] (arg) = map<string, string>();
        }
        return map::operator[](arg);
    }

};

int main()
{
    mmap obj = mmap();
    obj["string1"] ["string2"] = "Some value!";
    
    cout << &obj["string1"] << endl;
    cout << obj["string1"]["string2"] << endl;
}

- vvvladdd October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't get why derived class need to define operator[] ? I'd understood if you use `private' inheritance and then just:

public:
using Base:operator[];

And typedef can do that too:
typedef std::map<std::string, std::map<std::string, std::string> mmap;

- Anonymous November 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And use a template to make it contain generic types.

template <typename T>
class mmap: private std::map<std::string, std::map<std::string, std::string>>
{
public:
using std::map<std::string, std::map<std::string, std::string>>::operator[];
};

- Jose November 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I meant:

template <typename T>
class map3 :private std::map<std::string, std::map<std::string, T>>
{
public:
using std::map<std::string, std::map<std::string, T>>::operator[];
};

- Jose November 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or you can use the c++11 alias declarations:

template <typename T>
using mmap = std::map < std::string, std::map<std::string, T>> ;

int main()
{
mmap<std::set<int>> mm;
std::set<int > s= {1, 2, 3};

mm["0"]["1"] = s;

return 0;
}

- Jose M. November 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MM
{
	unordered_map<string, unordered_map<string, MM>> m;	

public:

	unordered_map<string, MM>& operator[](const string& s)
	{
		return m[s];
	}
};

int main()
{
	MM obj1;
	MM obj2;
	obj1["s1"]["s2"] = obj2;
	
	return 0;
}

- luminaobscura December 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <typename T>
using mmap = std::map < std::string, std::map<std::string, T>> ;

then you can do:

mmap<int> x;
x["gigi"]["migi"] = 2;

- mcmarcu April 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A working code for this

#include <iostream>
#include <string>
#include <map>
#include <unordered_map>

using namespace std;

template <typename T>
class classMap
{
private:
	map<string, map<string, T> > mmap;
public:
	classMap()
	{
	}
	
	~classMap(){};
	
	map<string, string>& operator [](string str)
	{
		if(mmap.find(str) == mmap.end())
			mmap[str] = map<string, T>();
		return mmap[str];
	}

	void getData()
	{
		for(typename map<string, map<string, T> >::iterator it = mmap.begin(); it != mmap.end(); ++it)
		{
			cout<<it->first<<" , ";
			typename map<string, T>::iterator it_second = (it->second).begin();
			cout<<it_second->first<<" , "<<it_second->second<<endl;
		}
	}
};

int main(int argc, char const *argv[])
{
	classMap<string> c;
	c["rob"]["van"] = "dam";
	c["the"]["big"] = "show";
	c.getData();
	return 0;
}

- Rakesh Sinha April 15, 2017 | 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