Level: BeginnerInterview

Go Slice Interview Questions: Arrays and Maps (Part 5)

Updated 9 min read
Go Slice Interview Questions featured image: array, capacity and map cards

These Go slice interview questions cover arrays, slices and maps. Together, these types bring more tricky questions than any other basic topic. We collected them from public interview lists, coding tests and community threads. Then we grouped them by idea, so you can revise one part at a time.

This post is questions only, so answer each one in your own words first. When you need help, read Go slices and arrays and Go maps with examples. Also, finish Part 4 on functions first, because many slice questions involve passing slices to functions.

How to Use These Go Slice Interview Questions

Answer each question out loud, then draw the slice header for any question about append. A quick sketch of pointer, length and capacity solves most traps. Finally, try the scenario questions, since they come from real bugs.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
ArraysFixed size and value copies12
Slice internalsHeader, length and capacity16
append and copyGrowth and shared arrays16
Slice operationsSlicing, deleting and sorting13
MapsKeys, lookups and iteration18
Maps and concurrencySafe access from goroutines10
Code readingPredict the output4
Go Slice Interview Questions diagram showing six question areas from arrays to safe map access
Go slice interview questions grouped into six areas: arrays, slice internals, append and copy, slice operations, maps and safe map access.

Go Slice Interview Questions on Arrays

Before you answer, study Go slices and arrays. Arrays are less common in real code, yet these Go slice interview questions start here because arrays explain how slices work.

  1. What is an array in Go, and why is its size part of its type?
  2. Are [3]int and [4]int the same type?
  3. What happens when you assign one array to another variable?
  4. What happens when you pass an array to a function?
  5. How do you pass an array so the function can change it?
  6. How do you let the compiler count the elements, such as [...]int{1, 2, 3}?
  7. Can you compare two arrays with ==?
  8. Can an array be a map key, while a slice cannot?
  9. What is the zero value of an array?
  10. When would you choose an array over a slice in real code, such as for a fixed grid?
  11. Scenario: when a function changes an array argument and the caller sees no change, why?
  12. Scenario: when you need a fixed size key such as a 32 byte hash, which type would you pick?

Go Slice Interview Questions on Internals

These Go slice interview questions test how a slice is built underneath. So think in terms of a small header that points to an array.

  1. What is a slice, and how is it different from an array?
  2. Which three fields does a slice header hold?
  3. What is the difference between length and capacity?
  4. How do you create a slice with make, and what do its arguments mean?
  5. What is the difference between a nil slice and an empty slice?
  6. How do a nil slice and an empty slice look when encoded to JSON?
  7. Why is it safe to call len and append on a nil slice?
  8. Can you compare two slices with ==, or only with nil?
  9. How would you check that two slices hold the same elements?
  10. Why can a slice not be a map key?
  11. What happens when two slices share the same underlying array?
  12. How does slicing an existing slice set the new capacity?
  13. What is a full slice expression, such as s[low:high:max]?
  14. Why can a small slice keep a huge array alive in memory?
  15. Scenario: when an API returns null instead of an empty list, how would you fix the slice?
  16. Scenario: when reading a 1 GB file keeps memory high after you keep only 10 bytes, what is the cause?

Go Slice Interview Questions on append and copy

These are the most common Go slice interview questions, because append hides surprises. So draw each case before you answer.

  1. How does append work when the slice still has spare capacity?
  2. When the capacity is full, what does append do?
  3. How does Go decide the new capacity when a slice grows?
  4. Why must you always use the value returned by append?
  5. Can two appends to the same base slice overwrite each other?
  6. How do you append one slice to another slice?
  7. What does copy do, and what does it return?
  8. What happens when the destination of copy is shorter than the source?
  9. How do you make a full, independent copy of a slice?
  10. What is slices.Clone, and when would you use it?
  11. Why does preallocating capacity with make help performance?
  12. How do you append to a slice inside a function so the caller sees it?
  13. What is the difference between make([]int, 5) and make([]int, 0, 5)?
  14. What bug appears when you append to a slice created with make([]int, 5)?
  15. Scenario: when two goroutines append to one shared slice, what can go wrong?
  16. Scenario: when a report shows wrong rows after appending to a sub slice, how would you debug it?

Slice Operations Questions

