webounstraininghub.in

Edit Content
Click on the Edit Content button to edit/add the content.

Craft Scalable Solutions with Go

Sharpen Skills, Ace Interviews

Go : Backend Development Interview Questions

Go, also known as Golang, is a statically typed, compiled language designed for simplicity, efficiency, and scalability, particularly in systems programming and cloud-based applications.

Goroutines are lightweight threads managed by the Go runtime, allowing concurrent execution of functions with minimal overhead.

A channel is a communication mechanism that allows Goroutines to exchange data. It ensures safe data sharing without explicit locking.

Go uses explicit error handling, where functions return an error value as the last return value, allowing for easy error checks and handling.

Arrays have a fixed size, while slices are dynamically sized. Slices are more flexible and widely used in Go for managing collections of elements.

Go uses garbage collection for memory management, automatically reclaiming unused memory. It also supports manual memory management with new and make functions.

Go has a strong, static type system with types like int, float, string, and complex types like structs and interfaces. It also supports type inference.

Interfaces in Go define a set of methods a type must implement. They enable polymorphism, allowing different types to be treated similarly based on shared behavior.

A struct is a composite data type that groups together variables under a single name. Each variable within a struct is called a field.

Go handles concurrency primarily through Goroutines and channels, allowing multiple processes to run simultaneously and communicate efficiently.

The defer statement delays the execution of a function until the surrounding function returns, commonly used for resource cleanup.

Go uses packages to organize and reuse code. The standard library provides numerous packages, and custom packages can be created to encapsulate functionality.

In Go, variables are automatically initialized to a zero value depending on their type (e.g., 0 for int, false for bool, "" for string).

Go does not support traditional inheritance but achieves similar functionality using embedding and interfaces for composition.

Concurrency support, fast compilation, cross-platform capabilities, and efficient memory management make Go suitable for cloud computing and microservices.

The go keyword is used to start a new Goroutine, enabling concurrent execution of functions.

Go programs are compiled using the go build command, producing an executable file, which can then be run independently.

Pointers hold the memory address of a variable. Go allows pointer manipulation but does not support pointer arithmetic.

The select statement is used for handling multiple channel operations. It blocks until one of its cases can proceed.

Go includes a built-in testing framework in the testing package, supporting unit tests, benchmarks, and example tests.

The sync package provides basic synchronization primitives such as Mutex, WaitGroup, and Once for safe concurrent programming.

The sync package provides basic synchronization primitives such as Mutex, WaitGroup, and Once for safe concurrent programming.

The context package provides a way to manage deadlines, cancelation signals, and other request-scoped values across API boundaries and Goroutines.

Go includes basic data structures such as arrays, slices, maps, and structs, which are the foundation for building complex data structures.

Go’s compiler produces statically linked binaries that can run on multiple platforms, making cross-platform development straightforward.

Go’s standard library is extensive, well-documented, and optimized, providing tools for networking, cryptography, data encoding, and more, reducing dependency on third-party libraries.

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
package main

import "fmt"

func add(a, b int) int {
return a + b
}

func main() {
fmt.Println(add(3, 4)) // Output: 7
}
package main

import "fmt"

func isEven(num int) bool {
return num%2 == 0
}

func main() {
fmt.Println(isEven(5)) // Output: false
}
package main

import "fmt"

func max(a, b int) int {
if a > b {
return a
}
return b
}

func main() {
fmt.Println(max(5, 10)) // Output: 10
}
package main

import "fmt"

func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}

func main() {
fmt.Println(reverse("hello")) // Output: "olleh"
}
package main

import "fmt"

func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}

func main() {
fmt.Println(factorial(5)) // Output: 120
}
package main

import "fmt"

func firstNonRepeatingChar(s string) rune {
charCount := make(map[rune]int)
for _, ch := range s {
charCount[ch]++
}
for _, ch := range s {
if charCount[ch] == 1 {
return ch
}
}
return '_'
}

func main() {
fmt.Println(string(firstNonRepeatingChar("swiss"))) // Output: w
}
package main

import "fmt"

func fibonacci(n int) []int {
fib := make([]int, n)
fib[0], fib[1] = 0, 1
for i := 2; i < n; i++ {
fib[i] = fib[i-1] + fib[i-2]
}
return fib
}

