Directi Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

import java.util.Arrays;


public class NewSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int data[] = new int[] {20,40,30,90,50,60,91,80,10,71 };
		int len = data.length;
		int s = 4;
		
		// logic
		int minIndex =0;
		logic(data,s,minIndex);
		
		System.out.println(Arrays.toString(data));
		
		
		

	}
	private static int minValue(int[] chars, int sLen, int b) {
		int min = chars[b];
		int ktr = b;
		int ret =b;
		for (;ktr < chars.length&& sLen>=0; ktr++) {
			sLen--;
			if (chars[ktr] < min) {
				min = chars[ktr];
				ret = ktr;
			}
		}
		return ret;
	}
	
	private static void logic(int[] data,int s, int minIndex)
	{
		if( s==0)
			return;
		int idex = minValue(data,s, minIndex);
		//System.out.println(idex - minIndex);
		if( data[idex] == data[minIndex])
		{
			minIndex++;
			logic(data,s,minIndex);
			return;
		}
		int swapDone= idex - minIndex;
		// swap
		int temp = data[idex];
		data[idex] = data[minIndex];
		data[minIndex]= temp;
		
		
		System.out.println("doing swap: "+data[idex] + " "+ data[minIndex]);
		logic(data,s-swapDone,minIndex+1);
		
	}

}

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

#include <iostream>
using namespace std;

int main()
{
    int T,n,l;
    cin>>T;
    for(int t=0;t<T;t++ )
    {
    	cin>>n>>l;
    	int a[n];
    	for(int i=0;i<n;i++)
    		cin>>a[i];
    	for(int i=0;i<n;i++)
    	{
    		int pos=i;
    		for(int j=i+1;j<n;j++)
			{
				if(j-i>l)
					break;
				if(a[pos]>=a[j])
					pos=j;
			}
			if(i!=pos)
			{
				for(int k=pos;k>i;k--)
					swap(a[k],a[k-1]);
				l-=(pos-i);
			}		   		    		
    	}
    	for(int i=0;i<n;i++)
    		cout<<a[i]<<" ";
    	cout<<"\n";
    }
    return 0;
}

- harshit.knit October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>
using namespace std;

void quickSort(int a[],int b[],int start, int end)
{
if(start<=end)
{
int i=start,j=end;
int mid=(start+end)/2 ;
int pivot = a[mid];
while(i<=j)
{
while(a[i]<pivot)
i++;
while(a[j]>pivot)
j--;
if(i<=j)
{
swap(a[i],a[j]);
swap(b[i],b[j]);
i++;
j--;
}


}
quickSort(a,b,start,j);
quickSort(a,b,i,end);
}
}

void function(int a[], int size, int k)
{
int b[size];

for(int i=0;i<size;i++)
b[i]=i;

quickSort(a,b,0,size-1);
int counter=0;

for(int j=0; j<size; j++)
{

if(k>=(b[j]-counter))
{
k-=(b[j]+counter);

swap(a[j],a[counter]);
swap(b[j],b[counter]);
counter++;
}

}

quickSort(b,a,counter,size-1);
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
}

int main()
{
int y;
cin>>y;
while(y)
{
int x,k;

cin>>x>>k;
int a[x];
for(int i=0;i<x;i++)
{
cin>>a[i];
}

function(a,x,k);
y--;
}
}

- Amit August 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is giving wrong output for first input ......

- Rahul Sharma August 23, 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