Coding Probem: Palindrome Checking in Golang

Coding Probem: Palindrome Checking in Golang

Learn three solutions and sharpen your coding skills!

Learn three solutions and sharpen your coding skills!

Apalindrome is a string of characters that read the same forwards as they do backwards. It’s a common coding interview question to check if a given string is a palindrome or not, so it’s useful to have the solution to this problem in your coding tool belt. Here’s a fun example of a palindrome:

“Are we not drawn onward, we few, drawn onward to new era?” — Socrates Setarcos

In this article, we will go over three solutions to the famous palindrome question. This will include analyzing their different time and space complexities while using my favorite programming language, Golang!

Let’s get started!

String Concatenation — O(N²) Time, O(N) Space

A common brute force solution would be to recreate our given string in reverse order, and then compare the two strings to see if they are equal to each other. These initial solutions are actually a great first step in coding questions and show your interviewer that you think of the basics first before jumping to optimization.

First we initialize an empty string we call reversedStr. We will then execute a for loop where we initialize the index i to the end of the array minus one (array of length 30 has max index of 29). As we move i from the end of the string to the beginning in reverse order, we concatenate the current character str[i] with reversedStr.

Note: Accessing an element of an array with an index in Go will give you a byte object, so you have to convert it to a string first string(str[i]).

Last we have another for loop in which we use the function range() to have our index i iterate through the length of the string str. As we do so, we compare each character if str and reversedS and check if they are equal. If they aren’t at any point in time, we return false. If we make it through the entire string and all of the characters are equal, then the string is in fact a palindrome and we return true!

func isPalindrome(str string) bool {
    reversedStr := ""
    for i := len(str)-1; i >= 0; i-- {
        reversedStr += string(str[i])
    }
    for i := range(str) {
        if str[i] != reversedStr[i] {
            return false
        }
    }
    return true
}

You might initially think that this function is O(N) time, but be careful! String concatenation is actually an O(N) operation. For every character of str, we concatenate it with reversedStr which results in an O(N²) time complexity. Since we also store the length of the str array in reversedStr, our space complexity will be O(N).

golang technology tutorial

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Data Visualization in R with ggplot2: A Beginner Tutorial

Data Visualization in R with ggplot2: A Beginner Tutorial. Learn to visualize your data using R and ggplot2 in this beginner-friendly tutorial that walks you through building a chart for data analysis.

Tutorial: Getting Started with R and RStudio

Get your R programming journey off on the right foot with this RStudio tutorial that walks through everything from installation to best practices.

Tutorial: Loading and Cleaning Data with R and the tidyverse

Learn how to load a data set and clean it using R programming and tidyverse tools in this free beginner-level data analysis tutorial.

R Tutorial: Better Blog Post Analysis with googleAnalyticsR

Learn to compare blog posts on even footing using R programming and the googleAnalyticsR package for blog data analysis in this free tutorial.

Top five technologies among young entrepreneurs

With transformational changes seen in the business and technology front, Entrepreneurs’ view towards technologies is changing. Here are [the top technologies that young entrepreneurs can embed to increase their business...