Answering a tricky interview question on swapping
When interviewing for a software programming position, a standard interview question that comes up a lot is how to swap two variables in a single step without using a third variable to hold a temporary value. This is a really stupid question to ask people since it really provides no insight into how well people can code, it relies on know a trick that you either know or don't; you can't reason this out. Regardless, if you are going to be interviewing for a software development position, it's a good idea to know an answer for this ahead of time.
If you are programming in PHP, this swapping of variables can easily be handled using the the list command which sets variables from the elements of an array. If $a and $b already exist, you can swap them in a single step like so:
Now go forth and amaze annoying interviewers with your incredible knowledge of computer science.
If you are programming in PHP, this swapping of variables can easily be handled using the the list command which sets variables from the elements of an array. If $a and $b already exist, you can swap them in a single step like so:
list($a, $b) = array($b, $a);
Now go forth and amaze annoying interviewers with your incredible knowledge of computer science.
| Rating: | 100% positive, 1 Vote |
| Categories: | interviews programming php |
| Added: | on Aug 20, 2007 at 7:26 am |
| Added By: | an anonymous user |

