Bird
Raised Fist0
DSA Goprogramming~5 mins

Substring Search Patterns in DSA Go - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the main goal of substring search algorithms?
To find the position(s) where a smaller string (pattern) appears within a larger string (text).
Click to reveal answer
beginner
Explain the naive substring search approach.
It checks every position in the text to see if the pattern matches starting there, moving one by one until a match is found or the text ends.
Click to reveal answer
intermediate
What is the key idea behind the Knuth-Morris-Pratt (KMP) algorithm?
KMP uses a precomputed table to skip unnecessary comparisons by remembering how the pattern matches itself, so it doesn't restart from the beginning after a mismatch.
Click to reveal answer
intermediate
How does the Rabin-Karp algorithm speed up substring search?
It uses a hash function to quickly compare the pattern's hash with substrings' hashes in the text, reducing the number of direct character comparisons.
Click to reveal answer
intermediate
Why is preprocessing important in substring search algorithms like KMP?
Preprocessing builds data structures (like prefix tables) that help the algorithm avoid repeating work, making the search faster overall.
Click to reveal answer
Which substring search algorithm uses a prefix table to avoid rechecking characters?
AKnuth-Morris-Pratt (KMP)
BNaive search
CRabin-Karp
DBubble sort
What does the Rabin-Karp algorithm use to quickly compare substrings?
AHash values
BPrefix table
CSorting
DRecursion
In naive substring search, how many positions in the text are checked in the worst case?
AOnly one
BAll positions where pattern fits
CNone
DHalf of the text
Which of these is NOT a substring search algorithm?
AKnuth-Morris-Pratt
BNaive search
CRabin-Karp
DQuickSort
What is the main advantage of using KMP over naive search?
AUses less memory
BUses hashing
CSkips unnecessary comparisons
DWorks only on sorted text
Describe how the Knuth-Morris-Pratt algorithm improves substring search compared to naive search.
Think about how KMP remembers pattern matches to avoid starting over.
You got /4 concepts.
    Explain the role of hashing in the Rabin-Karp substring search algorithm.
    Focus on how hashing helps quickly find potential matches.
    You got /4 concepts.

      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