Level: BeginnerInterview

Go Strings Interview Questions: Runes and Bytes (Part 6)

Updated 9 min read
Go Strings Interview Questions featured image: UTF-8, rune and builder cards

These Go strings interview questions cover strings, bytes, runes, UTF-8, string building, conversion and regular expressions. We collected them from public interview lists, coding rounds 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 strings with examples and Go regular expressions. Also, finish Part 5 on slices and maps first, because a string behaves much like a read-only byte slice.

How to Use These Go Strings Interview Questions

Answer each question out loud, then test the tricky ones with a word that has an accent, such as “café”. Most string bugs only show up with non English text. Finally, try the scenario questions, since they come from real production issues.

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

SectionWhat it testsQuestions
String basicsLiterals, immutability and length15
Bytes, runes and UTF-8How text is stored16
Building stringsConcatenation and Builder13
strings and strconvCommon helper functions16
Regular expressionsThe regexp package11
String coding tasksClassic interview programs14
Code readingPredict the output4
Go Strings Interview Questions diagram showing six question areas from string basics to coding tasks
Go strings interview questions grouped into six areas: string basics, bytes and runes, building strings, the strings and strconv packages, regular expressions and coding tasks.

Go Strings Interview Questions on the Basics

Before you answer, study Go strings with examples. These Go strings interview questions check the rules that every Go developer should know.

  1. What is a string in Go, and also how is it stored in memory?
  2. Why are strings immutable in Go?
  3. What is the difference between a raw string literal and an interpreted one?
  4. When would you use a raw string literal, such as for JSON or a regex?
  5. What does len return for a string, bytes or characters, so how do you count letters?
  6. What is the zero value of a string, and how do you check for it before use?
  7. How do you compare two strings, and is the comparison case sensitive?
  8. How do you compare strings while you ignore case?
  9. Can you change one character of a string directly, as in s[0] = 'H'?
  10. What does indexing a string with s[i] return, a byte or a character?
  11. What does slicing a string with s[1:4] return, and does it copy?
  12. Are strings passed to functions by value, and is that expensive when they are long?
  13. How do you write a multi line string in Go, for example an SQL query?
  14. Scenario: when a user name with an accent shows the wrong length, what is the cause?
  15. Scenario: when a team compares passwords with ==, which safer function would you suggest?

Bytes, Runes and UTF-8 Questions

Text in Go is stored as UTF-8 bytes. So these Go strings interview questions check whether you know a byte from a character.

  1. What is a byte, and which type is it an alias for?
  2. Explain what a rune is, and also which type it is an alias for.
  3. What is UTF-8, and why does Go use it for source code and strings?
  4. How many bytes can one character take in UTF-8, such as an emoji?
  5. How do you count the characters in a string instead of its bytes?
  6. What does a for range loop over a string give you?
  7. How is a for range loop different from a classic index loop over a string?
  8. What happens when a string holds invalid UTF-8 bytes?
  9. When would you use the unicode/utf8 package?
  10. How do you convert a string to a byte slice and then back again?
  11. Similarly, how do you convert a string to a rune slice and back?
  12. Why does converting between a string and a byte slice usually copy the data?
  13. What does string(rune(65)) return, and why is string(65) flagged by go vet?
  14. Which functions check if a rune is a letter, digit or space?
  15. Scenario: when you cut a name to 10 bytes and the last letter becomes broken, how would you fix it?
  16. Scenario: when a file from Windows has a byte order mark at the start, how would you handle it?

Go Strings Interview Questions on Building Strings

Building text in a loop is a common performance question. So these Go strings interview questions focus on cost.

  1. Why is s += x inside a long loop slow?
  2. What is strings.Builder, and how does it avoid extra copies?
  3. When would you call Grow on a strings.Builder?
  4. Can you copy a strings.Builder value after you start writing to it?
  5. How does bytes.Buffer compare with strings.Builder, and when would you pick each?
  6. When is strings.Join better than a Builder?
  7. Is fmt.Sprintf slower than a Builder, and why?
  8. How would you build a CSV line from a slice of fields, so quotes stay safe?
  9. What is the cost of converting a large byte slice to a string?
  10. How would you measure which method is fastest with a benchmark?
  11. Why should you avoid fmt.Sprintf in a hot loop?
  12. Scenario: when a report builder takes 4 seconds for 50,000 rows, which change would you try first?
  13. Scenario: when a log line is built even though the log level hides it, how would you avoid the waste?

Go Strings Interview Questions on strings and strconv

