Bird
Raised Fist0
DSA Goprogramming~10 mins

Substring Search Patterns in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the pattern exists in the text using Go's built-in function.

DSA Go
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "hello world"
	pattern := "world"
	found := strings.[1](text, pattern)
	fmt.Println(found)
}
Drag options to blanks, or click blank then click option'
AContains
BIndex
CHasPrefix
DSplit
Attempts:
3 left
💡 Hint
Common Mistakes
Using Index returns the position, not a boolean.
HasPrefix checks only the start of the string.
Split divides the string and does not check for substring presence.
2fill in blank
medium

Complete the code to find the first index of the pattern in the text.

DSA Go
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "golang is fun"
	pattern := "is"
	index := strings.[1](text, pattern)
	fmt.Println(index)
}
Drag options to blanks, or click blank then click option'
AContains
BLastIndex
CIndex
DHasSuffix
Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains returns a boolean, not the index.
LastIndex returns the last occurrence, not the first.
HasSuffix checks the end of the string.
3fill in blank
hard

Fix the error in the code to correctly check if the pattern is at the start of the text.

DSA Go
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "start here"
	pattern := "start"
	result := strings.[1](text, pattern)
	fmt.Println(result)
}
Drag options to blanks, or click blank then click option'
AHasSuffix
BHasPrefix
CContains
DIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using HasSuffix checks the end, not the start.
Contains checks anywhere in the string.
Index returns position, not a boolean.
4fill in blank
hard

Fill both blanks to create a map of words and their lengths for words longer than 3 characters.

DSA Go
package main

import "fmt"

func main() {
	words := []string{"go", "code", "play", "fun"}
	lengths := map[string]int{
		[1]: [2],
	}
	fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C"word"
Dlen("word")
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of variables.
Using length of the string literal 'word' instead of the variable.
5fill in blank
hard

Fill all three blanks to create a filtered map of words and lengths where length is greater than 3.

DSA Go
package main

import "fmt"

func main() {
	words := []string{"go", "code", "play", "fun"}
	lengths := map[string]int{}
	for _, [1] := range words {
		if [2] [3] 3 {
			lengths[[1]] = len([1])
		}
	}
	fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the loop.
Using less than instead of greater than.
Comparing the word string instead of its length.

Practice

(1/5)
1. What does the strings.Contains(s, substr) function in Go do?
easy
A. Returns the index of substr in s
B. Checks if substr is present anywhere in s
C. Splits s by substr
D. Replaces substr with another string in s

Solution

  1. Step 1: Understand the function purpose

    strings.Contains checks if one string is inside another.
  2. Step 2: Check the return type

    It returns true if substr is found anywhere in s, otherwise false.
  3. Final Answer:

    Checks if substr is present anywhere in s -> Option B
  4. Quick Check:

    Contains means presence check = A [OK]
Hint: Contains means checking presence, not position or replacement [OK]
Common Mistakes:
  • Confusing Contains with Index or Replace functions
  • Thinking it returns position instead of boolean
  • Assuming it modifies the original string
2. Which of the following is the correct syntax to find the index of substring "go" in string s using Go's standard library?
easy
A. index := strings.Index(s, "go")
B. index := s.IndexOf("go")
C. index := s.find("go")
D. index := strings.Find(s, "go")

Solution

  1. Step 1: Recall Go's strings package functions

    The correct function to find substring index is strings.Index.
  2. Step 2: Check syntax correctness

    The function is called as strings.Index(s, "go"), returning the first index or -1 if not found.
  3. Final Answer:

    index := strings.Index(s, "go") -> Option A
  4. Quick Check:

    Index function in strings package = B [OK]
Hint: Use strings.Index for substring position in Go [OK]
Common Mistakes:
  • Using methods on string variable directly (strings are not objects)
  • Using Python or JavaScript syntax like find or IndexOf
  • Calling non-existent functions like strings.Find
3. What will be the output of the following Go code?
package main
import (
  "fmt"
  "strings"
)
func main() {
  s := "hello golang"
  pos := strings.Index(s, "go")
  fmt.Println(pos)
}
medium
A. 6
B. -1
C. 0
D. Error

Solution

  1. Step 1: Understand strings.Index behavior

    It returns the first index where substring "go" appears in "hello golang".
  2. Step 2: Find substring position

    Counting from 0: h(0) e(1) l(2) l(3) o(4) ' '(5) g(6) o(7)... So "go" starts at index 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Index of "go" in "hello golang" = 6 [OK]
Hint: Count characters from zero to find substring index [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Assuming substring not found returns 0
  • Confusing Index with Contains boolean
4. Identify the error in the following Go code snippet for substring search:
package main
import (
  "fmt"
  "strings"
)
func main() {
  s := "golang"
  if s.Contains("go") {
    fmt.Println("Found")
  }
}
medium
A. Incorrect string literal syntax
B. Missing import for fmt package
C. No error, code runs fine
D. strings package has no Contains method on string variable

Solution

  1. Step 1: Check method call on string variable

    In Go, strings are not objects and do not have methods like Contains directly.
  2. Step 2: Correct usage of Contains function

    The correct call is strings.Contains(s, "go") from the strings package, not s.Contains("go").
  3. Final Answer:

    strings package has no Contains method on string variable -> Option D
  4. Quick Check:

    Use strings.Contains(s, substr), not s.Contains(substr) [OK]
Hint: Call Contains from strings package, not on string variable [OK]
Common Mistakes:
  • Calling methods on string variables as if they were objects
  • Forgetting to import strings package
  • Assuming Contains returns index
5. Given a list of strings words := []string{"go", "golang", "gopher", "gone"}, which code snippet correctly finds all words containing substring "go" and stores them in result?
hard
A. result := []string{} for _, w := range words { if strings.Index(w, "go") > 5 { result = append(result, w) } }
B. var result []string for i := 0; i < len(words); i++ { if words[i].Contains("go") { result = append(result, words[i]) } }
C. var result []string for _, w := range words { if strings.Contains(w, "go") { result = append(result, w) } }
D. result := []string{} for _, w := range words { if strings.Index(w, "go") == -1 { result = append(result, w) } }

Solution

  1. Step 1: Identify correct substring check

    Use strings.Contains(w, "go") to check if substring exists.
  2. Step 2: Append matching words to result slice

    For each word containing "go", append it to result slice.
  3. Final Answer:

    var result []string for _, w := range words { if strings.Contains(w, "go") { result = append(result, w) } } -> Option C
  4. Quick Check:

    Contains + append in loop = D [OK]
Hint: Use strings.Contains inside loop and append matches [OK]
Common Mistakes:
  • Calling Contains as method on string variable
  • Appending words that do NOT contain substring
  • Checking Index == -1 instead of != -1
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard