Bird
Raised Fist0
DSA Goprogramming

Palindrome Detection in DSA Go

Choose your learning style10 modes available
Mental Model
A palindrome reads the same forwards and backwards. We check if the first and last characters match, then move inward.
Analogy: Like checking if a word is the same when you read it from left to right or right to left, like 'racecar' or 'level'.
r -> a -> c -> e -> c -> a -> r -> null
↑                 ↑
start             end
Dry Run Walkthrough
Input: string: "racecar"
Goal: Check if the string reads the same forwards and backwards
Step 1: Compare first and last characters 'r' and 'r'
r -> a -> c -> e -> c -> a -> r -> null
↑                 ↑
Why: We start by checking the outermost characters to see if they match
Step 2: Move inward: compare 'a' and 'a'
r -> a -> c -> e -> c -> a -> r -> null
  ↑             ↑
Why: Since outer characters matched, check next inner pair
Step 3: Move inward: compare 'c' and 'c'
r -> a -> c -> e -> c -> a -> r -> null
    ↑         ↑
Why: Continue checking pairs moving inward
Step 4: Middle character 'e' reached, no mismatch found
r -> a -> c -> e -> c -> a -> r -> null
      ↑
Why: Middle reached means all pairs matched, so string is palindrome
Result:
r -> a -> c -> e -> c -> a -> r -> null
Palindrome: true
Annotated Code
DSA Go
package main

import (
	"fmt"
)

func isPalindrome(s string) bool {
	start, end := 0, len(s)-1
	for start < end {
		if s[start] != s[end] {
			return false // mismatch found, not palindrome
		}
		start++ // move start pointer inward
		end--   // move end pointer inward
	}
	return true // all pairs matched
}

