10 Golang Basic Exercises for Advanced with Solutions

Master advanced Golang skills with our comprehensive list of top 10 exercises. Dive into coding challenges that improve your understanding and proficiency in Golang, setting a solid foundation for professional-level challenges. Start your journey to Golang mastery today!

Learning Objectives:

Master advanced Go concepts such as microservices, performance optimization, testing, dependency management, and integrating with databases and web frameworks.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Implement a Goroutine-based Fibonacci sequence generator that uses channels to produce the first n Fibonacci numbers.

Required Input:

n = 10

Expected Output:

0 1 1 2 3 5 8 13 21 34 

Code In Go

package main import "fmt" func fibonacci(n int) <-chan int { ch := make(chan int) go func() { // Your code here }() return ch } func main() { n := 10 fibCh := fibonacci(n) for i := 0; i < n; i++ { fmt.Print(<-fibCh, " ") } }

Run Code?

Click Run Button to view compiled output

2. Create a generic Map type that supports integer keys and allows inserting, retrieving, and deleting key-value pairs.

Required Input:

m := NewMap[int, string](); m.Set(1, "Go")

Expected Output:

Go

Code In Go

package main import "fmt" type Map[K comparable, V any] struct { // Your code here } func NewMap[K comparable, V any]() *Map[K, V] { return &Map[K, V]{} } func main() { m := NewMap[int, string]() m.Set(1, "Go") fmt.Println(m.Get(1)) // Expected output: "Go" }

Run Code?

Click Run Button to view compiled output

3. Implement Merge Sort using Goroutines to divide and merge subarrays in parallel.

Required Input:

[9, 5, 1, 3, 7]

Expected Output:

[1 3 5 7 9]

Code In Go

package main import "fmt" func mergeSort(arr []int) []int { if len(arr) < 2 { return arr } mid := len(arr) / 2 left := mergeSort(arr[:mid]) right := mergeSort(arr[mid:]) return merge(left, right) } func merge(left, right []int) []int { // Your code here } func main() { arr := []int{9, 5, 1, 3, 7} sortedArr := mergeSort(arr) fmt.Println(sortedArr) }

Run Code?

Click Run Button to view compiled output

4. Create a Trie data structure to support efficient insertion and lookup of words.

Required Input:

Insert("go"); Search("go")

Expected Output:

true

Code In Go

package main import "fmt" type Trie struct { // Your code here } func NewTrie() *Trie { return &Trie{} } func main() { trie := NewTrie() trie.Insert("go") fmt.Println(trie.Search("go")) // Expected Output: true fmt.Println(trie.Search("golang")) // Expected Output: false }

Run Code?

Click Run Button to view compiled output

5. Implement Floyd's cycle detection algorithm (Tortoise and Hare) to determine if a linked list contains a cycle.

Required Input:

Linked List: [1 -> 2 -> 3 -> 4 -> 2 (cycle)]

Expected Output:

true

Code In Go

package main import "fmt" type ListNode struct { val int next *ListNode } func hasCycle(head *ListNode) bool { // Your code here } func main() { node1 := &ListNode{val: 1} node2 := &ListNode{val: 2} node3 := &ListNode{val: 3} node4 := &ListNode{val: 4} node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node2 // Creates a cycle fmt.Println(hasCycle(node1)) // Expected Output: true }

Run Code?

Click Run Button to view compiled output

6. Implement a struct with methods to safely increment, decrement, and retrieve a counter using sync.Mutex or sync/atomic.

Required Input:

Increment(); Get()

Expected Output:

100

Code In Go

package main import ( "fmt" "sync" ) type SafeCounter struct { // Your code here } func main() { counter := &SafeCounter{} var wg sync.WaitGroup for i := 0; i < 100; i++ { wg.Add(1) go func() { counter.Increment() wg.Done() }() } wg.Wait() fmt.Println(counter.Get()) // Expected Output: 100 }

Run Code?

Click Run Button to view compiled output

7. Write a function that checks if a given string of parentheses ((), {}, []) is balanced using a stack-based approach.

Required Input:

"({[]})"

Expected Output:

true

Code In Go

package main import "fmt" func isBalanced(s string) bool { // Your code here } func main() { fmt.Println(isBalanced("({[]})")) // Expected Output: true fmt.Println(isBalanced("({[})")) // Expected Output: false }

Run Code?

Click Run Button to view compiled output

8. Implement an efficient function to find the first non-repeating character in a given string.

Required Input:

"gogolang"

Expected Output:

l

Code In Go

package main import "fmt" func firstNonRepeatingChar(s string) rune { // Your code here } func main() { fmt.Println(string(firstNonRepeatingChar("gogolang"))) // Expected Output: "l" }

Run Code?

Click Run Button to view compiled output

9. Implement a Min-Heap with insertion, deletion, and heapify operations using a slice.

Required Input:

Insert(5); Insert(3); GetMin()

Expected Output:

3

Code In Go

package main import "fmt" type MinHeap struct { // Your code here } func main() { heap := &MinHeap{} heap.Insert(5) heap.Insert(3) heap.Insert(8) heap.Insert(1) fmt.Println(heap.ExtractMin()) // Expected Output: 1 fmt.Println(heap.ExtractMin()) // Expected Output: 3 }

Run Code?

Click Run Button to view compiled output

10. Implement an efficient O(n log n) solution to find the longest increasing subsequence in an array.

Required Input:

[10, 9, 2, 5, 3, 7, 101, 18]

Expected Output:

4

Code In Go

package main import "fmt" func longestIncreasingSubsequence(arr []int) int { // Your code here } func main() { arr := []int{10, 9, 2, 5, 3, 7, 101, 18} fmt.Println(longestIncreasingSubsequence(arr)) // Expected Output: 4 }

Run Code?

Click Run Button to view compiled output

ad vertical