Goldman Sachs Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

package com.home.careercup;

public class Doubleton {
    private Doubleton() {
    }

    private static int call = 0;
    private static Doubleton[] instances = new Doubleton[]{
            new Doubleton(), new Doubleton()
    };

    public static Doubleton getInstance() {
        return instances[call++ % 2];
    }

    public static void main(String[] args) {
        Doubleton d1 = Doubleton.getInstance();
        Doubleton d2 = Doubleton.getInstance();
        for (int i=0;i<10;i++){
            if ( i%2 ==0)
                System.out.println(d1 == Doubleton.getInstance());
            else
                System.out.println(d2 == Doubleton.getInstance());

        }
    }
}

- Makarand September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Makarand, I believe your code is faulty, and gravely so. Read more about :
[ journaldev.com/171/thread-safety-in-java-singleton-classes-with-example-code ]

- NoOne September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Doubleton {

private static volatile Doubleton _instance1;
private static volatile Doubleton _instance2;

private static int count = 0;

private Doubleton() {

}

public static Doubleton getInstance() {
if (count++ % 2 == 0) {
if (_instance1 == null) {

synchronized (Doubleton.class) {
if (_instance1 == null) {
_instance1 = new Doubleton();
}
}
}

return _instance1;
} else {
if (_instance2 == null) {

synchronized (Doubleton.class) {
if (_instance2 == null) {
_instance2 = new Doubleton();
}
}
}

return _instance2;

}
}
}

- Kishan September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If two calls getinstance occur at exactly the same time, count++ would be called twice immediately and both those calls will have the same instance returned.

I believe the count++ would also need to be within the lock.

- noname September 08, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@NoOne I notice that the variable 'call' needs protection. So making getInstance() synchronized should fix this. Thanks for pointing this out.

- Makarand September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <stdio.h>

class multiTon
{
private:

static std::vector<multiTon *> obj;

int id;

static int counter;

public:

multiTon(int Identity)
{
id = Identity;
}

~multiTon()
{
}

static multiTon * getInstance()
{
return obj[counter++ % 2];
}

int getId()
{
return id;
}
};

int multiTon::counter = 0;
std::vector<multiTon *> multiTon::obj {new multiTon(0), new multiTon(1)};

int main()
{
printf ("%d \n",multiTon::getInstance()->getId());
printf ("%d \n",multiTon::getInstance()->getId());
printf ("%d \n",multiTon::getInstance()->getId());
printf ("%d \n",multiTon::getInstance()->getId());
printf ("%d \n",multiTon::getInstance()->getId());
}

- mahesh g s September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <stdio.h>

class multiTon
{
    private:
    
    static std::vector<multiTon *> obj;
    
    int id;
    
    static int counter;
    
    public:
    
    multiTon(int Identity)
    {
        id = Identity;
    }
    
    ~multiTon()
    {
    }
    
    static multiTon * getInstance()
    {
        return obj[counter++ % 2];
    }
    
    int getId()
    {
        return id;
    }
};

int multiTon::counter = 0;
std::vector<multiTon *> multiTon::obj {new multiTon(0), new multiTon(1)};

int main()
{
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
}

- mahesh g s September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <stdio.h>

class multiTon
{
    private:
    
    static std::vector<multiTon *> obj;
    
    int id;
    
    static int counter;
    
    public:
    
    multiTon(int Identity)
    {
        id = Identity;
    }
    
    ~multiTon()
    {
    }
    
    static multiTon * getInstance()
    {
        return obj[counter++ % 2];
    }
    
    int getId()
    {
        return id;
    }
};

int multiTon::counter = 0;
std::vector<multiTon *> multiTon::obj {new multiTon(0), new multiTon(1)};

int main()
{
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
    printf ("%d \n",multiTon::getInstance()->getId());
}

- Anonymous September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package designpattern;

public class DoubleTon {

final static private DoubleTon instance1=new DoubleTon();
final static private DoubleTon instance2=new DoubleTon();

static private int counter=0;

private DoubleTon(){

}

static public DoubleTon getInstance(){

counter++;
if(counter%2==0){
System.out.println("instance1");
return instance1;
}

return instance2;

}
}

- vijay.lokhande.in September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this the same an object-pool (generalization of a connection pool)?

- kredible September 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Car {

    private static Car instance = null;

    private Car() {}

    public static Car getInstance() {
        if (instance == null) {
            synchronized(Car.class) {
                if (instance == null) {
                    instance = new Car();
                }
            }
        }
        return instance;
    }
}

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

public class Car {

    private static Car instance = null;

    private Car() {}

    public static Car getInstance() {
        if (instance == null) {
            synchronized(Car.class) {
                if (instance == null) {
                    instance = new Car();
                }
            }
        }
        return instance;
    }
}

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

public class Car {

    private static Car instance = null;

    private Car() {}

    public static Car getInstance() {
        if (instance == null) {
            synchronized(Car.class) {
                if (instance == null) {
                    instance = new Car();
                }
            }
        }
        return instance;
    }
}

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

public class Car {

private static Car instance = null;

private Car() {}

public static Car getInstance() {
if (instance == null) {
synchronized(Car.class) {
if (instance == null) {
instance = new Car();
}
}
}
return instance;
}
}

- Prajay December 15, 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