Goldman Sachs Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
How is this code faster in any way? Its just a loop and same old conditionns. Am I missing something
//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));
}
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
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);
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;
}
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;
}
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
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.
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;
}
- Anonymous July 02, 2014