Start learning free

LESSON 172 OF 338 · STRINGS

Reverse String in Go: O(n) with strings.Builder

Practice O(n) Read the full explanation

Given a string, write a function that reverses that string without using built-in functions or libraries.

Example:

Input:  "hello"

Output: "olleh"

Note:

Your algorithm should run in O(n) time and use O(n) extra space.


STEP 1 OF 4
    Ctrl + Enter to check

    GO · STRINGS

    Reverse String in Go, explained

    Problem overview

    The function reverseString takes a string, input, and returns it reversed. For "hello" the answer is "olleh". This time the statement asks for O(n) time and O(n) extra space, so the repeated copying of the earlier approach is not allowed.

    Approach: read backwards, write forwards

    The slower version prepends each character to a growing string. In Go every such step allocates a new string and copies the old one, so the total work grows as O(n²). To reach O(n), two things change.

    1. Instead of inserting at the front, read the input from the last index down to 0 and append each element to the result. The order of the output is then already reversed.
    2. Instead of concatenating immutable strings, collect the output in a strings.Builder, which keeps a growable buffer. Appending to it is amortized O(1) per byte, because the buffer grows geometrically and each byte is copied only a few times over the whole run.

    The final String() call hands back the accumulated text without copying it again.

    Step by step

    1. Declare reversedString with var as a strings.Builder. Its zero value is ready to use, so no constructor call is needed.
    2. Loop with an index i that starts at len(input) - 1, continues while i >= 0, and decrements each pass. That visits the last byte first.
    3. In the loop body, append input[i] with WriteByte.
    4. After the loop, return reversedString.String().

    The solution in Go

    func reverseString(input string) string {
        var reversedString strings.Builder
        for i := len(input) - 1; i >= 0; i-- {
            reversedString.WriteByte(input[i])
        }
        return reversedString.String()
    }
    

    Language details worth knowing

    input[i] is a byte, and WriteByte takes a byte, so the types match with no conversion. WriteByte also returns an error value, which is always nil for a builder. Ignoring it is normal here and compiles without complaint.

    The function needs the strings package imported. A builder must not be copied after you first use it; since the variable lives and dies inside this function, that never comes up.

    The loop counts down, so i-- is the post statement, and the condition is i >= 0. Go's int is signed, so it can go below zero and stop the loop. This would be a bug with an unsigned index type, which would wrap around instead of becoming negative.

    Complexity

    Time is O(n): one pass over n bytes, each appended in amortized constant time. Space is O(n) for the builder's buffer, which ends up holding a string as long as the input.

    Edge cases and common mistakes

    • An empty string: len(input) - 1 is -1, the condition fails at once, and the function returns "".
    • Starting at len(input) instead of len(input) - 1 reads one past the end and panics.
    • Stopping at i > 0 would drop the first character of the input from the result.
    • Reversing bytes is correct for ASCII, but it scrambles any character that uses more than one byte. Reversing "héllo" this way produces bytes that are not valid UTF-8 and prints as garbage, because the two bytes of é swap places. If the input may contain such characters, convert it to a []rune and reverse that instead, or loop with range as in the slower version.
    • Using += on a string inside this loop would compile and give the right answer, but it brings back the O(n²) copying.

    Testing

    • "hello" gives "olleh".
    • "" gives "".
    • "a" gives "a".
    • "ab" gives "ba".
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free