Palindrome in Golang

One of the common coding interview questions is Palindromes. A palindrome is a sentence (can be a number too) that is read the same backward as it is forwards, such as “ABCBA”, “KODOK”, etc. The complexity of this problem depends on whether we have to check a single word or if we also have to check full sentences.

Usually, we will be given a string and check whether it is a palindrome or not. It can be a number too, but this time I will provide an example of how to check whether a string is a palindrome or not.

In this case, I will use Go as an example. Voila, here is the code.

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func palindrome(word1 string) bool {
	wordLen1 := len(word1)
	for i := 0; i < wordLen1; i++ {
		if word1[i] != word1[wordLen1-1-i] {
			return false
		}
	}
	return true
}

func main() {
	a := "abcba"
	fmt.Println(palindrome(a))
}

Quite simple isn’t it? Today I got an interview as a Senior Software Engineer with this question. Actually, I got two questions only. The first one is how to swap two variables without using a temporary variable. I will share it in the other post.

Leave a Comment