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.
3
of 3 vote

This problem can actually be solved in a very simple manner in C++. You just take the pointer to the first element in the multidimensional array and then you keep advancing that pointer until you reach the last item in the multidimensional array which would be the first elements pointer plus size of the multidimensional array. As a result, the only two things you need to pass to your function is a pointer to the first element of the array and the size of the array:

#include <iostream>

using namespace std;

#define X 5
#define Y 3
#define Z 2
int array3D[X][Y][Z];

void printNDArray(int *arr, int size)
{
	for(int i=0;i<size;i++)
	{
		cout << *(int *)(arr+i) << endl;
	}
}

int main()
{
	//Generate Multidimensional Array Going From 1 to MAX
	int val=1;
	for(int i=0;i<X;i++)
	{
		for(int j=0;j<Y;j++)
		{
			for(int k=0;k<Z;k++)
			{
				array3D[i][j][k] = val;
				val++;
			}
		}
	}
	
	//Pass pointer to first value in array and the size.
	int size = X*Y*Z;
	printNDArray(&array3D[0][0][0], size);
}

- Simmon Yau November 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Java Solution:

public static void main(String[] args) {

		System.out.println("String 3D Array");
		String[][][] str3DArray = { { { "a", "b" }, { "c", "d" }, { "e", "f" } },
				{ { "g", "h" }, { "i", "j" }, { "k", "l" } } };

		showArray(str3DArray);

		System.out.println("int 2D Array");
		int[][] int2DArray = { { 0, 1, 2, 3 }, { 3, 2, 1, 0 }, { 3, 5, 6, 1 },
				{ 3, 8, 3, 4 } };
		showArray(int2DArray);

		System.out.println("int 3D Array");
		int[][][] int3DArray = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
				{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };

		showArray(int3DArray);
		
		System.out.println("int 1D Array");
		int[] int1DArray = { 0, 1, 2, 3 };
		showArray(int1DArray);
		
		System.out.println("float 3D Array");
		float[][][] float3DArray = { { { 0.0f, 1.1f }, { 2.0f, 3.1f }, { 4.0f, 5.1f } },
				{ { 6.0f, 7.1f }, { 8.0f, 9.1f }, { 10.0f, 11.1f } } };

		showArray(float3DArray);
		
		System.out.println("Integer 3D Array");
		Integer[][][] integer3DArray = { { { new Integer(0), 1 }, { new Integer(2), 3 }, { new Integer(4), 5 } },
				{ { new Integer(6), 7 }, { new Integer(8), 9 }, { new Integer(10), 11 } } };

		showArray(integer3DArray);
	}

	public static void showArray(Object objects) {

		Object[] objArr = null;
		if (null != objects && null != objects.getClass()
				&& null != objects.getClass().getComponentType()
				&& objects.getClass().isArray()) {
			objArr = (Object[]) (toObjectArray(objects));
			for (int i = 0; i < objArr.length; i++) {
				showArray(objArr[i]);
				if (objArr[i].getClass().isArray()) {
					System.out.println();
				}
			}
		} else {
			System.out.print(objects);
		}
	}

	@SuppressWarnings("unchecked")
	public static Object[] toObjectArray(Object source) {
		if (source instanceof Object[]) {
			return (Object[]) source;
		}
		if (source == null) {
			return new Object[0];
		}
		if (!source.getClass().isArray()) {
			throw new IllegalArgumentException("Source is not an array: "
					+ source);
		}
		int length = Array.getLength(source);
		if (length == 0) {
			return new Object[0];
		}
		Class wrapperType = Array.get(source, 0).getClass();
		Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
		for (int i = 0; i < length; i++) {
			newArray[i] = Array.get(source, i);
		}
		return newArray;
	}

- Amit K Bist September 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

