Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

August 25, 2009

Three ants puzzle

There are three ants at the three corners of a regular
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?
2 comments:

Anonymous said...

25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25

Secret Squïrrel
March 9, 2008 6:34 AM
Neeraj said...

There are two directions tomove for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25


Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html

Three ants puzzle

There are three ants at the three corners of a regular
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?

25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25

There are two directions to move for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25


Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html

How many such points on the earth

Q Find all the points on the globe from which if you travel 1km south, then 1 km east and then 1 km north; you end up at the same point from which you started.

Hints:
1. There is an infinite amount of such points.
2. The answer is 100% true, no tricks - so don't waste your time searching for any.


Ans : Half the answer is this:
There is some place on the globe, where the perimeter of the earth is exactly 1 km. If you start walking from any point which is 1 km north of that ring, and walk 1 km south, you'll end up on the 1 km perimeter. You'll then go 1 km east which will take you all the way around the globe and then you'll go 1 km north and end up in the starting point.

This is the answer which many people give, but it's not full. It gives us a stip (or a ring) of points with this property, but there is also an infinite amount of such strips (or rings):
The full answer, as given by UnToha, is that the perimeter of the walk around the globe doesn't have to be 1 km. It can be any factor of 1 km so that when walking 1 km east, you'll walk some K times around the globe (where K has to be an integer), thus making several complete trips around the globe. This gives us an infinite amount of rings, each having an infinite amount of points, all of which have this strange property.

reference : http://neworder.box.sk/forum/viewtopic.php?id=40242

java.lang.String

Q : If we create a custom class String in the directory structure like java.lang and import it in our class. Which version of String class would be imported : Custom class or Standard Library class ??

Ans : Java has been coded such that it would pick its Standard library class only even if we define class with same name and same package structure.
So the class might compile but would give a runtime exception if we try to invoke a method which we defined in our custom class n which is not present in the std library class.

e.g.
package java.lang;

public final class String {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

public String toString(){
return "Custom Java Class";
}

public static void myMethod(){
System.out.println("My custom java class");
}

}


and a class

import java.lang.String;
public class StringTesting {

/**
* @param args
*/
public static void main(String[] args) {
String str = new String();
java.lang.String.myMethod();
}

}


the code will compile coz at the compile time , its referring to the custom java class , but at run time, the standard java class would be loaded and since it doesnt have myMethod(), it would generate run time exception.

This is because when our custom java.lang.String class is referenced, the class loader checks if this has already been loaded and it finds that a class with the same fully classified class name has already been loaded (which is the one provided by java) and so does not load the custom java.lang.String class

August 24, 2009

Command to see decompiler output

javap -s -verbose -l com.xyz.collection.IteratorException >op.txt

Nice Question

class Test {

static int m(byte a, int b) { return a+b; }

static int m(short a, short b) { return a-b; }

public static void main(String[] args) {

System.out.println(m(12, 2));// compile-time error

}

}

A very good interview question (OverridingTest ) --> Static Polymorphism

Q: What would be the output of the following program ?
public class OverridingTest {



//~ Methods --------------------------------------------------------------------------------------------------------



public static void abc(Object str) {

System.out.println("Object");

}



public static void abc(String str) {

System.out.println("String");

}



public static void main(String[] args) {



// Example of Static polymorphism

abc(null);

abc((Object) null);



String[] str = { null, null, null, null };



for (String st : str) {

abc(st);

}

}

}

Ans : String

Reason : http://www.javafaq.nu/java-article383.html