1

Appending an item to a slice in Go has amortized O(1) performance, since slices are backed by an array. Is this the same case for prepending an item to a slice? For example, this is usually done as follows:

a = append([]T{x}, a...)

Am I right in thinking that the elements of a are being copied to a new array here, and that this happens in O(n) time? If so, if I want to implement a deque-like data structure, would using a List be more appropriate (such as how deques are implemented in python)?

This may be a niche use case. I'm mostly asking for scenarios like coding interviews, where the time limit may be exceeded for certain test cases.

koko
  • 158
  • 1
  • 7

0 Answers0