#1 Coding Interview Preparation Problems in Go

Go (often referred to as Golang) is a powerful, efficient, and statically typed programming language developed by Google. In this blog, we’ll solve ten popular beginner-level programming challenges using Go. These problems are great for honing your problem-solving and coding skills.

1. Reverse a String

Explanation: This program reverses a string by iterating through its characters and prepending them to a new result string.

package main

import "fmt"

func reverseString(s string) string {
result := ""
for _, char := range s {
result = string(char) + result
}
return result
}

func main() {
input := "hello"
fmt.Println("Reversed String:", reverseString(input))
}

Output:

Reversed String: olleh

2. Check Palindrome

Explanation: A string is a palindrome if it reads the same backward as forward. This program reverses the string and compares it with the original.

package main

import (
"fmt"
"strings"
)

func isPalindrome(s string) bool {
// Convert the string to lowercase before checking
s = strings.ToLower(s)
reversed := reverseString(s)
return s == reversed
}

func reverseString(s string) string {
result := ""
for _, char := range s {
result = string(char) + result
}
return result
}

func main() {
input := "Radar"
fmt.Println("Is Palindrome:", isPalindrome(input))
}

Output:

Palindrome: true

3. Find the Largest Number

Explanation: This program iterates through the list and keeps track of the largest number encountered so far.

package main

import "fmt"

func findLargest(nums []int) int {
largest := nums[0]
for _, num := range nums {
if num > largest {
largest = num
}
}
return largest
}

func main() {
nums := []int{3, 5, 7, 2, 8}
fmt.Println("Largest Number:", findLargest(nums))
}

Output:

Largest Number: 8

4. Count Vowels in a String

Explanation: This program counts vowels (a, e, i, o, u) by checking each character against a set of vowels.

package main

import "fmt"

func countVowels(s string) int {
vowels := "aeiouAEIOU"
count := 0
for _, char := range s {
if contains(vowels, char) {
count++
}
}
return count
}

func contains(set string, char rune) bool {
for _, c := range set {
if c == char {
return true
}
}
return false
}

func main() {
input := "education"
fmt.Println("Number of Vowels:", countVowels(input))
}

Output:

Number of Vowels: 5

5. FizzBuzz

Explanation: This program prints “Fizz” for multiples of 3, “Buzz” for multiples of 5, and “FizzBuzz” for multiples of both.

package main

import "fmt"

func main() {
for i := 1; i <= 50; i++ {
if i%3 == 0 && i%5 == 0 {
fmt.Println("FizzBuzz")
} else if i%3 == 0 {
fmt.Println("Fizz")
} else if i%5 == 0 {
fmt.Println("Buzz")
} else {
fmt.Println(i)
}
}
}

Output:

1
2
Fizz
4
Buzz
Fizz
7
...
FizzBuzz

6. Sum of Digits

Explanation: This program repeatedly extracts the last digit of the number using % 10 and adds it to a running sum.

package main

import "fmt"

func sumOfDigits(n int) int {
sum := 0
for n > 0 {
sum += n % 10
n /= 10
}
return sum
}

func main() {
num := 1234
fmt.Println("Sum of Digits:", sumOfDigits(num))
}

Output:

Sum of Digits: 10

7. Check Prime

Explanation: A prime number is greater than 1 and divisible only by 1 and itself. This program checks divisibility up to the square root of the number.

package main

import "fmt"

func isPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}

func main() {
num := 13
fmt.Println("Is Prime:", isPrime(num))
}

Output:

Is Prime: true

8. Fibonacci Series

Explanation: The Fibonacci series starts with 0, 1, and each subsequent number is the sum of the previous two.

package main

import "fmt"

func fibonacci(n int) []int {
if n <= 0 {
return []int{}
}
seq := []int{0, 1}
for len(seq) < n {
seq = append(seq, seq[len(seq)-1]+seq[len(seq)-2])
}
return seq[:n]
}

func main() {
n := 6
fmt.Println("Fibonacci Series:", fibonacci(n))
}

Output:

Fibonacci Series: [0 1 1 2 3 5]

9. Find Factorial

Explanation: The factorial of a number is the product of all integers from 1 to the number itself.

package main

import "fmt"

func factorial(n int) int {
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result
}

func main() {
num := 5
fmt.Println("Factorial:", factorial(num))
}

Output:

Factorial: 120

10. Find Missing Number

Explanation: The sum of integers from 1 to n is n(n+1)/2. Subtract the sum of the given numbers from this total to find the missing number.

package main

import "fmt"

func findMissing(nums []int, n int) int {
total := n * (n + 1) / 2
sum := 0
for _, num := range nums {
sum += num
}
return total - sum
}

func main() {
nums := []int{1, 2, 4, 5}
n := 5
fmt.Println("Missing Number:", findMissing(nums, n))
}

Output:

Missing Number: 3