func main() {
fmt.Println(fibonacci(10)) // Output: [0 1 1 2 3 5 8 13 21 34]
}
package main

import "fmt"

func isPalindrome(s string) bool {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
if r[i] != r[j] {
return false
}
}
return true
}

func main() {
fmt.Println(isPalindrome("racecar")) // Output: true
}
package main

import "fmt"

func sumArray(arr []int) int {
sum := 0
for _, v := range arr {
sum += v
}
return sum
}

func main() {
fmt.Println(sumArray([]int{1, 2, 3, 4})) // Output: 10
}
package main

import "fmt"

func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}

func main() {
fmt.Println(gcd(56, 98)) // Output: 14
}
package main

import "fmt"

func power(base, exp int) int {
if exp == 0 {
return 1
}
return base * power(base, exp-1)
}

func main() {
fmt.Println(power(2, 3)) // Output: 8
}
package main

import "fmt"

func minArray(arr []int) int {
min := arr[0]
for _, v := range arr {
if v < min {
min = v
}
}
return min
}

func main() {
fmt.Println(minArray([]int{3, 5, 7, 2, 8})) // Output: 2
}
package main

import "fmt"

func mergeArrays(arr1, arr2 []int) []int {
merged := make([]int, len(arr1)+len(arr2))
i, j, k := 0, 0, 0
for i < len(arr1) && j < len(arr2) {
if arr1[i] < arr2[j] {
merged[k] = arr1[i]
i++
} else {
merged[k] = arr2[j]
j++
}
k++
}
for i < len(arr1) {
merged[k] = arr1[i]
i++
k++
}
for j < len(arr2) {
merged[k] = arr2[j]
j++
k++
}
return merged
}

func main() {
arr1 := []int{1, 3, 5, 7}
arr2 := []int{2, 4, 6, 8}
fmt.Println(mergeArrays(arr1, arr2)) // Output: [1 2 3 4 5 6 7 8]
}
package main

import (
"fmt"
"strings"
)

func wordCount(s string) map[string]int {
words := strings.Fields(s)
counts := make(map[string]int)
for _, word := range words {
counts[word]++
}
return counts
}

func main() {
fmt.Println(wordCount("this is a test this is only a test"))
// Output: map[a:2 is:2 only:1 test:2 this:2]
}
package main

import "fmt"

func maxSlice(slice []int) int {
max := slice[0]
for _, v := range slice {
if v > max {
max = v
}
}
return max
}

func main() {
fmt.Println(maxSlice([]int{3, 5, 7, 2, 8})) // Output: 8
}
package main

import "fmt"

func removeDuplicates(slice []int) []int {
unique := make(map[int]bool)
result := []int{}
for _, v := range slice {
if !unique[v] {
unique[v] = true
result = append(result, v)
}
}
return result
}

func main() {
fmt.Println(removeDuplicates([]int{1, 2, 2, 3, 4, 4, 5}))
// Output: [1 2 3 4 5]
}
package main

import "fmt"

func bubbleSort(arr []int) {
n := len(arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
}

func main() {
arr := []int{5, 1, 4, 2, 8}
bubbleSort(arr)
fmt.Println(arr) // Output: [1 2 4 5 8]
}



package main

import "fmt"

func binarySearch(arr []int, x int) int {
low, high := 0, len(arr)-1
for low <= high {
mid := (low + high) / 2
if arr[mid] == x {
return mid
} else if arr[mid] < x {
low = mid + 1
} else {
high = mid - 1
}
}
return -1
}

func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(binarySearch(arr, 4)) // Output: 3
}
package main

import "fmt"

func secondLargest(slice []int) int {
first, second := -1<<31, -1<<31
for _, v := range slice {
if v > first {
second = first
first = v
} else if v > second && v != first {
second = v
}
}
return second
}

func main() {
fmt.Println(secondLargest([]int{3, 5, 7, 2, 8})) // Output: 7
}
package main

import "fmt"

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

func main() {
fmt.Println(sumOfDigits(12345)) // Output: 15
}
package main

import (
"fmt"
"sort"
"strings"
)

