Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

i = 1;
int count = getCount(ds[i++]);
while(count < 100 && i < 6) {
	count += getCount(ds[i++])
}
return count;

- Anonymous July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How is this code faster in any way? Its just a loop and same old conditionns. Am I missing something

- Rahul December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rahul, the code will reduce the unnecessary checks if the count goes about 100.

- zg8006 January 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

//here the data sources are the ds1, ds2 , ds3  so on and so-forth
ArrayList <DataSources> datasourceList = new ArrayLIst<DataSources>
int i=-1;
while( count<100 )
{
count = count + getCount(datasourceList .get(++i));

}

- mahanthopensource@gmail.com mahanth July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is an object oriented way of solving the problem , really good solution *** nice one . no if or else . the code will be faster than other codes

- Sharan July 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The exit condition is not correct. There needs to be one more condition to check if i< datasourceList.length in the while block

- Anonymous October 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think condition can be reverse and we can return from that:

int count = getCount(ds1);

if(count >= 100 )
return count;

count = count + getCount(ds2);

if(count >= 100 )
return count;

count = count + getCount(ds3);

if(count >= 100 )
return count;

count = count + getCount(ds4);

if(count >= 100 )
return count;

retrurn count = count + getCount(ds5);

- Krishna Kumar July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

int getCount(){
		
		final int dsCount = 5;
		List<Future<Integer>> results = new ArrayList<>();
		ExecutorService TH_POOL = Executors.newFixedThreadPool(dsCount);
		
		int count = 0;
		
		try{
			for( int i = 0; i < dsCount; i++ ){
				int dsNo = i+1;
				results.add( TH_POOL.submit( ()->getCount(dsNo) ) );
			}
			
			for( Future<Integer> singleResult : results ){
				try{
					count += singleResult.get();
				}
				catch( Exception ex ){
					ex.printStackTrace();
				}
				
				if( count >= 100 ){
					return count;
				}
			}
		}
		finally {
			TH_POOL.shutdown();
		}
		
		return count;
	}

- m@}{ July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

you can write code like this,i think it will give best performance wise.if the count value is initially more then 100 then

count=109

it will execute only one IF condition in best case.

int count= getCount(ds1);
		return count<100?
	count=count+getCount(ds2) < 100?
	count=count+getCount(ds3) < 100?
	count=count+getCount(ds4) < 100?
	count=count+getCount(ds5):count:count:count:count;

}

- yugandharr147 July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

What a retard... assignment statement cannot be present in the ternary operator.

- Murali Mohan July 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Fetch all the counts from all the data sources parallel, say you store them in an array dsCount[5];
2. Now, add one by one from dsCount to the variable count and go until count < 100;

- TitatL August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count = 0;
        String ds = "ds";
        int i=1;
        do {
            count += getCount(ds+(i++));
            i++;
        }while (count < 100);

