Sobes.tech
Junior — Senior

Behavior when copying and expanding a slice

livecode

Task condition

In the example, a slice arr is first created using make, then a second slice newArr is formed as a full slice of arr[:]. After that, elements are sequentially added to newArr using append. It is necessary to determine how the length and capacity of newArr will change after each operation and how these changes will affect the original slice arr.

arr := make([]int, 0, 2)        // len 0, cap 2
arr = append(arr, 1)            // len 1, cap 2

newArr := arr[:]                // len = 1, cap = ?
newArr = append(newArr, 2)      // what will happen to the original?
newArr = append(newArr, 3)      // what will happen to the original?