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));
		
	}

}

- enthusiastic techie January 27, 2021 | Flag Reply
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;
}

}

- Anonymous January 27, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will count all nodes not cycle nodes!

- Anonymous February 01, 2021 | Flag
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;
    }

- kirikanjoroge January 28, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will just count all nodes not length of cycle!

- Above February 01, 2021 | 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