har mat[2][4][6] =
{
{
{'w', 's', 'r', 't', 'g', 'g'},
{'s', 'a', 'c', 'h', 'i', 'n'},
{'k', 'c', 'h', 'u', 'j', 'j'},
{'o', 'h', 'i', 'n', 'y', 'q'},
},
{
{'w', 's', 'r', 't', 'g', 'g'},
{'s', 'a', 'c', 'h', 'i', 'n'},
{'k', 'c', 'h', 'u', 'j', 'j'},
{'o', 'h', 'i', 'n', 'y', 'q'},
},
};

void
ndfunc (char *a, int *loop, int maxl)
{
int im = 1, imost, i, iloop;

i = 0;

while (i != maxl) {
im = im * loop[i];
i++;
}

while (im) {
printf(" %c ", *a);
a++;
im--;
}
}

int
main ()
{
char *ptr = &mat[0][0][0];
int loop[3] = { 2, 4, 6};

ndfunc(ptr, loop, 3);
}

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

Here is my solution by templates.

#include "stdafx.h"
#include<iostream>
using namespace std;
const int cMaxSize = 100;

template<typename T>
class OneDimArr
{
public:
	OneDimArr(int aSize = cMaxSize)
	{
		m_Elems = new T[aSize];
	}
	~OneDimArr()
	{
		delete []m_Elems;
	}
	T& operator[](int aIndex)
	{
		return m_Elems[aIndex];
	}


private:
	T* m_Elems;
	int m_Size;
};

int _tmain(int argc, _TCHAR* argv[])
{
	OneDimArr<int>							oneDimArr;
	OneDimArr<OneDimArr<int> >				twoDimArr;
	OneDimArr<OneDimArr<OneDimArr<int> > > threeDimArr;

	oneDimArr[0] = 8;
	twoDimArr[0][2] = 23;
	threeDimArr[1][0][3] = 8;

	cout << oneDimArr[0] <<'\t'<<twoDimArr[0][2] << '\t'<< threeDimArr[1][0][3]<<endl;	

	return 0;
}

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

package koustav;

public class Tree
{
  public static void main(String args[])
  {
    String a[][][] =
    {
      {
        { "a", "b" },
        { "c", "d" },
        { "e", "f" } },
      {
        { "g", "h" },
        { "i", "j" },
        { "k", "l" } }
    };
    printArray(a);
  }

  public static void printArray(Object obj)
  {
    Class cls = obj.getClass();
    Object obj1[] = (Object[]) cls.cast(obj);
    if (cls.getComponentType().isArray())
    {
      for (Object obj2: obj1)
      {
        printArray(obj2);
      }
    }
    else
    {
      System.out.print("\n");
      for (int i = 0; i < obj1.length; i++)
      {
        System.out.print(obj1[i]);
      }
    }
  }
}

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

Below line is giving ClassCastException for the one dimensional array

Object obj1[] = (Object[]) cls.cast(obj);

- algolearner September 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ #include<iostream> int Arr[2][3][3]={{{1,2,3},{4,5,6},{7,8,9}}, {{21,22,23},{24,25,26},{27,28,29}}}; template <typename T> void printArrWoutLB(T *x,int Dim){ while(Dim--) std::cout<<*x++<<"\n"; } int main(){ printArrWoutLB((int *)Arr,sizeof(Arr)/sizeof(Arr[0][0][0])); return 0; } }}} - Mohit September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ #include<iostream> int Arr[2][3][3]={{{1,2,3},{4,5,6},{7,8,9}}, {{21,22,23},{24,25,26},{27,28,29}}}; template <typename T> void printArrWoutLB(T *x,int Dim){ while(Dim--) std::cout<<*x++<<"\n"; } int main(){ printArrWoutLB((int *)Arr,sizeof(Arr)/sizeof(Arr[0][0][0])); return 0; } }}} - Anonymous September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

/*
* My solution is simpler to others but simpler and uses recursion.
* 1. Check if array (directly from Class).
* a.) array then cast to Object[] (no need of Class.cast()), loop through the dimension calling func recursively
* b.) not array (assuming String) then print it out.
* That's it!
*/

