Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: In-Person




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

I will go with Queue Implementation.

- Nitin July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can u let me know how queue will help to solve this problem

- gaurav September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

he is right, Queue will be the most suitable data structure for this, the person requested first should get connection first, Also its does not arrive the starvation problem.

- ajayv October 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

we can use two arraylists one for total available connections and other for connections being in use

of course these must be in a singleton object

- bk August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

First of all Connection Pool mechanism uses singleton pattern so we should not use interface otherwise will not get singleton behavior using getConnection method.
Simply ArrayList can be used for connection pooling.
public class ConnectionPool {
private static List<Connection> pool = null;
private static int available = 0;
private ConnectionPool() {}

public static Connection getConnection() {
if (pool == null) {
pool = new ArrayList<Connection>();
for (int i = 0; i < 3; i++) {
try {
pool.add(new Connection());
available++;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if (pool.size() > 0) {
available--;
return pool.remove(available);
} else {
return null;
}
}

public static void put(Connection c) {
pool.add(c);
available++;
}
}

- Krishna Singh September 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we should use HashMap

- Anonymous July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure how does HashMap solve the problem. "Connection Pool" behaviour?
Note: *I don't need this question answered*

- SoMiE July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Java 1.7
public class ConnectionPool implements Pool
{
    private ConcurrentLinkedDeque<Connection> cld = new ConcurrentLinkedDeque<Connection>();
    public Connection get() {
        return cld.pop();
    }
    public void put(Connection c) {
        cld.push(c);
    }
}

- ed July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think ConnectionPool should be singleton class.

- yolo July 12, 2013 | Flag


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