yoklainterview sim

Backend Go Memory Performance Interview Questions

75 verified Backend Go Memory Performance interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Go Memory PerformanceDifficulty 1
In Go, a slice value is a small header containing a pointer, a length, and a capacity. What does the capacity represent?
  • aThe number of elements currently stored in the slice, same as its length
  • bHow many elements fit in the underlying array from the slice's start onward
  • cThe total memory in bytes allocated for the slice header itself
  • dThe maximum number of elements Go allows any slice to ever hold
Explanation:Length is how many elements the slice currently exposes. Capacity is how far the underlying array extends past the slice's start, so append can grow into that space without allocating a new array until capacity runs out.
Go Memory PerformanceDifficulty 2
What does this program print?

a := make([]int, 3, 3)
a[0], a[1], a[2] = 1, 2, 3
b := append(a, 4)
b[0] = 99
fmt.Println(a[0])
  • a1
  • b99
  • c4
  • dThis does not compile
Explanation:a has length 3 and capacity 3, so it is already full. append(a, 4) has no room to grow into, so Go allocates a brand new backing array for b and copies the elements over. b and a no longer share storage, so modifying b[0] leaves a[0] unchanged at 1.
Go Memory PerformanceDifficulty 3
What does this program print?

a := make([]int, 3, 5)
a[0], a[1], a[2] = 1, 2, 3
b := append(a, 4)
b[0] = 99
fmt.Println(a[0])
  • a1
  • b4
  • c99
  • dpanic: index out of range
Explanation:a has length 3 but capacity 5, so there is room for one more element without reallocating. append(a, 4) reuses the same backing array for b, meaning a and b alias the same storage. Writing b[0] = 99 changes that shared array, so a[0] becomes 99 too.
Go Memory PerformanceDifficulty 2
What does this program print?

nums := []int{10, 20, 30, 40, 50}
sub := nums[1:3]
sub[0] = 999
fmt.Println(nums[1])
  • a20
  • b0
  • c30
  • d999
Explanation:sub := nums[1:3] does not copy any elements; it is a new slice header pointing into the same backing array as nums, starting at index 1. sub[0] refers to the same memory cell as nums[1], so writing through sub is visible in nums.
Go Memory PerformanceDifficulty 2
What does this program print?

src := []int{1, 2, 3, 4, 5}
dst := make([]int, 3)
n := copy(dst, src)
fmt.Println(n, dst)
  • a5 [1 2 3 4 5]
  • b3 [1 2 3]
  • c3 [1 2 3 4 5]
  • d5 [1 2 3]
Explanation:copy(dst, src) copies min(len(dst), len(src)) elements and returns that count; it never grows dst. Here len(dst) is 3, so only the first 3 elements of src are copied, and copy returns 3.
Go Memory PerformanceDifficulty 1
A function repeatedly needs to start from an empty slice on each call while reusing the same backing storage, to avoid extra allocations. Which technique achieves this without allocating a new array?
  • aReset with s = s[:0], which keeps the same backing array and capacity while setting the length to zero
  • bReassign with s = []int{}, which is guaranteed to reuse the same array pointer as before
  • cCall s = make([]int, 0) each time, which always reuses the previous backing array
  • dSet s = nil and rely on the next append reusing the array that was freed
Explanation:s[:0] re-slices s to length zero but keeps the same pointer and capacity, so subsequent appends can reuse the existing backing array up to its old capacity before any new allocation is needed. Both []int{} and make([]int, 0) create a fresh, independent slice.

Test yourself against the 3300-question Backend bank.

Start interview