public class PrintNDimensionArray {

public static void printNDimensionArray(Object o) {
if (o == null) {return;}

// if array then loop thru and recursive call
Class cls = o.getClass();
if (cls.isArray()) {
Object[] a = (Object[]) o;
for (Object o2 : a) {
printNDimensionArray(o2);
}

} else { // print out value
System.out.println(o);
}

}

public static void main(String[] args) {
// create 3-D array
String a[][][] =
{
{
{ "a", "b" },
{ "c", "d" },
{ "e", "f" }
},
{
{ "g", "h" },
{ "i", "j" },
{ "k", "l" }
},
};

printNDimensionArray(a);
}

}

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

The problem is that we should be able to print out any n-dimensional array, not assuming a specific dimension. I think any n-dimensional array is expressed as just contiguous memory in memory. So we can express any n-dimensional array as follows.

// C++
struct md_arr {
	vector<size_t> dim;
	int* data;
};

With this representation, I can print out n-dim array as follows using suffix product.

#include <iostream>
#include <vector>
#include <functional>
#include <memory>

using namespace std;

struct md_arr {
	vector<size_t> dim;
	int* data;
};

auto print_md_arr = [](const md_arr& arr) {
	vector<size_t> sprd(arr.dim.size());
	sprd[sprd.size()-1] = 1;
	for (int i = sprd.size() - 2; i >= 0; --i) {
		sprd[i] = sprd[i+1] * arr.dim[i+1];
	}
	function<void(size_t, size_t)> impl = [&](size_t i, size_t start) {
		if (i >= arr.dim.size()) {
			cout << arr.data[start] << " ";
			return;
		}
		for (size_t j = 0; j < arr.dim[i]; ++j) {
			impl(i+1, start + j * sprd[i]);
		}
		cout << endl;
	};
	impl(0, 0);
};

int main() {
	md_arr ma = {
		vector<size_t>{2,2,2,3},
		new int[24]{1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6}
	};
	print_md_arr(ma);
	return 0;
}

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

updated one of the above java code:

public class PrintArray {

	public static void main(String args[]) {
		String a[][][] = 
			{ 
				{
					{ "a", "b" },
					{ "c", "d" },
					{ "e", "f" } 
				},
				{ 
					{ "g", "h" },
					{ "i", "j" },
					{ "k", "l" }
				}
			};
		printArray(a);
	}

	public static void printArray(Object obj) {
		if (obj.getClass().isArray()) {
			Object[] obj1 = (Object[])obj;
			System.out.print("{");
			for (Object obj2 : obj1) {
				printArray(obj2);
			}
			System.out.print("}");
		} else {
			System.out.print("|" + obj+ "|");
			
		}
	}
}

- Anonymous February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is what we do in C#

public static void PrintArray(Array arr)
{
    if(arr.Rank > 1)
    {
        foreach(Array i in arr)
        {
            PrintArray(i);
        }
    }
    else
    {
        foreach(int i in arr)
        {
            Console.WriteLine(i);
        }
    }
}

This will print each array right from their distance from 0th element.
I think a whole array is stored at the same place in the memory, that is the reason, we are able to access any element of an array directly by indexing. But when array dimensions increases, I think it will store each subarray together, by that, you can say if our array was a 4 dimension array, then, it will store each two dimensions array together.

- sonesh April 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Modifying one of the above solution:

public class PrintArray {

	public static void main(String args[]) {
		String a[][][] = 
			{ 
				{
					{ "a", "b" },
					{ "c", "d" },
					{ "e", "f" } 
				},
				{ 
					{ "g", "h" },
					{ "i", "j" },
					{ "k", "l" }
				}
			};
		printArray(a);
	}

	public static void printArray(Object obj) {
		if (obj.getClass().isArray()) {
			Object[] obj1 = (Object[])obj;
			System.out.print("{");
			for (Object obj2 : obj1) {
				printArray(obj2);
			}
			System.out.print("}");
		} else {
			System.out.print("|" + obj+ "|");
			
		}
	}
}

- Anonymous February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

DFS for array? Can you please explain?

- Saurabh December 27, 2014 | 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