Interview Question


Country: United States




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

You can sort all of them as float numbers.

- V.Krestyannikov July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But it will print it as 2.0,2.3,5.7,6

I want it to sort like 2,2.3,5.7,6

- Anonymous July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have one more pass to store the number as they were in original array. This would be something like this.

if(ceil(a[i])==floor(a[i]))
    a[i]= int(a[i]);

This won't work if there are numbers like 2.0 , 3.0 in the original array.
You can do one more thing. Sort the number by any method . For all comparison do something like this

if((float)a >float(b)
..

Make all other operations (swapping) with original values

- words&lyrics July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In java we can use java.lang.Number which is parent class of Integer and Float
{{
Number array[]={1,1.2f,4.4f,4};
}}
Java 1.5 or higher version will support auto boxing so no need to do any thing
If i want to store them in sorted order i would use TreeSet<Number, myComparator>()
myComparator would implement Comparator<Number>
If i want to sort unsorted list, i would go for Collections.sort(list, myCompartator);

- pratap.raavi July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In Python, Put the numbers in list and use sort function

- jj August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just another thought. Could we radix sort using bits? Both are 4 bytes right?

- Noobie July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Type your Java code and click the "Run Code" button!
// Your code output will be shown on the left.
// Click on the "Show input" button to enter input data to be read (from
stdin).

public class Interpreter {
public static void main(String[] args) {
// Start typing your code here...
System.out.println("Hello world!");
double[] arr={1,2,3.98,-2,6.545};
findsort(arr);
}

public static String[] findsort(double[] arr)
{
String[] temp=new String[arr.length];

for(int i=0;i<arr.length;i++)
{
temp[i]=new Double(arr[i]).toString();
}
java.util.Arrays.sort(temp);

for(String x:temp){
System.out.println(x);
}


return arr;
}


}

- saurabh August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops made a small mistake..the final return would be return temp and not return arr...also the point of calling the function can be similarly fixed...

- saurabhg August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
    List<Number> list=new ArrayList<Number>(0);
    list.add(10);
    list.add(10.20);
    list.add(9.5);
    System.out.println(list);
    Collections.sort(list,new Comp());
    System.out.println(list);
    
  }
 static class Comp implements Comparator< Number>{

    public int compare(Number o1, Number o2) {
      return ((Float)Float.parseFloat(o1.toString())).compareTo(Float.parseFloat(o2.toString()));
    }
    
  }

try this.

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

public class TestCollection
{
public static void main(String args[])
{
ArrayList<Number> list = new ArrayList<Number>();
list.add(10);
list.add(20);
list.add(1.5f);
list.add(3);
Collections.sort(list,new xyz());
Iterator<Number> iterator = list.listIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
class xyz implements Comparator<Number>
{
public int compare(Number arg0, Number arg1)
{
BigDecimal ar0 = new BigDecimal(arg0.doubleValue());
BigDecimal ar1 = new BigDecimal(arg1.doubleValue());
return ar0.compareTo(ar1);
}
}

- raghava.javvaji September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a comparator:

import java.util.*;

class D implements Comparator<Number>
{



@Override
public int compare(Number arg0, Number arg1) {


float i = (arg0.floatValue() - arg1.floatValue()) ;

if(i==0)
{
return 0;
}

else
{
if (i>0) {return 1;}
else{
if (i< 0){return -1;}
}

}
return 0;

}
}

public class Test1 {


public static void main(String[] args)//generates the exception
{
List s1 = new LinkedList();

s1.add(5);
s1.add(2);
s1.add(3.2);
s1.add(4.6);

Collections.sort(s1,new D());
System.out.println(s1);



}


}

- Sushant September 30, 2012 | 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