The standard library has helpers for most text tasks. So these Go strings interview questions ask which function you would reach for.

  1. How do you check if a string contains a word, and also if it starts with a prefix?
  2. How do you split a string by commas, and what does Split return for an empty string?
  3. When there are many spaces, why is strings.Fields safer than strings.Split?
  4. How do you trim spaces and a given prefix from a string?
  5. Which function replaces all matches of a word in a string?
  6. How do you change a string to upper case or lower case, even for German text?
  7. What does strings.EqualFold do?
  8. How do you find the position of a substring, and what if it is missing?
  9. Which helper repeats a string, such as making a line of 20 dashes?
  10. How do you convert a string to an int with strconv.Atoi?
  11. Then, how do you convert a string to a float or a bool?
  12. Why is strconv.Itoa faster than fmt.Sprint for numbers?
  13. How do you quote a string with escape characters using strconv?
  14. Which error does strconv return for “12a”, and how do you check it?
  15. Scenario: when a CSV field ” 42 ” fails to parse, how would you clean it first?
  16. Scenario: when user input must be a whole number between 1 and 100, how would you validate it?

Go Strings Interview Questions on Regular Expressions

Before you answer, study Go regular expressions. Regex questions appear in validation and parsing rounds.

  1. How do you compile a regular expression in Go before you use it?
  2. What is the difference between regexp.Compile and regexp.MustCompile?
  3. Why should you compile a regex once instead of inside a loop?
  4. How do you find all matches in a string instead of only the first?
  5. How do you use capture groups to pull out parts of a match?
  6. Which regex engine does Go use, and why does it not support back references?
  7. Why is the Go regexp package safe from slow catastrophic matching?
  8. When is a simple strings function better than a regex?
  9. How do you replace matches with a function result?
  10. Scenario: when you must validate Indian mobile numbers, how would you write and test the pattern?
  11. Scenario: when log parsing with regex is slow, what would you measure and change?

String Coding Task Questions

Coding rounds often include a short string program. So practise each of these Go strings interview questions in under ten minutes.

  1. Write a function that reverses a string, so multi byte characters stay correct.
  2. Next, check if a string is a palindrome while you ignore case and spaces.
  3. Count how often each word appears in a sentence, then print the top three.
  4. How would you check if two words are anagrams, for example “listen” and “silent”?
  5. Find the first character that does not repeat, and also explain its cost.
  6. Remove duplicate characters from a string while you keep their order.
  7. Convert “hello world” to “Hello World” without using a regex.
  8. Can you count the vowels in a string, including capital letters?
  9. Compress “aaabcc” into “a3b1c2”, then handle an empty input as well.
  10. Check if the brackets in a string are balanced, such as “{[()]}”.
  11. Which approach finds the longest word in a sentence with one pass?
  12. Write a check that a string holds only digits, since IDs often need it.
  13. Scenario: when your reverse function breaks Hindi text, how would you fix it?
  14. Scenario: when the interviewer asks for the time and space cost of your solution, how would you explain it?

Code Reading Questions

Read each program and predict the output before you run it. Then explain the answer with bytes and runes in mind.

package main

import "fmt"

func main() {
	s := "héllo"
	fmt.Println(len(s), len([]rune(s)))
	for i, r := range s {
		fmt.Print(i, ":", string(r), " ")
	}
	fmt.Println()
	fmt.Println(s[1], string(s[1]))
}
  1. Why do the two lengths differ, and which numbers are printed?
  2. Why does the index jump from 1 to 3 in the loop, and what does the last line show?

Here is the second program. This time, focus on conversion and building.

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	fmt.Println(string(rune(65)), strconv.Itoa(65))

	var sb strings.Builder
	for i := 0; i < 3; i++ {
		sb.WriteString("go")
	}
	fmt.Println(sb.String(), sb.Len())

	b := []byte("cat")
	b[0] = 'b'
	fmt.Println(string(b), strings.ToUpper("straße"))
}
  1. What does the first line print, and how do the two conversions differ?
  2. Why can you change b[0] here, even though strings are immutable?

Quick Revision Table for Strings

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

TypeWhat it holdsMutableTypical use
stringRead-only bytes, usually UTF-8NoText values and map keys
[]byteRaw bytesYesFiles, network data, building text
runeOne Unicode code pointNot applicableWorking with characters
[]runeCode points in a sliceYesReversing or cutting by character

Also, many interviewers ask you to compare bytes and runes directly. The diagram below shows the difference for one word.

Go Strings Interview Questions diagram showing the bytes and runes of the word héllo
A byte is one 8 bit unit of a UTF-8 string, while a rune is a full Unicode character that can take one to four bytes.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go strings interview questions cover basics, bytes, runes, building, helpers, regex and coding tasks.
  • A string is read-only, and len counts bytes, not characters.
  • A for range loop over a string gives runes and byte positions.
  • Use strings.Builder to build text in a loop.
  • Compile a regex once and reuse it.
  • Test string code with accented or non English text.

What’s Next?

By now, you have Go strings interview questions for every text topic, so the basic level is done. Next, we will move to the intermediate level with structs, pointers and methods.

Next up: Go structs interview questions: pointers and methods (Part 7).