func areAnagrams(str1, str2 string) bool {
if len(str1) != len(str2) {
return false
}
s1 := strings.Split(str1, "")
s2 := strings.Split(str2, "")
sort.Strings(s1)
sort.Strings(s2)
return strings.Join(s1, "") == strings.Join(s2, "")
}

func main() {
fmt.Println(areAnagrams("listen", "silent")) // Output: true
}
package main

import "fmt"

func longestCommonPrefix(strs []string) string {
if len(strs) == 0 {
return ""
}
prefix := strs[0]
for _, str := range strs[1:] {
for len(str) < len(prefix) || prefix != str[:len(prefix)] {
prefix = prefix[:len(prefix)-1]
if len(prefix) == 0 {
return ""
}
}
}
return prefix
}

func main() {
strs := []string{"flower", "flow", "flight"}
fmt.Println(longestCommonPrefix(strs)) // Output: "fl"
}
package main

import "fmt"

func multiplyMatrices(a, b [][]int) [][]int {
n, m := len(a), len(b[0])
result := make([][]int, n)
for i := range result {
result[i] = make([]int, m)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
for k := 0; k < len(b); k++ {
result[i][j] += a[i][k] * b[k][j]
}
}
}
return result
}

func main() {
a := [][]int{{1, 2}, {3, 4}}
b := [][]int{{5, 6}, {7, 8}}
result := multiplyMatrices(a, b)
fmt.Println(result) // Output: [[19 22] [43 50]]
}
package main

import "fmt"

type Stack []int

func (s *Stack) Push(v int) {
*s = append(*s, v)
}

func (s *Stack) Pop() int {
if len(*s) == 0 {
return -1 // or panic
}
index := len(*s) - 1
element := (*s)[index]
*s = (*s)[:index]
return element
}

func (s *Stack) Peek() int {
if len(*s) == 0 {
return -1 // or panic
}
return (*s)[len(*s)-1]
}

func main() {
var s Stack
s.Push(1)
s.Push(2)
fmt.Println(s.Peek()) // Output: 2
fmt.Println(s.Pop()) // Output: 2
fmt.Println(s.Pop()) // Output: 1
fmt.Println(s.Pop()) // Output: -1
}
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() {
fmt.Println(isPrime(29)) // Output: true
}
package main

import "fmt"

func decimalToBinary(n int) string {
if n == 0 {
return "0"
}
binary := ""
for n > 0 {
binary = fmt.Sprintf("%d", n%2) + binary
n /= 2
}
return binary
}

func main() {
fmt.Println(decimalToBinary(10)) // Output: "1010"
}
package main

import "fmt"

type Node struct {
value int
next *Node
}

func reverseLinkedList(head *Node) *Node {
var prev *Node
curr := head
for curr != nil {
next := curr.next
curr.next = prev
prev = curr
curr = next
}
return prev
}

func printLinkedList(head *Node) {
for head != nil {
fmt.Print(head.value, " ")
head = head.next
}
fmt.Println()
}

func main() {
head := &Node{1, &Node{2, &Node{3, &Node{4, nil}}}}
printLinkedList(head) // Output: 1 2 3 4
head = reverseLinkedList(head)
printLinkedList(head) // Output: 4 3 2 1
}
package main

import "fmt"

type Queue []int

func (q *Queue) Enqueue(v int) {
*q = append(*q, v)
}

func (q *Queue) Dequeue() int {
if len(*q) == 0 {
return -1 // or panic
}
element := (*q)[0]
*q = (*q)[1:]
return element
}

func main() {
var q Queue
q.Enqueue(1)
q.Enqueue(2)
fmt.Println(q.Dequeue()) // Output: 1
fmt.Println(q.Dequeue()) // Output: 2
fmt.Println(q.Dequeue()) // Output: -1
}
package main

import "fmt"

func isPalindrome(s string) bool {
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}

func main() {
fmt.Println(isPalindrome("racecar")) // Output: true
}
Get in touch

We are here to help you & your business

We provide expert guidance, personalized support, and resources to help you excel in your digital marketing career.

Timing
9:00 am - 5:00 pm

    Book Your FREE  Digital Marketing Consultation

    +91 8005836769

    info@webounstraininghub.in