## Goldman Sachs Interview Question for Developer Program Engineers

• 0

Country: India
Interview Type: In-Person

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

My solution:

``````def arr_print(arr):
for i in arr:
if type(i) == type([]):
arr_print(i)
else:
print(i)``````

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

``````var reducer = function(arr){
return arr.reduce((a, b) => a.concat(b), []).reduce((x, y) => x.concat(y), []);
};

console.log(reducer([1, 5, 8, 9, 10, [24, 20, [39, 48], 89], 105, 99]));``````

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

The JavaScript way will be

``````var reducer = function(arr){
return arr.reduce((a, b) => a.concat(b), []).reduce((x, y) => x.concat(y), []);
};

console.log(reducer([1, 5, 8, 9, 10, [24, 20, [39, 48], 89], 105, 99]));``````

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

``````class Poly(object):
arr = []
str = ""
def parseArr(self,arr=None):
if (arr is None):
arr = self.arr
for i in arr:
if (isinstance(i,list)):
self.parseArr(i)
else:
self.str = "%s%s," % (self.str,i)

def printStr(self):
str = self.str[:-1]+'.'
print str
if __name__ == '__main__':
x = Poly()
x.arr = [1,5,8,[9,10,24,20,[39,48],89],105,99]
x.parseArr()
x.printStr()``````

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

``````class Poly(object):
arr = []
str = ""
def parseArr(self,arr=None):
if (arr is None):
arr = self.arr
for i in arr:
if (isinstance(i,list)):
self.parseArr(i)
else:
self.str = "%s%s," % (self.str,i)

def printStr(self):
str = self.str[:-1]+'.'
print str
if __name__ == '__main__':
x = Poly()
x.arr = [1,5,8,[9,10,24,20,[39,48],89],105,99]
x.parseArr()
x.printStr()``````

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

look into source code of java.util.Arrays#deepToString(Object[] a)

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

For instance, look in to source code for java.util.Arrays.deepToString(Object[])

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

``````public class NestedArrayAlgo {

public static void main(String[] args) {

//[1, 5, 8, 9, 10, [24, 20, [39, 48], 89], 105, 99]

Object[] arraN_1_1 = {39,48};
Object[] arraN_1 = {24,20,arraN_1_1,89};
Object[] arraN = {1,5,8,9,10,arraN_1,105,99};

printNestedArray(arraN);

}

public static void printNestedArray(Object[] NestArray){

for(int i=0;i<NestArray.length;i++){

if(NestArray[i].getClass().isArray()){
Object[] tempArray = (Object[]) NestArray[i];
printNestedArray(tempArray);
}else{
System.out.print(NestArray[i]+",");
}
}
}
}``````

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

In javascript

function reduce(arr){
return arr.reduce(function(farr,element){
return farr.concat(Array.isArray(element)?reduce(element):element)
},[])
}

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

function reduce(arr){
return arr.reduce(function(farr,element){
return farr.concat(Array.isArray(element)?reduce(element):element)
},[])
}

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

``````#include <iostream>
#include <vector>

using namespace std;

class Node {
public:
Node(int val)
{
leaf_ = true;
val_ = val;
}
Node(vector<Node *> const &children)
{
leaf_ = false;
val_ = 0;
children_ = children;
}
~Node()
{
for (Node *c : children_) {
delete c;
}
}
bool leaf_;
int val_;
vector<Node *> children_;
};

void Print (Node *n)
{
if (n) {
for (Node *c : n->children_) {
if (c->leaf_) {
cout << c->val_ << ", ";
} else {
Print(c);
}
}
}
}

int main() {
Node *root = new Node({
new Node(1),
new Node(5),
new Node(8),
new Node({
new Node(9),
new Node(10),
new Node(24),
new Node(20),
new Node({
new Node(39),
new Node(48)
}),
new Node(89)
}),
new Node(105),
new Node(99)
});
Print(root);
cout << "\n";
delete root;
}``````

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

We can use a custom object to store such type of data as depicted below.

``````class NestedArray{
private Object[] e;

NestedArray(Object e){
if(e instanceof Integer) {
this.e = new Integer[] {(Integer)e};
}else if(e.getClass().isArray()) {
this.e = (Object[]) e;
}
}

void printArray(Object[] e) {
for(Object o: e) {
if(o instanceof Integer) {
System.out.println((Integer)o);
}else if(o.getClass().isArray()) {
printArray((Object[]) o);
}
}
}
}``````

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

Python solution:

``````class MultiList:
def __init__( self ):
self.items = []

if isinstance(item, int):
self.items.append( item )
else:
for x in range(len(item)):

arr = [1, 5, 8, 9, 10, [24, 20, [39, 48], 89], 105, 99]

multiList = MultiList()
for x in range(1, len(arr)):

print(', '.join([ str(x) for x in multiList.items ]))``````

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

I solved it with polymorphism defining a super class for a generic element (that can be a single element or an array).

This is my solution in C++:

``````class ListableObject {
public:
virtual void print() const = 0;
virtual ~ListableObject() {}
};

class Vector : public ListableObject {

private:
vector<ListableObject*> v;

public:

Vector(const vector<ListableObject*> &_v):
v(_v) {}

void push_back(ListableObject *o) {
v.push_back(o);
}

virtual void print() const {
cout << "[";
if(v.size() > 0) {
for(int i = 0, l = v.size() - 1; i < l; i++) {
v[i]->print();
cout << ", ";
}
v[v.size() - 1]->print();
}
cout << "]";
}

virtual ~Vector() {

}

};

class Element : public ListableObject {

private:
int value;

public:

Element(const int _value):
value(_value) {}

virtual void print() const {
cout << this->value;
}

virtual ~Element() {

}

};

int main() {

Element e1 = 2;
Element e2 = 3;
vector<ListableObject*> v2 = {&e1, &e2};
Vector innerObject = v2;
Element e3 = 8;
Vector* outerObject = new Vector(vector<ListableObject*>{&innerObject, &e3});
outerObject->push_back(new Vector(vector<ListableObject*>{new Element(18), new Element(25)}));

outerObject->print();

return 0;
}``````

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.

### 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.