Given an array of thousand numbers how many comparison is required to find a number x ?

Q: Given an array of thousand numbers how many comparison is required to find a number x ?
A: Divide array into two everytime and find which side X will fall, repeat this untill found. Could take around 8-10 comparision.


This is also open for better solution...

How to find a word in file most time used ?

Q: How to find a word in file most time used ?
A: Keep words in a HashMap, everytime a new element found add that in map and increase their respective counter or if already available just increase the counter, return the max counter.


Any better solution welcome.....

Number to Word converter

Q: Number to Word converter.
A: Will provide description later ...
public class NumberToWord {

public static void main(String[] args) {
String inputNumber = "1";
System.out.println("Input Number = " + inputNumber);
System.out.println("Words = " + ChangeToWord(inputNumber));
}

public static String ChangeToWord(String num) {

int inputNumber = Integer.parseInt(num);

if (!(inputNumber > 0 && inputNumber < 99999))
return "Aukat se bahar mat ja !!!";

String oneTensPlace;
String hundredPlace;
String tenThousandPlace;
char[] inputNumberArray = { '0', '0', '0', '0', '0' };
char[] dummyArray = num.toCharArray();
for (int i = inputNumberArray.length - 1, j = dummyArray.length - 1; i >= 0; i--) {
inputNumberArray[i] = dummyArray[j];
j--;
if (j < 0)break;
}
// There are some vars initialized but unutilized for some cases, which
// can further be enhanced
oneTensPlace = "" + inputNumberArray[3] + inputNumberArray[4];
hundredPlace = "" + inputNumberArray[2];
tenThousandPlace = "" + inputNumberArray[0] + inputNumberArray[1];

StringBuilder inWords = new StringBuilder();
if (Integer.parseInt(tenThousandPlace) > 0) {
inWords.append(HandleTwoDigit(tenThousandPlace) + "thousand ");
}
if (Integer.parseInt(hundredPlace) > 0) {
inWords.append(NumWordMap(hundredPlace) + "hundred ");
}
if (!inWords.toString().isEmpty() && Integer.parseInt(oneTensPlace) > 0)
inWords.append("and ");
if (Integer.parseInt(oneTensPlace) > 0) {
inWords.append(HandleTwoDigit(oneTensPlace));
}
return inWords.toString();
}

public static String HandleTwoDigit(String num) {

String word = null;
int i = Integer.parseInt(num);

if (i <= 20) {
word = NumWordMap(num);
} else {
char[] inputNumberArray = num.toCharArray();
String onesPlace = "" + inputNumberArray[1];
String tensPlace = inputNumberArray[0] + "0";
if (Integer.parseInt(onesPlace) > 0)
word = NumWordMap(tensPlace) + NumWordMap(onesPlace);
else
word = NumWordMap(tensPlace);
}

return word;
}

public static String NumWordMap(String num) {

String word = null;
int i = Integer.parseInt(num);

String[] words = { "Zero ", "One ", "Two ", "Three ", "Four ", "Five ",
"Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ",
"Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ",
"Seventeen ", "Eighteen ", "Nineteen ", "Twenty " };
String[] higherWords = { "Zero ", "Ten ", "Twenty ", "Thirty ",
"Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };

if (i > 20)
word = higherWords[i / 10];
else
word = words[i];

return word;
}
}

Rearrange array in -ve, +ve, -ve of any given array in Ologn time

Q: Rearrange array in -ve, +ve, -ve of any given array in Ologn time complexity.
A: This solution is provided by Jagadeesh Tellakula 


Will provide short explanation for this later ....

public class RearrangeArray {

static void swap(int []a,int i,int j){
int temp=0;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int findNextNeg(int []a,int negitPos){
//if(a)
if(negitPos==-2)negitPos=-1;
for(int i=negitPos+1;i<a.length;i++){
if(a[i]<0) return i;
}
return -1;
}
static int findNextPos(int []a,int positPos){
if(positPos==-2)positPos=-1;
for(int i=positPos+1;i<a.length;i++){
if(a[i]>=0) return i;
}
return -1;
}
public static void main(String[] args) {
int []a={-1,-2,-3,-4,-5,6,7,8,9,-10,-11,-12,13,-14,-15,-16,-17};
//int leftIndex=0;
int nextNeg=-2;
int nextPos=-2;
nextNeg =findNextNeg(a,nextNeg);
nextPos =findNextPos(a,nextPos);
//System.out.println(a.length+"17"+a[a.length-1]);
System.out.println("nextNeg:"+nextNeg);
System.out.println("nextPos:"+nextPos);
    for(int i=0;i<a.length;i++){
    
     if(i%2==0){
     if(a[i]>=0){
     continue;
     }
     else{
     if(nextPos!=-1){
     swap(a,nextPos,i);
     nextPos=findNextPos(a, nextPos);
     }else break;
     }
     }
     if(i%2==1){
     if(a[i]<0){
     continue;
     }
     else{
     if(nextNeg!=-1){
     swap(a,nextNeg,i);
     nextNeg=findNextNeg(a, nextNeg);
     }else break;
    
     }
     }
     if(nextNeg<=i)nextNeg=i;
     if(nextPos<=i)nextPos=i;
    
     for(int j=0;j<a.length;j++){
         System.out.print(a[j]+" ");
         }
         
         System.out.print(" nextNeg:"+nextNeg);
     System.out.print(" nextPos:"+nextPos);
     System.out.print("\n"); 
    }
    for(int j=0;j<a.length;j++){
         System.out.print(a[j]+" ");
         }System.out.print(":last\n");
}
}

Finding Kth largest element in a array

Q: Find the Kth largest element in a array without using additional memory space.
A: Use Array’s 0 to Kth element as variable and match with every value of the array in one iteration and sort 0 to Kth array everytime. Will provide details later on this.

Find largest possible number formed by elements of array.

Q: Given an array of elements find the largest possible number that can be formed by using the elements of the array. Ex:
10, 9 - 910
2, 3, 5, 78 - 78532.
A: Its




Reference - http://www.careercup.com/question?id=12053663

Find lighter ball out of 9 in 2 tries.

Q: There are nine balls, one of which is lighter. How would you find the lighter ball in two tries?
A: In the first step, divide the balls into sets of three and weigh all three sets. One of those sets will be lighter, so now you know it's one of those three. In the second step, you weigh two of the three balls. If one weighs less, you now that's the light ball. If they're equal, you know it's the ball you didn't weigh. That's it — done in two steps!
Reference - http://www.cnbc.com/id/41854610/

Why are manhole covers round?

Q: Why are manhole covers round?
A: Of course, there isn't always a right answer.This is a favorite question asked at Microsoft. Brian Groth, an advertising executive at Microsoft, who says he's asked this question in an interview, admits that he doesn't actually know the right answer — he asks it just to loosen up and have some fun!

For the record, the scientific answer is "so they won't fall in the hole." A square cover, if tilted, could fall into a square hole. Other possible answers include "because they're heavy and that way you can roll them" or, if you're feeling cheeky, "because the hole is round!"

Reference - http://www.cnbc.com/id/41854610/