Posted by: filchiprogrammer on: December 9, 2006
I am making a post about these usual programming trick questions and give out their solutions because, I really want to show you how simple it could be and advise you that you do not “overthink” it. Oh, I am not giving you exactly in a programming code, but maybe just something like a pseudo-code.
If you have other trick questions that you might want to share, please post it as a comment.
Problems:
1. ) How do you swap two integer values from two variables without using another variable?
2. ) How do you convert an integer ranging from 0 to 999,999,999 to words?
Solutions:
1.
a = a + b;
b= a – b ;
a = a – b ;
2. Note: You’ll have to do this in divisions(functions) to parse a number and convert it to to words. And In each division you have to know if they are in the millions, thousands and ones. (Another Note: I heard from someone that this type of trick question may have a slight variation like, giving out their conversion to Roman Numerals.)
I also tried this in PHP, and you can try the live demo
If(num >=1000000 && num1000 && num1 && num
Run this sample php code and see the output. Can any one explain this kind of behavior of php operators??
// code is here – i think site removed my php tags and script within tags.
$x = “08″;
$y = 08;
$z = ” 47″;
$x +=15;
$y +=”15″;
$z +=11;
echo $x;
echo $y;
echo $z;
// output
231558
Well, more intuitive, and short solution to the first one would be:
list($a, $b) = array($b, $a);
It works with objects too, not only scalars.
March 3, 2009 at 1:38 pm
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru