Swap Two Numbers

Medium
7 months ago

I'd like to start with a fundamental programming question. Your task is to write a function that swaps the values of two numbers. The function should take two integer arguments (passed by reference if your language supports it, or using pointers if necessary) and modify them such that the original value of the first argument is now the value of the second argument, and vice versa.

Here are some test cases to illustrate the expected behavior:

  • Test Case 1:
    • Input: a = 5, b = 10
    • Expected Output: a = 10, b = 5
  • Test Case 2:
    • Input: a = -3, b = 12
    • Expected Output: a = 12, b = -3
  • Test Case 3:
    • Input: a = 0, b = 0
    • Expected Output: a = 0, b = 0
  • Test Case 4:
    • Input: a = 1000, b = -1000
    • Expected Output: a = -1000, b = 1000

Please provide the code for your function and explain your approach. Consider different programming languages and memory constraints as you formulate your solution. Feel free to ask any clarifying questions.

Sample Answer

Swapping Two Numbers

Here's how you can swap two numbers in place.

Naive Approach (Using a Temporary Variable)

The most straightforward way to swap two numbers is to use a temporary variable. This approach is easy to understand and implement.

python def swap_naive(a, b): temp = a a = b b = temp return a, b

Example usage:

a = 5 b = 10 a, b = swap_naive(a, b) print(f"a = {a}, b = {b}") # Output: a = 10, b = 5

  • Big O Runtime: O(1) - Constant time, as it takes the same amount of time regardless of the input values.
  • Big O Space: O(1) - Constant space, as we're only using one extra variable (temp).

Optimal Approach (Without a Temporary Variable)

You can also swap two numbers without using a temporary variable by employing arithmetic operations or the XOR (exclusive OR) bitwise operator.

1. Arithmetic Operations:

python def swap_arithmetic(a, b): a = a + b b = a - b a = a - b return a, b

Example usage:

a = 5 b = 10 a, b = swap_arithmetic(a, b) print(f"a = {a}, b = {b}") # Output: a = 10, b = 5

2. XOR Operator:

python def swap_xor(a, b): a = a ^ b b = a ^ b a = a ^ b return a, b

Example usage:

a = 5 b = 10 a, b = swap_xor(a, b) print(f"a = {a}, b = {b}") # Output: a = 10, b = 5

  • Big O Runtime: O(1) - Constant time, regardless of the input values.
  • Big O Space: O(1) - Constant space, as no extra variables are used.

Edge Cases

  • Identical Numbers: All methods will work correctly even if a and b are the same number.
  • Zero/Negative Numbers: The arithmetic and XOR methods work with zero and negative numbers.
  • Very Large Numbers (Arithmetic Method): With the arithmetic method, you might encounter an overflow issue if the sum of a and b exceeds the maximum representable value for the data type. This is generally why the XOR method is preferred over the arithmetic method when avoiding temporary variables.

When to Use Which Method

  • The naive method with a temporary variable is often the most readable and easiest to understand. If performance isn't critical, it's a good choice.
  • The XOR method is generally favored when you absolutely need to swap in place (without extra variables) and avoid potential overflow issues associated with the arithmetic method. However, it may be slightly less readable for some.
  • The arithmetic method should be used with caution, keeping overflow in mind.