Practice 50 Go Lang Collections Coding Questions, TechnoVlogs

Practice 50 Go Lang Collections Coding Questions


Q1. Write a Go program to create a slice of integers and print its length and capacity.  
  Input:   
  Slice: [1, 2, 3, 4, 5]
  Expected Output:  
  Length: 5, Capacity: 5

Q2. Write a Go program to add an element to the end of a slice.  
  Input:   
  Slice: [1, 2, 3], Add: 4
  Expected Output:  
  [1, 2, 3, 4]

Q3. Write a Go program to find the maximum element in an array.  
  Input:   
  Array: [3, 1, 4, 1, 5, 9]
  Expected Output:  
  Maximum: 9

Q4. Write a Go program to reverse a slice of integers.  
  Input:   
  Slice: [1, 2, 3, 4, 5]
  Expected Output:  
  [5, 4, 3, 2, 1]

Q5. Write a Go program to append multiple values to an existing slice.  
  Input:   
  Slice: [1, 2], Append: [3, 4, 5]
  Expected Output:  
  [1, 2, 3, 4, 5]

Q6. Write a Go program to delete an element from a slice at a specific index.  
  Input:   
  Slice: [10, 20, 30, 40], Index: 2
  Expected Output:  
  [10, 20, 40]

Q7. Write a Go program to check if a number is present in a slice.  
  Input:   
  Slice: [5, 10, 15, 20], Number: 15
  Expected Output:  
  Found: 15

Q8. Write a Go program to sum all elements of a slice.  
  Input:   
  Slice: [1, 2, 3, 4, 5]
  Expected Output:  
  Sum: 15

Q9. Write a Go program to find the index of an element in a slice.  
  Input:   
  Slice: [3, 6, 9, 12], Element: 9
  Expected Output:  
  Index: 2

Q10. Write a Go program to create a map from a slice of keys and a slice of values.  
   Input:    
   Keys: ["a", "b", "c"], Values: [1, 2, 3]
   Expected Output:  
   Map: {"a": 1, "b": 2, "c": 3}

Q11. Write a Go program to check if a map contains a specific key.  
   Input:    
   Map: {"a": 1, "b": 2, "c": 3}, Key: "b"
   Expected Output:  
   Key "b" exists in the map.

Q12. Write a Go program to iterate over a map and print all keys and values.  
   Input:    
   Map: {"apple": 5, "banana": 10, "cherry": 15}
   Expected Output:  
   apple: 5
   banana: 10
   cherry: 15

Q13. Write a Go program to merge two slices into a single slice.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [4, 5, 6]
   Expected Output:  
   [1, 2, 3, 4, 5, 6]

Q14. Write a Go program to count the number of occurrences of an element in a slice.  
   Input:    
   Slice: [1, 2, 2, 3, 2, 4], Element: 2
   Expected Output:  
   3 occurrences of 2

Q15. Write a Go program to create a slice of strings and remove a specific string.  
   Input:    
   Slice: ["apple", "banana", "cherry"], Remove: "banana"
   Expected Output:  
   ["apple", "cherry"]

Q16. Write a Go program to find the average of the elements in a slice of integers.  
   Input:    
   Slice: [10, 20, 30, 40]
   Expected Output:  
   Average: 25

Q17. Write a Go program to check if a slice is empty.  
   Input:    
   Slice: []
   Expected Output:  
   The slice is empty.

Q18. Write a Go program to add a key-value pair to a map.  
   Input:    
   Map: {"a": 1, "b": 2}, Key: "c", Value: 3
   Expected Output:  
   Map: {"a": 1, "b": 2, "c": 3}

Q19. Write a Go program to get the keys of a map into a slice.  
   Input:    
   Map: {"a": 1, "b": 2, "c": 3}
   Expected Output:  
   Keys: ["a", "b", "c"]

Q20. Write a Go program to check if a slice is a subset of another slice.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [1, 2]
   Expected Output:  
   Slice2 is a subset of Slice1.

Q21. Write a Go program to find the common elements between two slices.  
   Input:    
   Slice1: [1, 2, 3, 4], Slice2: [3, 4, 5, 6]
   Expected Output:  
   Common elements: [3, 4]

Q22. Write a Go program to create a slice of struct and print the values.  
   Input:    
   Struct: {Name: "Alice", Age: 25}, {Name: "Bob", Age: 30}
   Expected Output:  
   Alice, 25
   Bob, 30

Q23. Write a Go program to find the difference between two slices.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [2, 3, 4]
   Expected Output:  
   Difference: [1]

Q24. Write a Go program to create a map with string keys and slice of integers as values.  
   Input:    
   Map: {"a": [1, 2], "b": [3, 4]}
   Expected Output:  
   {"a": [1, 2], "b": [3, 4]}

Q25. Write a Go program to find if two slices are identical.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [1, 2, 3]
   Expected Output:  
   The slices are identical.

