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?

1 comment:

  1. Hmm, Puzzle OK, But I had solved this Puzzle in 1999 when I was studying FoxPro. Do you have any other puzzles ? :) :)

    ReplyDelete