Hone logo
Hone
Problems

Mastering Go Slices: Implementing Custom Slice Operations

Go's built-in slice operations are powerful, but sometimes you need more specialized functionality. This challenge will test your understanding of slice manipulation by having you implement custom functions for common slice operations that Go doesn't directly provide. Successfully completing this will solidify your grasp of slice indexing, slicing syntax, and element manipulation.

Problem Description

Your task is to implement three distinct functions that perform custom operations on integer slices in Go. These functions should mimic common array/list operations found in other languages or provide extended slicing capabilities.

You need to implement the following functions:

  1. SliceCopy(src []int, dest []int) int: This function should copy elements from a source slice (src) into a destination slice (dest). It should copy at most len(dest) elements. The function should return the number of elements copied.
  2. SliceAppend(slice []int, element int) []int: This function should append a single element to the end of the given slice. It should return a new slice with the element appended.
  3. SliceRemove(slice []int, index int) ([]int, error): This function should remove the element at a specific index from the given slice. It should return a new slice with the element removed. If the index is out of bounds (less than 0 or greater than or equal to the length of the slice), it should return nil and an appropriate error.

Key Requirements:

  • Immutability (for SliceAppend and SliceRemove): The original slices passed to SliceAppend and SliceRemove should not be modified. The functions must return new slices.
  • Error Handling: SliceRemove must correctly handle invalid index inputs.
  • Efficiency: While not the primary focus, consider reasonable efficiency for your implementations.

Expected Behavior:

  • SliceCopy should copy as many elements as possible from src to dest, up to the capacity of dest.
  • SliceAppend should always create a new slice and place the new element at the end.
  • SliceRemove should create a new slice, omitting the element at the specified index, maintaining the order of the remaining elements.

Edge Cases to Consider:

  • Empty source or destination slices in SliceCopy.
  • Appending to an empty slice.
  • Removing from an empty slice (should result in an error).
  • Removing the first or last element.
  • Invalid indices for SliceRemove.

Examples

Example 1: SliceCopy

Input:
src = []int{1, 2, 3, 4, 5}
dest = []int{0, 0, 0}

Output:
dest = []int{1, 2, 3}
copiedCount = 3

Explanation:
3 elements were copied from src to dest. dest had enough capacity to hold the first 3 elements of src.

Example 2: SliceAppend

Input:
slice = []int{10, 20}
element = 30

Output:
newSlice = []int{10, 20, 30}
Original slice remains: []int{10, 20}

Explanation:
The element 30 was appended to the end of the original slice, creating a new slice. The original slice was unchanged.

Example 3: SliceRemove (Valid Removal)

Input:
slice = []int{1, 2, 3, 4, 5}
index = 2

Output:
newSlice = []int{1, 2, 4, 5}
Original slice remains: []int{1, 2, 3, 4, 5}

Explanation:
The element at index 2 (which is 3) was removed, and a new slice was returned. The original slice was unaffected.

Example 4: SliceRemove (Invalid Index)

Input:
slice = []int{1, 2, 3}
index = 5

Output:
newSlice = nil
err = "index out of bounds"

Explanation:
The provided index (5) is out of bounds for the slice of length 3. An error is returned, and newSlice is nil.

Example 5: SliceRemove (First Element)

Input:
slice = []int{1, 2, 3}
index = 0

Output:
newSlice = []int{2, 3}
Original slice remains: []int{1, 2, 3}

Explanation:
The first element (at index 0) was removed, creating a new slice.

Constraints

  • The input slices will contain only integers (int).
  • The maximum length of any input slice is not strictly limited, but your solutions should be reasonably efficient for typical slice sizes (e.g., up to 10,000 elements).
  • For SliceCopy, the destination slice dest will have a pre-allocated capacity.
  • For SliceRemove, the index will be an int.
  • The error returned by SliceRemove for out-of-bounds indices should be a standard Go error type, with a descriptive message like "index out of bounds".

Notes

  • Remember that slices in Go are references to underlying arrays. When implementing SliceAppend and SliceRemove, you'll often need to create a new underlying array to avoid modifying the original slice.
  • Pay close attention to slice syntax, particularly how to create new slices and copy elements between them.
  • Consider using the built-in append function for SliceAppend and copy for SliceCopy as starting points, but understand how they work internally. For SliceRemove, you might need to combine slicing and append to construct the new slice.
  • For SliceRemove, ensure you handle the case where the index is out of bounds correctly by returning an error.
Loading editor...
go