Q26. Write a Go program to create a slice of floats and print the sum of all elements.  
   Input:    
   Slice: [1.5, 2.5, 3.5, 4.5]
   Expected Output:  
   Sum: 12.0

Q27. Write a Go program to find the smallest element in a slice of integers.  
   Input:    
   Slice: [3, 1, 4, 1, 5, 9]
   Expected Output:  
   Smallest: 1

Q28. Write a Go program to check if two slices are equal.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [1, 2, 3]
   Expected Output:  
   The slices are equal.

Q29. Write a Go program to find the average value of a slice of integers.  
   Input:    
   Slice: [10, 20, 30, 40, 50]
   Expected Output:  
   Average: 30

Q30. Write a Go program to create a slice of strings and sort them alphabetically.  
   Input:    
   Slice: ["banana", "apple", "cherry"]
   Expected Output:  
   Sorted: ["apple", "banana", "cherry"]

Q31. Write a Go program to check if a slice contains only unique elements.  
   Input:    
   Slice: [1, 2, 3, 4, 5]
   Expected Output:  
   The slice contains only unique elements.

Q32. Write a Go program to merge two maps into one.  
   Input:    
   Map1: {"a": 1, "b": 2}, Map2: {"c": 3, "d": 4}
   Expected Output:  
   Merged Map: {"a": 1, "b": 2, "c": 3, "d": 4}

Q33. Write a Go program to find the longest string in a slice of strings.  
   Input:    
   Slice: ["apple", "banana", "cherry"]
   Expected Output:  
   Longest String: "banana"

Q34. Write a Go program to iterate over a slice of structs and print all values.  
   Input:    
   Slice of structs: {Name: "John", Age: 25}, {Name: "Jane", Age: 30}
   Expected Output:  
   John, 25
   Jane, 30

Q35. Write a Go program to create a slice of structs and update a field of a struct.  
   Input:    
   Structs: {Name: "Alice", Age: 25}, Update Age: 26
   Expected Output:  
   Updated Struct: {Name: "Alice", Age: 26}

Q36. Write a Go program to convert a slice of integers to a slice of strings.  
   Input:    
   Slice: [1, 2, 3, 4]
   Expected Output:  
   String Slice: ["1", "2", "3", "4"]

Q37. Write a Go program to convert a slice of strings to a slice of integers.  
   Input:    
   Slice: ["1", "2", "3"]
   Expected Output:  
   Integer Slice: [1, 2, 3]

Q38. Write a Go program to count how many times a specific element occurs in a slice of strings.  
   Input:    
   Slice: ["apple", "banana", "apple", "cherry"], Element: "apple"
   Expected Output:  
   "apple" appears 2 times.

Q39. Write a Go program to find the intersection of two slices.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [2, 3, 4]
   Expected Output:  
   Intersection: [2, 3]

Q40. Write a Go program to remove duplicate elements from a slice.  
   Input:    
   Slice: [1, 2, 2, 3, 4, 4]
   Expected Output:  
   [1, 2, 3, 4]

Q41. Write a Go program to shuffle the elements of a slice randomly.  
   Input:    
   Slice: [1, 2, 3, 4, 5]
   Expected Output:  
   Shuffled Slice: [3, 5, 1, 2, 4] (Random order)

Q42. Write a Go program to find the union of two slices.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [3, 4, 5]
   Expected Output:  
   Union: [1, 2, 3, 4, 5]

Q43. Write a Go program to create a map with slice as value and print the map.  
   Input:    
   Map: {"a": [1, 2], "b": [3, 4]}
   Expected Output:  
   {"a": [1, 2], "b": [3, 4]}

Q44. Write a Go program to find the even numbers from a slice of integers.  
   Input:    
   Slice: [1, 2, 3, 4, 5, 6]
   Expected Output:  
   Even Numbers: [2, 4, 6]

Q45. Write a Go program to convert a slice of bytes into a string.  
   Input:    
   Slice: [72, 101, 108, 108, 111]
   Expected Output:  
   String: "Hello"

Q46. Write a Go program to convert a string into a slice of bytes.  
   Input:    
   String: "Hello"
   Expected Output:  
   Byte Slice: [72, 101, 108, 108, 111]

Q47. Write a Go program to copy one slice to another.  
   Input:    
   Slice1: [1, 2, 3], Slice2: [0, 0, 0]
   Expected Output:  
   Slice2 after copy: [1, 2, 3]

Q48. Write a Go program to find the common values between two maps.  
   Input:    
   Map1: {"a": 1, "b": 2, "c": 3}, Map2: {"a": 1, "d": 4, "c": 3}
   Expected Output:  
   Common values: [1, 3]

Q49. Write a Go program to create a slice and check if an element is present in the slice.  
   Input:    
   Slice: [10, 20, 30, 40], Element: 20
   Expected Output:  
   20 is present in the slice.

Q50. Write a Go program to check if a slice is a palindrome.  
   Input:    
   Slice: [1, 2, 3, 2, 1]
   Expected Output:  
   The slice is a palindrome.

Share on Social Media