Yahoo Interview Question for Backend Developers


Country: United States
Interview Type: Written Test




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

Question sounds to be tricky

- DATA April 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1- Compare current and next element in array , if current is greater than next then
a- if the diff between these two numbers is 1 then swap
b- else set boolean flag false and break the loop as it can not be sorted
2- If flag is true then print yes else print no

time complexity - O(n)

for(int i=0; i < a.length -1 ;i++){
	                
	                if(a[i] > a[i+1] ){
	                	if(a[i] - a[i+1] == 1){
	                		swap(a,i,i+1);
	                	}else {
	                		flag = false;
	                		break;
	                	}
	                    
	                }
	                
	                
	            }
	            
	            if(flag){
	            	System.out.println("Yes");
	            }else{
	            	System.out.println("No");
	            }

- azambhrgn April 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool flag = true;
        for(int i=1;i<n;i++){
            if(a[i]-a[i-1]==1 || a[i-1]-a[i]==1){
                if(a[i]<a[i-1]) SWAP(a[i], a[i-1]);
            }
            if(a[i]<a[i-1]){
                flag= false;
                break;
            }
        }
        flag?cout<<"Yes":cout<<"No";

- Sindhura Katta April 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done the following way

bool flag = true;
        for(int i=1;i<n;i++){
            if(a[i]-a[i-1]==1 || a[i-1]-a[i]==1){
                if(a[i]<a[i-1]) SWAP(a[i], a[i-1]);
            }
            if(a[i]<a[i-1]){
                flag= false;
                break;
            }
        }
        flag?cout<<"Yes":cout<<"No";

- Anonymous April 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var output = "true";
        for(var counter = 1; counter<a.length; counter++){
            if((a[counter]-a[counter-1])== -1){
                a[counter] = a[counter] + a[counter-1];
                a[counter-1] = a[counter] - a[counter-1];
                a[counter] = a[counter] - a[counter-1];   
            }
            if(a[counter] < a[counter-1]){
                output = "false";
                break;
            }
        }
        console.log(output);

- vshasrani May 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(n<(length(a)-1)){
  n = 0
  for (i in 1:(length(a)-1)){
    print(i)
    if (a[i]>a[i+1]) {
      ai_temp = a[i] #store a[i] actual value
      aj_temp = a[i+1] #store a[i+1] actual value
      a[i] = a[i+1]+1 # modifying a[i] to bring the abs diff to 1
      #now that a[i] has bee modified, abs diff is 1, i can move a[i+1]
      #will double check before calling swap function
      if(abs(a[i]-a[i+1])==1) {
        #swapping a[i] and a[i+1]
        a[i+1] = a[i] #j becomes a[i] modified
        a[i] = aj_temp #i becomes a[i+1] original	
      }
      #now swapping is complete previously a[i] has not become a[i+1]
      #now I need to adjust the value of new a[i+1] back to the original a[i]
      a[i+1] = ai_temp #setting the value of a[i+1] to the original a[i] stored as ai_temp
      
    }else{
      #no need to sort, these two elements are already in increasing order
      n = n + 1
    }
  }
  
}

- If swap is limited to diff=1 then modify the array itself before swapping June 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

the answer is yes:
find the min value and start swapping adj elements.
It is similar to insertion sort

- Hash April 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I mean selection sort.

- Hash April 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Coincidence? I think not

hackerrank.com/contests/w31/challenges/accurate-sorting

- Marcus.Danielsson.86 April 11, 2017 | Flag Reply


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