Goldman Sachs Interview Question for Developer Program Engineers


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)

- xXx November 25, 2017 | Flag Reply
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]));

- Anonymous November 20, 2017 | Flag Reply
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]));

- holocaust November 20, 2017 | Flag Reply
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()

- Cory Thorson November 21, 2017 | Flag Reply
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()

- Cory Thorson November 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous November 22, 2017 | Flag Reply
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[])

- code_begger November 22, 2017 | Flag Reply
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]+",");
			}
		}
	}
}

- Ashok Rachamalla November 25, 2017 | Flag Reply
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)
},[])
}

- Abhijit Mohanty November 27, 2017 | Flag Reply
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)
},[])
}

- Abhijit Mohanty November 27, 2017 | Flag Reply
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;
}

- Alex November 29, 2017 | Flag Reply
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);
			}
		}
	}
}

- Nitin December 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python solution:

class MultiList:
    def __init__( self ):
        self.items = []
    
    def add( self, item ):
        if isinstance(item, int):
            self.items.append( item )
        else:
            for x in range(len(item)):
                self.add(item[x])
        
    
arr = [1, 5, 8, 9, 10, [24, 20, [39, 48], 89], 105, 99]

multiList = MultiList()
for x in range(1, len(arr)):
    multiList.add(arr[x])
    
print(', '.join([ str(x) for x in multiList.items ]))

- Anthony February 13, 2018 | Flag Reply
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;
}

- DaveRoox November 20, 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