Tip Details Add a Related Tip

Rate as: Positive Negative

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:

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

Comments on this Tip

Add a Comment
I actually like when I get asked this question since it tells me something about the person asking the question. If they ask a lot of questions like this, I know it's a place I don't want to work since it'll be full of people who care about trivial matters.
– Added by an anonymous user on Aug 20, 2007 at 8:55 am
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Using functions in conditionals in Smarty

Categories: php programming Smarty web
100% positive, 3 comments - last added on May 21, 2008 at 1:36 pm
– Tip added by rlansky on Aug 10, 2007 at 1:02 pm

How to Find a Random Date in PHP

25% positive, 0 comments
– Tip added by Walkere on Feb 07, 2008 at 8:25 pm

Something to consider before switching platforms

100% positive, 0 comments
– Tip added by an anonymous user on Sep 25, 2007 at 7:31 pm

Best PHP reference

Categories: PHP coding programming
100% positive, 0 comments
– Tip added by rlansky on Mar 07, 2007 at 2:38 pm

Redirecting in PHP

100% positive, 0 comments
– Tip added by an anonymous user on Jul 17, 2008 at 7:27 am

Copyright date on sites

Categories: web programming php Smarty
100% positive, 0 comments
– Tip added by an anonymous user on Mar 11, 2008 at 8:14 am

How to Syndicate an RSS Feed On Your Site

100% positive, 0 comments
– Tip added by Walkere on Feb 09, 2008 at 5:48 pm

How to Send E-Mail in PHP: A “Share This” Form

33% positive, 0 comments
– Tip added by Walkere on Feb 06, 2008 at 6:45 pm

How to Create a Random Password in PHP

100% positive, 0 comments
– Tip added by Walkere on Feb 03, 2008 at 9:14 am

Referencing methods in PHP usort

Categories: php coding programming
no ratings, 0 comments
– Tip added by an anonymous user on Jun 13, 2008 at 11:18 am

CakePHP: Working with Associations

no ratings, 0 comments
– Tip added by an anonymous user on May 08, 2008 at 4:09 am

File upload progress meters in PHP

Categories: programming web 2.0 php
no ratings, 0 comments
– Tip added by an anonymous user on Jun 16, 2007 at 5:03 pm

Single versus double quotes in PHP

Categories: PHP coding programming
no ratings, 0 comments
– Tip added by Marcos84 on Mar 31, 2007 at 8:00 pm

Learn By Doing

100% positive, 0 comments
– Tip added by an anonymous user on Aug 25, 2008 at 2:00 am

Matching any character in a javascript regular expression

100% positive, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 9:06 am

Submitting web forms without buttons

100% positive, 1 comment - last added on Aug 06, 2008 at 1:26 pm
– Tip added by an anonymous user on Aug 06, 2008 at 8:16 am

Random number generation

100% positive, 0 comments
– Tip added by an anonymous user on Aug 05, 2008 at 6:49 am

attaching events to nodes in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 11:23 am

Figuring out shortcuts in Eclipse

no ratings, 0 comments
– Tip added by an anonymous user on Aug 13, 2008 at 3:17 pm

animating elements with jQuery

no ratings, 0 comments
– Tip added by an anonymous user on Aug 04, 2008 at 1:46 pm