Bloomberg LP Interview Question for Software Engineer / Developers






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

I think you can start with visiting each element from ptr to ptr + sizeof(ptr)/sizeof(ptr[0])..if the ptr was defined as an array.
Now you have the pointer to each element...
Was this to be done in C or C++?

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

u dont need this. ptr++ automatically moves to the next element depending on its type.
i.e if it was char array, ptr++ moves one byte at a time.
if it was int array, ptr++ moves 4bytes.

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

user overloaded functions for each type with arguments as first element value, pointer to the first element and number of elements.

- Brahmananda Reddy December 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we know the size of the array the pointer points to?

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

yupp the size of array is given say n.
i believe this was to be done in C++.

my guess was to use templates ..

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

yupp the size of array is given say n.
i believe this was to be done in C++.

my guess was to use templates ..

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

if we use template then how do you templify the operation - add/concatenate?

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

operator+ for std::string means concatenate so whatever you do, you use operator+

- eric May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think its easy to write a template to add int, float...

template<class T>
T addarray(T *v, int n)
{
T sum=0;
for(i=0;i<n;i++)
{
sum+=v[i];
}
return sum;
}

but for char and string the above code template shld be changed...I donno how we can write a generic code for all the four different data types for both add & concatenate...

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

For that template specialization can be used and special case can be added for the char * type

- Saurabh February 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since we do not know what type of data is present inside the array(int,float,char), the trick is to identify the data type first. To do that, we can increment pointer location.
Example

if ptr points to first block of the array, then (ptr+1) points to second position in the array. Find the difference between both the blocks of memory.
If they are separated by 8 bytes, its char
separated by 32 bytes its int
separated by 64 bytes, its float.

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

But the sizes of different data types are dependent on machines.

- Xiao January 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mike...its bits not bytes....i guess

- Neerav March 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bits..

- su July 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

finding a data type can be done easily using typeid(object).name();

- @Mike.. January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

At first, the result can be define as a class with member functions of + and += operators
I guess we can define the opertator + and += as pure vitual function in the base class. Then define derived classes to accept int/float/char/string. In these derived classes, redine the + and +=. Use the reference of the base class to call + and += dynamcially.

class sum{
public:
virtual sum& operator+=(const sum&) = 0;
}

class sum_int: public sum{
public:
sum_int(const int&);
sum_int& operator+=(const sum_int&);
}

class sum_float: public sum{
public:
sum_float(const float&);
sum_float& operator+=(const sum_float&);
}

class sum_string: public sum{
public:
sum_string(const string&);
sum_string& operator+=(const sum_string&);
}

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

template + operator overloading?

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

just use std::accumulate

template <typename T>
T AddOrConcatenate(T* first, int N)
{
return std::accumulate(first, first+N, T());
}

simple as that. Works for any type that has an operator+ defined

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

Eric, your answer is awesome, but I think for char you need a specialization,
since char() is not the initial value for char concatenation, and operator+ is not defined.

- Haoju July 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With some template tricks, we can deduce the correct return type using another simple template, so the main template doesn't need any specialization:

template<typename T>
struct Aggregate {
    typedef T type;
};

template<>
struct Aggregate<char> {
    typedef string type;
};

template<typename T>
typename Aggregate<T>::type combine(T *first, size_t N) {
    typename Aggregate<T>::type sum = typename Aggregate<T>::type();
    for (T *p = first; p < first + N; ++p) {
        sum += *p;
    }
    return sum;
}

- stachenov October 19, 2016 | 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