One97 Interview Question for Software Engineer / Developers






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

void merge_sort(arr,low,high)
{
    if(high-low == 0)
         return;
    else
       mid = low + (high-low)/2;
       merge_sort(arr,low,mid);
       merge_sort(arr,mid,high);
       merge(arr,low,mid,high);
}

merge(arr,low,mid,high)
{
   int i = 0;
   int j = mid+1;
   int* c;
   c = malloc(sizeof(int)*(high-low));
   k = 0;
   while((i <= mid) && (j <= high))
   {
           if(a[i] < a[j])
           {
                c[k] = a[i]
                i++; 
           }
           else
           {
                c[k] = a[j];
                j++;
           }
        k++;
   }
   
   for(int t = i; t <= mid; t++)
   {
        c[k++] = a[i++]
   }

   for(int t = mid+1; t <= high; t++)
   {
        c[k++] = a[i++]
   }
   for(int i = low; i < high; i++)
   {
        a[i] = c[i];
   }

}

- Messi February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey4476" class="run-this">import java.io.*;
class Array
{
private int[] a;
private int nElems = 0;

Array(int size)
{
a = new int[size];
nElems = 0;
}
public void Insert(int value)
{
a[nElems] = value;
++nElems;
}
public void Display()
{
for(int i = 0; i < nElems; ++i)
System.out.println(a[i]);
}
public void MergeSorting()
{
int[] temp = new int[nElems];
MergeSort(temp,0,nElems-1);
}
public void MergeSort(int[] temp,int low, int high)
{
if(low == high)
return;
else
{
int mid = (low+high)/2;

MergeSort(temp, low, mid);

MergeSort(temp, mid+1, high);

Merge(temp, low, high);
}
}
public void Merge(int[] temp, int low, int high)
{


int mid = (low+high)/2;
int k = mid + 1;
int l = low;

while(low<=mid && k<=high)
{
if(a[low] <= a[k])
temp[l++] = a[low++];
else
temp[l++] = a[k++];
}



while(low <= mid)
temp[l++] = a[low++];

while(k <= high)
temp[l++] = a[k++];

for(int i = 0; i <= high; ++i)
a[i] = temp[i];
}
}
public class MergeSortApp {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("enter the size of the array");
int n = getInt();
Array arr = new Array(n);

while(true)
{
System.out.println("press 'i' to insert value");
System.out.println("press 'd' to display data");
System.out.println("press 's' to merge sort data");
System.out.println("enter your choice");

char ch = getChar();
switch(ch)
{
case 'i':
System.out.println("enter any number");
n = getInt();
arr.Insert(n);
break;
case 'd':
arr.Display();
break;
case 's':
arr.MergeSorting();
break;
default:
break;
}
}

}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}

}</pre><pre title="CodeMonkey4476" input="yes">
</pre>

- Anonymous August 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

ggdsg

- Anonymous May 10, 2011 | 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