## Goldman Sachs Interview Question for Senior Software Development Engineers

Country: United States
Interview Type: Phone Interview

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

``````public class DetectcycleinArray {

public static int LengthofArraywithoutCycle(int arr[]) {
int length = 1;
int tmp;
if(arr.length ==0)
return 0;
for(int i = 1; i<arr.length; i++) {
for(int j = i+1; j<arr.length; j++) {
if(arr[j]<arr[i]) {
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}

}

for(int i=0;i<arr.length-1;i++) {
if(arr[i]== arr[i+1])
return length;
else
length++;
}

return length;

}

public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 2};
LengthofArraywithoutCycle(a);
System.out.println(LengthofArraywithoutCycle(a));

}

}``````

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

*
* @author kirika
*/
public class ArrayCycle {

public static void main(String[] args){

ArrayCycle ac=new ArrayCycle();

System.out.println("Array Cycle Length: "+ac.detectArrayCycle(new int[]{1,0}));

}

public int detectArrayCycle(int[] a){
int s=a.length;
int c=0;//cycle length
int ni=0; //next arrary idx, start from a[0]
boolean cd=false;//cycle detected
int [] v=new int[s];//array idx visit tracker to calculate
do{
v[ni]=1;//mark a[n] as visited
ni=a[ni];
c+=1;
if(v[ni]==1){
cd=true;
}
}while(ni<s && cd!=true);

return cd?c:-1;
}

}

Comment hidden because of low score. Click to expand.
0

this will count all nodes not cycle nodes!

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

``````public class ArrayCycle {

public static void main(String[] args){

ArrayCycle ac=new ArrayCycle();

System.out.println("Array Cycle Length: "+ac.detectArrayCycle(new int[]{1,0}));

}

public int detectArrayCycle(int[] a){
int s=a.length;
int c=0;//cycle length
int ni=0; //next arrary idx, start from a[0]
boolean cd=false;//cycle detected
int [] v=new int[s];//array idx visit tracker to calculate
do{
v[ni]=1;//mark a[n] as visited
ni=a[ni];
c+=1;
if(v[ni]==1){
cd=true;
}
}while(ni<s && cd!=true);

return cd?c:-1;
}``````

Comment hidden because of low score. Click to expand.
0

This will just count all nodes not length of cycle!

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.

### 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.