Daily work needs a few common slice tricks. So these Go slice interview questions ask how you would write them.

  1. How do you remove an element from the middle of a slice?
  2. Explain how you insert an element at a given index.
  3. How do you reverse a slice in place, so no new slice is made?
  4. Can you filter a slice without allocating a new one, and how?
  5. Which helpers does the slices package offer, such as Contains and Index?
  6. How do you sort a slice of ints and a slice of structs?
  7. What is the difference between sort.Slice and slices.SortFunc?
  8. How do you remove duplicate values from a slice while you keep the order?
  9. How do you build a two dimensional slice, for example a 3 by 4 grid?
  10. Why should you clear deleted pointer elements in a slice?
  11. How would you split a slice into chunks of 100 items?
  12. Scenario: when you delete items while looping forward and some are skipped, how would you fix the loop?
  13. Scenario: when sorting 1 million structs is slow, what would you measure and change?

Go Slice Interview Questions on Maps

Before you answer, study Go maps with examples. Maps appear in almost every coding round, so these questions are common.

  1. What is a map in Go, and also how is it built inside?
  2. How do you create a map with make and with a literal?
  3. What happens when you read a key that does not exist?
  4. Explain the comma ok idiom, and also why you need it.
  5. What happens when you write to a nil map?
  6. Can you read from a nil map without a panic?
  7. Which types can be map keys, and which types cannot?
  8. Can a struct be a map key, and when?
  9. Why is map iteration order random?
  10. How do you delete a key, and is it safe if the key is missing?
  11. How do you get the number of entries in a map, and can you get its capacity too?
  12. Can you take the address of a map element, such as &m["a"]?
  13. How do you update a field of a struct stored as a map value?
  14. Is a map passed by value or by reference to a function?
  15. What does the clear built in do for maps?
  16. How do you use a map as a set, for example with map[string]struct{}?
  17. Scenario: when counting words in a large text, how would you use a map?
  18. Scenario: when a cache map never shrinks after deletes, what would you check?

Maps and Concurrency Questions

Maps are not safe for writes from many goroutines. So the last Go slice interview questions check that you know the safe options.

  1. Is a plain map safe for concurrent reads and writes?
  2. What error does Go report when two goroutines write to a map at once?
  3. How do you protect a map with sync.Mutex?
  4. When is sync.RWMutex better than sync.Mutex for a map?
  5. What is sync.Map, and when should you use it?
  6. Why is sync.Map not the default choice for every shared map?
  7. How can the race detector help with map bugs?
  8. How would you build a sharded map to reduce lock contention?
  9. Scenario: when a web service panics with “concurrent map writes” under load, how would you fix it?
  10. Scenario: when a read heavy config map is updated once a minute, which approach would you choose?

Code Reading Questions

Read each program and predict the output before you run it. Then explain your answer with a slice header sketch.

package main

import "fmt"

func main() {
	base := make([]int, 3, 5)
	a := append(base, 10)
	b := append(base, 20)
	fmt.Println(a[3], b[3], len(base), cap(base))

	c := base[1:2]
	c = append(c, 99)
	fmt.Println(base, c, len(c), cap(c))
}
  1. Why do a[3] and b[3] show the same number?
  2. What does the second Println show, and why did base change?

Here is the second program. This time, focus on missing keys and nil maps.

package main

import "fmt"

func main() {
	stock := map[string]int{"pen": 0}
	qty, ok := stock["pen"]
	fmt.Println(qty, ok)
	qty, ok = stock["book"]
	fmt.Println(qty, ok)

	var prices map[string]float64
	fmt.Println(len(prices), prices["tea"])
	prices["tea"] = 20
}
  1. What do the first two Println calls show, and how does ok help?
  2. Which line causes a problem, and what happens when it runs?

Quick Revision Table for Slices and Maps

Use the table below as a final check before your interview. It compares the three types side by side.

FeatureArraySliceMap
SizeFixed, part of the typeGrows with appendGrows on insert
Zero valueAll elements zeronilnil
Compare with ==YesOnly with nilOnly with nil
Can be a map keyYes, if elements are comparableNoNo
Passed to a functionFull copyHeader copy, shared dataShared data

Also, many interviewers ask you to compare arrays and slices directly. The diagram below shows the main differences.

Go Slice Interview Questions diagram showing a comparison of arrays and slices
An array holds its values directly and is copied in full, while a slice is a small header that points to an array and shares it.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go slice interview questions cover arrays, slice internals, append, copy and maps.
  • A slice is a header with a pointer, a length and a capacity.
  • Always use the value returned by append.
  • Reading a missing map key gives the zero value, so use the comma ok idiom.
  • Writing to a nil map causes a panic.
  • Plain maps are not safe for concurrent writes, so add a lock or use sync.Map.

What’s Next?

By now, you have Go slice interview questions for the toughest basic topic. So next, we will cover strings, runes, bytes and UTF-8.

Next up: Go strings interview questions on runes and bytes (Part 6)