- Anonymous August 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public class ParallelCount {
    static final int SINGLE_SLEEP = 50;
    static int getCount(String s) {
        try {
            Thread.sleep(SINGLE_SLEEP);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return (int) (Math.random()*100.);
    }

    public static void main(String[] args) throws Exception{
        boolean isMultiThreaded = true;
        for(int i=0;i<10;i++) {
            long start = System.currentTimeMillis();
            if (!isMultiThreaded) {
                int count = getCount("ds1");
                if (count < 100) count = count + getCount("ds2");
                if (count < 100) count = count + getCount("ds3");
                if (count < 100) count = count + getCount("ds4");
                if (count < 100) count = count + getCount("ds4");
            }else {
                final int[] counts = new int[5];
                Thread[] t = new Thread[5];
                for (int j = 0; j < 5; j++) {
                    final int finalJ = j;
                    t[j] = new Thread() {
                        public void run() {
                            counts[finalJ] = getCount("ds" + finalJ);
                        }
                    };
                    t[j].start();
                }
                int count = 0;
                for (int j = 0; j < 5; j++) {
                    t[j].join();
                    count += counts[j];
                    if (count >= 100) break;
                }
            }
            System.out.println("Total time = " + (System.currentTimeMillis() - start));
        }
    }
}

The total time is the time it takes to make one call even if you dont need all the results. Output (from multithreaded run) :
Total time = 53
Total time = 54
Total time = 54
Total time = 53
Total time = 53
Total time = 54
Total time = 52
Total time = 53
Total time = 52
Total time = 52

- petko.ivanov September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was wondering, you use thread to run the getCount() function, however the best case is once the count>100, we do not need to call getCount() anymore if getCount() is a huge function taking hours to run... will multi thread be better than just using if-condition to stop and return the result? I am new to multi thread, so just wanna know more about how the multithread in this case will help.

- Mandy December 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

count = ds1+ds2+ds3; 
if (count < 100) 
{ 
count = count + ds4; 
if (count < 100) 
count = count + ds5; 
} 
else 
{ 
count = count - ds3; 
if (count > 100) 
count = count - ds2; 
}

- postforshubham October 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count = getCount(ds1);

if(count<100){
count = count + getCount(ds2);
if(count<100){
count = count + getcount(ds3);
if(count<100){
count= count+getCount(ds4);
if(count<100){
count=count+getcount(ds5);
}
}
}
}

- Anonymous November 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Interviewer must be looking for multithreading.
Fetch count from all sources in parallel.
Bonus points: If machine has less than 5 cores (for 5 data sources), say 4 cores, prioritize so that first 4 cores get the highest priority. Not sure how to do this in code.

- im.code.warrior January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Interviewer must be looking for multithreading.
Fetch count from all sources in parallel.
Bonus points: If machine has less than 5 cores (for 5 data sources), say 4 cores, prioritize so that first 4 cores get the highest priority. Not sure how to do this in code.

- im.code.warrior January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Interviewer must be looking for multithreading.
Fetch count from all sources in parallel.
Bonus points: If machine has less than 5 cores (for 5 data sources), say 4 cores, prioritize so that first 4 data sources get the highest priority. Not sure how to do this in code.

- im.code.warrior January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ArrayList ds = dataSourceArray;
int dLength = dataSourceArray.size();
int count = 0;
for(int i=0; i< dLength; i++)
{
if(count<100)
{
count = count + getCount(ds.get(i));
if(count >= 100)
break;
}
}
As we don't need to check remaining conditions if count >= 100, we can break the loop

- chaitanya.aripaka87 February 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assuming getting count from a data source is IO bound, first spawn 5 threads to get counts from each source, join with all of them and do the calculation.

- sura December 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

Spawn 5 threads parallel on the data sources and get the total count.

- HardCode July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
4
of 4 votes

I think he meant :
keep 'count' shared among 5 threads
each thread will only add a number in count if total does not exceed 100
this looks perfectly valid to me because each data source may may take its own time to return the value.

- Annonymous August 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

This can be done in the following manner by applying goto label in this way:

int count,i=1;
label ptr:
if(count<100&&i<=5)
{	count=count+getCount(dsi);
	i++;
	goto ptr;	
}

- vgeek July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes.we can write like that .but that code holding extra space for "i" variable and one more thinks is your using "goto" and "label" statements ,now this two are not re-commanded in java.but the above code reduce the program code.

- yugandharr147 July 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int count = getCount(ds1);
if ( count < 100 )
count += getCount(ds2) + getCount(ds3) + getCount(ds4) + getCount(ds5)

- ravi July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

count = ds1+ds2+ds3;
if (count < 100)
{
count = count + ds4;
if (count < 100)
count = count + ds5;
}
else
{
count = count - ds3;
if (count < 100)
count = count - ds2;
}

with this approach you are only doing 2 if checks and 5 add/subtract options instead of 4 if checks and 5 adds.

- ac22 September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

count = ds1+ds2+ds3;
if (count < 100)
{
count = count + ds4;
if (count < 100)
count = count + ds5;
}
else
{
count = count - ds3;
if (count < 100)
count = count - ds2;
}

with this approach you are only doing 2 if checks and 5 add/subtract options instead of 4 if checks and 5 adds.

- ac22 September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

count = ds1+ds2+ds3;
if (count < 100)
{
count = count + ds4;
if (count < 100)
count = count + ds5;
}
else
{
count = count - ds3;
if (count < 100)
count = count - ds2;
}

with this approach you are only doing 2 if checks and 5 add/subtract options instead of 4 if checks and 5 adds.

- ac22 September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

count = ds1+ds2+ds3;
if (count < 100)
{
count = count + ds4;
if (count < 100)
count = count + ds5;
}
else
{
count = count - ds3;
if (count < 100)
count = count - ds2;
}

with this approach you are only doing 2 if checks and 5 add/subtract options instead of 4 if checks and 5 adds.

- ac September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote
{{{ int count = getCount(ds1); if(count < 100 ) count = count + getCount(ds2); else return count; if(count < 100) count = count + getCount(ds3); else return count; if(count < 100) count = count + getCount(ds4); else return count; if(count < 100) count = count + getCount(ds5); else return count; - Max July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't get why this answer is downvoted. Could the voter please explain.

- kr.neerav July 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kr.neerav: Idiots on this site who are clueless. They think that throwing threads at everything will solve their perfomance problems.

- Anonymous July 02, 2014 | 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