Start learning free

LESSON 117 OF 291 · ARRAYS - CHALLENGES

Swap First and Last in Scala: ArrayBuffer and a Temp Variable

Practice Read the full explanation

Given an ArrayBuffer of integers nums, swap its first and last numbers and return it.

Change nums itself, then return it. The numbers in between stay where they are. When nums has only one number, that number is both the first and the last, so it stays as it is.

 

Example 1:

Input: nums = [1, 2, 3, 4, 5]
Output: [5, 2, 3, 4, 1]
Explanation: 1 and 5 trade places. 2, 3 and 4 don't move.

Example 2:

Input: nums = [4, 9]
Output: [9, 4]
Explanation: With two numbers, the first and the last are next to each other, and they trade places.

Example 3:

Input: nums = [7]
Output: [7]
Explanation: One number is both the first and the last, so nothing changes.

 

Constraints:

  • nums has at least one number.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    SCALA · ARRAYS - CHALLENGES

    Swap First And Last in Scala, explained

    Problem overview

    The method swapFirstAndLast receives an ArrayBuffer[Int] named nums, swaps its first and last numbers, and returns it. The method changes nums itself and then returns that same buffer. The numbers in between stay where they are. [1, 2, 3, 4, 5] becomes [5, 2, 3, 4, 1], and [4, 9] becomes [9, 4]. With one number, that number is both the first and the last, so [7] stays [7].

    Approach

    An ArrayBuffer is a mutable, growable sequence from scala.collection.mutable. Items are read with nums(i) and written with nums(i) = value, and nums.size is the number of items. Because the buffer can be modified in place, the swap can be done on nums directly, with no new collection.

    The trap in a swap is that two values can't trade places in one move. If you write the last number into the first place right away, the old first number is overwritten and lost. The standard fix is a temporary variable: save the first number somewhere safe, overwrite its place, and then put the saved number into the last place. It is like using a third cup to swap the drinks in two cups.

    Step by step

    1. Save the first number in a val named temp: val temp = nums(0).
    2. Copy the last number into the first place. The last index is nums.size - 1, so the line is nums(0) = nums(nums.size - 1).
    3. Put the saved number in the last place: nums(nums.size - 1) = temp.
    4. Return nums. It is the same buffer that was passed in, now swapped.

    The solution in Scala

    class Solution {
        def swapFirstAndLast(nums: ArrayBuffer[Int]): ArrayBuffer[Int] = {
            val temp = nums(0)
            nums(0) = nums(nums.size - 1)
            nums(nums.size - 1) = temp
            return nums
        }
    }
    

    A few Scala details:

    • nums(0) = x is how Scala writes into a mutable collection. It is a call to the buffer's update method, written like an assignment.
    • The method modifies its argument. After val result = solution.swapFirstAndLast(buffer), the names result and buffer refer to the same object, so buffer is swapped too.
    • The type ArrayBuffer lives in scala.collection.mutable. In a file of your own, add import scala.collection.mutable.ArrayBuffer to use it.
    • val temp never changes after it is set, so val is the right choice over var.

    Tracing an example

    Start with nums = [1, 2, 3, 4, 5]. After val temp = nums(0), temp is 1. After nums(0) = nums(nums.size - 1), the buffer is [5, 2, 3, 4, 5]: the first place holds the old last number, and the 1 survives only in temp. After nums(nums.size - 1) = temp, the buffer is [5, 2, 3, 4, 1].

    Skipping temp and writing nums(0) = nums(4) followed by nums(4) = nums(0) ends with [5, 2, 3, 4, 5]: the second line copies the 5 back, because the 1 is already gone.

    Edge cases and common mistakes

    • One number. The first and last index are both 0. The method saves the number, writes it to the same place and writes it back, so [7] stays [7] with no special case.
    • Two numbers. The first and last places are neighbours, and the same three lines swap them: [4, 9] becomes [9, 4].
    • Using nums.size as the last index. The last valid index is nums.size - 1. Index nums.size throws an exception.
    • An empty buffer. nums(0) would throw an exception. The lesson guarantees at least one number.

    Testing

    • [1, 2, 3, 4, 5] gives [5, 2, 3, 4, 1].
    • [4, 9] gives [9, 4].
    • [7] gives [7].
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free