Showing posts with label Puzzles. Show all posts
Showing posts with label Puzzles. Show all posts

Saturday, June 5, 2010

Common Interview Puzzle 1

This is a common puzzle which is asked in interviews. This is simple, but tricky.
Let us try this.

You have two variables say a & b. Let a = 3 and b = 5. Swap the values of a & b without using a third variable. Interesting?? Let us try the answer,

Step 1: a = a + b   //Now a = 8 & b = 5
Step 2: b = a - b   //Now a = 8 & b = 3
Step 3: a = a - b   //Now a = 5 & b = 3

Simple?? Is'nt it?