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
Searches: interview programming php question variable

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, 4 comments - last added on Oct 31, 2008 at 2:17 pm
– Tip added by rlansky on Aug 10, 2007 at 1:02 pm

553 sorry, your envelope sender domain must exist errors

100% positive, 0 comments
– Tip added by an anonymous user on Sep 26, 2008 at 3:35 pm

Referencing methods in PHP usort

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

How to Find a Random Date in PHP

20% 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

Determining keywords for your pages

100% positive, 0 comments
– Tip added by an anonymous user on Nov 13, 2008 at 10:28 am

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

PHP strftime errors in Smarty

no ratings, 0 comments
– Tip added by an anonymous user on Nov 03, 2009 at 3:04 pm

Creating charts in PHP

no ratings, 0 comments
– Tip added by an anonymous user on Nov 28, 2008 at 10:37 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

First PHP Script

no ratings, 0 comments
– Tip added by learnhtmlonline 3 days, 1 hour ago

Make and Print Clip-art Card for Thanksgiving 2009

no ratings, 0 comments
– Tip added by an anonymous user on Nov 09, 2009 at 8:25 pm

Delete Records With Effect Using jQuery And PHP

Categories: php jQuery MySQL
no ratings, 0 comments
– Tip added by an anonymous user on Sep 27, 2009 at 7:10 am