Bloomberg LP Interview Question
Software Engineer / DevelopersTeam: Price history
Country: United States
Interview Type: In-Person
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;
}
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);
}
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;
}
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]);
}
}
}
}
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);
}
}
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;
}
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+ "|");
}
}
}
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.
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+ "|");
}
}
}
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:
- Simmon Yau November 03, 2014