Goldman Sachs Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
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()
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()
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]+",");
}
}
}
}
#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;
}
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);
}
}
}
}
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 ]))
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;
}
My solution:
- xXx November 25, 2017