func main() {
	input := "racecar"
	result := isPalindrome(input)
	fmt.Printf("%s\nPalindrome: %v\n", input, result)
}
for start < end {
loop to compare characters from both ends moving inward
if s[start] != s[end] {
check if characters at start and end differ
return false // mismatch found, not palindrome
stop early if mismatch found
start++ // move start pointer inward
advance start pointer to next character
end-- // move end pointer inward
move end pointer to previous character
return true // all pairs matched
if loop completes, string is palindrome
OutputSuccess
racecar Palindrome: true
Complexity Analysis
Time: O(n) because we compare pairs of characters from start to end once
Space: O(1) because we use only two pointers and no extra space
vs Alternative: Compared to reversing the string and comparing, this method uses less space and can stop early on mismatch
Edge Cases
empty string
returns true because empty string reads same forwards and backwards
DSA Go
for start < end {
single character string
returns true because single character is palindrome
DSA Go
for start < end {
string with mismatch early
returns false immediately when first mismatch found
DSA Go
if s[start] != s[end] {
When to Use This Pattern
When you need to check if a sequence reads the same forwards and backwards, use two pointers moving inward from both ends to compare pairs.
Common Mistakes
Mistake: Comparing characters but forgetting to move pointers inward
Fix: Increment start and decrement end pointers inside the loop after comparison
Mistake: Using a loop that goes beyond the middle causing redundant checks
Fix: Use condition start < end to stop at the middle
Summary
Checks if a string reads the same forwards and backwards by comparing pairs of characters.
Use when you want to verify if a word or sequence is a palindrome.
The key insight is to compare characters from both ends moving inward until the middle.

Practice

(1/5)
1. What does a palindrome string mean in simple terms?
easy
A. A string with all unique characters
B. A string that contains only vowels
C. A string that reads the same forwards and backwards
D. A string that ends with a special character

Solution

  1. Step 1: Understand palindrome definition

    A palindrome is a word or phrase that reads the same from left to right and right to left.
  2. Step 2: Match options with definition

    A string that reads the same forwards and backwards matches this definition exactly, others describe different string properties.
  3. Final Answer:

    A string that reads the same forwards and backwards -> Option C
  4. Quick Check:

    Palindrome = Same forwards and backwards [OK]
Hint: Check if string equals its reverse [OK]
Common Mistakes:
  • Confusing palindrome with unique characters
  • Thinking palindrome means only vowels
  • Assuming palindrome ends with special character
2. Which Go code snippet correctly checks if a string s is a palindrome?
easy
A. for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true
B. for i := 0; i < len(s); i++ { if s[i] == s[i+1] { return true } } return false
C. for i := 0; i < len(s)/2; i++ { if s[i] == s[len(s)-1-i] { return false } } return true
D. for i := 0; i < len(s); i++ { if s[i] != s[i] { return false } } return true

Solution

  1. Step 1: Analyze palindrome check logic

    We compare characters from start and end moving towards center. If any pair differs, return false.
  2. Step 2: Match code with logic

    for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true correctly compares s[i] with s[len(s)-1-i] for half the string length and returns false if mismatch found.
  3. Final Answer:

    for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true -> Option A
  4. Quick Check:

    Compare pairs from ends = for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true [OK]
Hint: Compare characters from ends to middle [OK]
Common Mistakes:
  • Checking only adjacent characters
  • Returning false when characters match
  • Comparing character with itself incorrectly
3. What is the output of this Go code snippet?
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
}

fmt.Println(isPalindrome("level"))
fmt.Println(isPalindrome("hello"))
medium
A. false false
B. false true
C. true true
D. true false

Solution

  1. Step 1: Check "level" palindrome

    "level" reads same forwards and backwards, so function returns true.
  2. Step 2: Check "hello" palindrome

    "hello" reversed is "olleh", different, so function returns false.
  3. Final Answer:

    true false -> Option D
  4. Quick Check:

    "level"=true, "hello"=false [OK]
Hint: Test string and its reverse equality [OK]
Common Mistakes:
  • Assuming all strings are palindrome
  • Mixing output order
  • Ignoring case sensitivity
4. Find the error in this Go function that checks palindrome:
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
}
medium
A. The loop condition should be 'i < len(s)/2' to avoid index out of range
B. The comparison should be 's[i] == s[len(s)-1-i]' to return true early
C. The function should return false at the end instead of true
D. The loop should start from 'i := 1' instead of 0

Solution

  1. Step 1: Analyze loop boundary

    The loop uses 'i <= len(s)/2', which causes 'i' to reach len(s)/2 inclusive, possibly exceeding index for odd length strings.
  2. Step 2: Correct loop condition

    Changing to 'i < len(s)/2' ensures no index out of range error and proper comparison only up to middle.
  3. Final Answer:

    The loop condition should be 'i < len(s)/2' to avoid index out of range -> Option A
  4. Quick Check:

    Loop boundary off by one causes error [OK]
Hint: Use '<' not '<=' for half-length loops [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Returning wrong boolean at end
  • Starting loop from 1 instead of 0
5. You want to detect if a string is a palindrome ignoring case and spaces. Which approach is best in Go?
hard
A. Check palindrome directly without changes
B. Remove spaces, convert to lowercase, then check palindrome normally
C. Only convert to lowercase, ignore spaces
D. Reverse string and compare without any changes

Solution

  1. Step 1: Understand problem requirements

    We must ignore spaces and case differences when checking palindrome.
  2. Step 2: Prepare string correctly

    Removing spaces and converting all letters to lowercase normalizes the string for accurate palindrome check.
  3. Step 3: Check palindrome

    After normalization, use standard palindrome check comparing characters from ends.
  4. Final Answer:

    Remove spaces, convert to lowercase, then check palindrome normally -> Option B
  5. Quick Check:

    Normalize string before check = Remove spaces, convert to lowercase, then check palindrome normally [OK]
Hint: Clean string first: lowercase + remove spaces [OK]
Common Mistakes:
  • Ignoring spaces in palindrome check
  • Not converting to lowercase
  • Comparing raw strings directly
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