Introduction
In this tutorial, we will look at top 10 Golang coding interview Questions and answers. Go has become of the the highly demanded programming language in today’s world as it is one of the most powerful language among all other languages. It’s key features like Garbage Collection, Concurrency support, Standard library, Static linking etc. makes it very different and demanding than any other programming languages. We will cover 10 very important problems and their solutions that will prepare and help you to solve coding questions in interviews.
Golang Overview
Go, also known as Golang is a statically typed, compiled programming language designed for simplicity, efficiency and ease of use. Go is designed with a focus on systems programming but it also is a versatile language suitable for a wide range of applications. We will look at 10 very basic yet important coding questions that are generally asked in the interview. so let’s get started.
Top 10 Golang Coding Interview Questions and Answers (2023)
Also read: Python Recursive Function [Interview Question]
Prerequisite
- Any Operating System
- Go installed
Question-1: Reverse a String
In this problem, you will be asked to write a Go code to reverse a string. Below is the solution given.
package main import "fmt" func reverseString(input string) string { strSlice := []rune(input) for i, j := 0, len(strSlice)-1; i < j; i, j = i+1, j-1 { strSlice[i], strSlice[j] = strSlice[j], strSlice[i] } return string(strSlice) } func main() { result := reverseString("Golang Tutorial") fmt.Printf(result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\reverseString.go lairotuT gnaloG
Question-2: Check for Palindrome
In this question, you will be asked to write a Go code to check if a given string is palindrome or not. Below is the solution.
package main import ( "fmt" "strings" ) func checkPalindrome(input string) bool { input = strings.ToLower(input) input = strings.ReplaceAll(input, " ", "") return input == reverseString(input) } func reverseString(input string) string { strSlice := []rune(input) for i, j := 0, len(strSlice)-1; i < j; i, j = i+1, j-1 { strSlice[i], strSlice[j] = strSlice[j], strSlice[i] } return string(strSlice) } func main() { result := checkPalindrome("omomo") fmt.Printf("%v\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\checkPalindrome.go false PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\checkPalindrome.go true
Question-3: Find the Missing number
In this question, you will be given an integer array which stores the elements from 1 to N with one element missing. Your job is to find that missing number in the array. Below is the solution.
package main import "fmt" func findMissingNumber(arr []int, n int) int { total := n * (n + 1) / 2 sum := 0 for _, num := range arr { sum += num } return total - sum } func main() { arr := []int{1, 2, 3, 5, 6} result := findMissingNumber(arr, 6) fmt.Printf("Missing number is: %d\n", result) }
OUTPUT
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\findNumber.go Missing number is: 4
Question-4: Count Words in a String
In this question, you will be given a string. Your job is to write a Golang code to count the total number of words in the given string. Below is the solution.
package main import ( "fmt" "strings" ) func countWords(input string) int { totalWords := strings.Fields(input) return len(totalWords) } func main() { input := "Hello from Golang Tutorial" result := countWords(input) fmt.Printf("Total word count in '%v' string is: %d\n", input, result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\wordCount.go Total word count in 'Hello from Golang Tutorial' string is: 4
Question-5: Calculate Factorial
In this question, you will be given a positive integer value. Your job is to write a Golang code to find the factorial of given number. Below is the solution.
package main import ( "fmt" ) func findFactorial(val int) int { if val == 0 { return 1 } return val * findFactorial(val-1) } func main() { result := findFactorial(5) fmt.Printf("Factorial Calculated: %d\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\calFactorial.go Factorial Calculated: 120
Question-6: Find Duplicate Elements in Array
In this question, you will be given an array of integer values. Your job is to find the duplicate elements in the given array. Below is the solution.
package main import ( "fmt" ) func findDuplicate(arr []int) []int { seenArray := make(map[int]bool) duplicateArray := []int{} for _, val := range arr { if seenArray[val] { duplicateArray = append(duplicateArray, val) } else { seenArray[val] = true } } return duplicateArray } func main() { arr := []int{1, 2, 1, 5, 4, 6, 6, 9} result := findDuplicate(arr) fmt.Printf("Duplicate elements in given Array are: %v\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\findDuplicate.go Duplicate elements in given Array are: [1 6]
Question-7: Merge Two Arrays
In this problem, you will be given two sorted integer arrays. Your job is to merge two arrays into a single sorted array. Below is the solution.
package main import ( "fmt" ) func mergeArray(arr1, arr2 []int) []int { mergedArray := []int{} i, j := 0, 0 for i < len(arr1) && j < len(arr2) { if arr1[i] < arr2[j] { mergedArray = append(mergedArray, arr1[i]) i++ } else { mergedArray = append(mergedArray, arr2[j]) j++ } } mergedArray = append(mergedArray, arr1[i:]...) mergedArray = append(mergedArray, arr2[j:]...) return mergedArray } func main() { arr1 := []int{1, 3, 8, 9, 17} arr2 := []int{2, 5, 11, 13} result := mergeArray(arr1, arr2) fmt.Printf("Merged array is: %v\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\mergeArray.go Merged array is: [1 2 3 5 8 9 11 13 17]
Question-8: Find Maximum Subarray Sum
In this problem you will be given an integer array. Your job is to find the maximum sum of a contiguous subarray from the given array. Below is the solution.
package main import ( "fmt" ) func maxSubarraySum(arr []int) int { maxSum := arr[0] currentSum := arr[0] for i := 1; i < len(arr); i++ { currentSum = max(arr[i], currentSum+arr[i]) maxSum = max(maxSum, currentSum) } return maxSum } func main() { arr := []int{1, 3, 8, 9, 17} result := maxSubarraySum(arr) fmt.Printf("Maximum sum of Subarray in given array is: %v\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\maxSum.go Maximum sum of Subarray in given array is: 38
Question-9: Implement Binary Search
In this question, you will be asked to implement binary search algorithm using Go. Pass one element as function argument to search using binary search algorithm. Below is the solution.
package main import "fmt" func binarySearch(arr []int, val int) int { left, right := 0, len(arr)-1 for left <= right { mid := left + (right-left)/2 if arr[mid] == val { return mid } else if arr[mid] < val { left = mid + 1 } else { right = mid - 1 } } return -1 // Value not found in array } func main() { arr := []int{2, 3, 5, 7, 8, 10} //create sorted Array result := binarySearch(arr, 12) fmt.Printf("Element Found at index: %v\n", result) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\binarySearch.go Element Found at index: 3 PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\binarySearch.go Element Found at index: 0 PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\binarySearch.go Element Found at index: -1
Question-10: Linked List Cycle Detection
In this problem, you will be given a Linked list. Your job is to find out if Linked list has a cycle (loop) by writing code in Go.
package main import "fmt" // ListNode represents a node in a singly linked list. type ListNode struct { Val int Next *ListNode } func checkCycle(head *ListNode) bool { slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next if slow == fast { return true } } return false } func main() { // Create a linked list without a cycle listWithoutCycle := &ListNode{Val: 1} listWithoutCycle.Next = &ListNode{Val: 2} listWithoutCycle.Next.Next = &ListNode{Val: 3} hasCycle1 := checkCycle(listWithoutCycle) fmt.Printf("Linked List 1 has a cycle: %v\n", hasCycle1) // Create a linked list with a cycle listWithCycle := &ListNode{Val: 1} listWithCycle.Next = &ListNode{Val: 2} listWithCycle.Next.Next = &ListNode{Val: 3} listWithCycle.Next.Next.Next = listWithCycle.Next // Create a cycle hasCycle2 := checkCycle(listWithCycle) fmt.Printf("Linked List 2 has a cycle: %v\n", hasCycle2) }
PS C:\Users\linuxnasa\Desktop\Go-Dump> go run .\cycleDetect.go Linked List 1 has a cycle: false Linked List 2 has a cycle: true
Summary
You can learn more about Go from their official website go.dev .