Bird
Raised Fist0
DSA Goprogramming~20 mins

Frequency Counter Pattern Using Hash Map in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Frequency Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Frequency Counter for String Characters
What is the output of the following Go code that counts character frequencies in a string?
DSA Go
package main
import "fmt"
func main() {
    str := "banana"
    freq := make(map[rune]int)
    for _, ch := range str {
        freq[ch]++
    }
    fmt.Println(freq)
}
Amap[98:3 97:1 110:2]
Bmap[97:2 98:2 110:2]
Cmap[110:3 97:2 98:1]
Dmap[97:3 98:1 110:2]
Attempts:
2 left
💡 Hint
Remember that 'a' is rune 97, 'b' is 98, and 'n' is 110 in ASCII.
🧠 Conceptual
intermediate
1:30remaining
Frequency Counter Pattern Purpose
What is the main purpose of using the frequency counter pattern with a hash map in algorithms?
ATo count how many times each element appears efficiently
BTo sort elements in ascending order
CTo reverse the order of elements
DTo remove duplicate elements permanently
Attempts:
2 left
💡 Hint
Think about what a hash map stores when used as a frequency counter.
❓ Predict Output
advanced
2:00remaining
Frequency Counter for Integer Slice
What is the output of this Go code that counts frequencies of integers in a slice?
DSA Go
package main
import "fmt"
func main() {
    nums := []int{2, 3, 2, 4, 3, 2}
    freq := make(map[int]int)
    for _, num := range nums {
        freq[num]++
    }
    fmt.Println(freq)
}
Amap[2:3 3:1 4:2]
Bmap[2:2 3:3 4:1]
Cmap[2:3 3:2 4:1]
Dmap[2:1 3:2 4:3]
Attempts:
2 left
💡 Hint
Count how many times each number appears in the slice.
🔧 Debug
advanced
2:00remaining
Identify the Error in Frequency Counter Code
What error will this Go code produce when trying to count frequencies of characters in a string?
DSA Go
package main
import "fmt"
func main() {
    str := "hello"
    freq := make(map[int]int)
    for _, ch := range str {
        freq[ch]++
    }
    fmt.Println(freq)
}
ACompilation error: missing import
BNo error, outputs map with counts
CCompilation error: cannot use rune as int key in map
DRuntime panic: invalid map key type
Attempts:
2 left
💡 Hint
Check if rune can be used as int key in map[int]int.
🚀 Application
expert
3:00remaining
Using Frequency Counter to Check Anagrams
Given two strings, which Go code correctly uses frequency counters to check if they are anagrams?
A
func isAnagram(s1, s2 string) bool {
    if len(s1) != len(s2) {
        return false
    }
    freq := make(map[rune]int)
    for i := 0; i < len(s1); i++ {
        freq[rune(s1[i])]++
        freq[rune(s2[i])]--
    }
    for _, count := range freq {
        if count != 0 {
            return false
        }
    }
    return true
}
B
func isAnagram(s1, s2 string) bool {
    freq1 := make(map[rune]int)
    freq2 := make(map[rune]int)
    for _, ch := range s1 {
        freq1[ch]++
    }
    for _, ch := range s2 {
        freq2[ch]++
    }
    return freq1 == freq2
}
C
func isAnagram(s1, s2 string) bool {
    freq := make(map[rune]int)
    for _, ch := range s1 {
        freq[ch]++
    }
    for _, ch := range s2 {
        freq[ch]++
    }
    for _, count := range freq {
        if count != 2 {
            return false
        }
    }
    return true
}
D
func isAnagram(s1, s2 string) bool {
    if len(s1) != len(s2) {
        return false
    }
    freq := make(map[rune]int)
    for _, ch := range s1 {
        freq[ch]++
    }
    for _, ch := range s2 {
        freq[ch]--
        if freq[ch] < 0 {
            return false
        }
    }
    return true
}
Attempts:
2 left
💡 Hint
Check which code balances increments and decrements correctly and verifies counts.

Practice

(1/5)
1. What is the main purpose of using a hash map in the Frequency Counter Pattern?
easy
A. To count how many times each item appears in a collection
B. To sort the items in ascending order
C. To remove duplicate items from the collection
D. To reverse the order of items in the collection

Solution

  1. Step 1: Understand the role of a hash map

    A hash map stores key-value pairs, allowing quick lookup and update of counts.
  2. Step 2: Apply to Frequency Counter Pattern

    We use the hash map to keep track of how many times each item appears by incrementing counts.
  3. Final Answer:

    To count how many times each item appears in a collection -> Option A
  4. Quick Check:

    Frequency counting = counting items [OK]
Hint: Hash map keys track items, values track counts [OK]
Common Mistakes:
  • Thinking hash map sorts items
  • Confusing frequency counting with removing duplicates
  • Assuming hash map reverses order
2. Which of the following Go code snippets correctly initializes an empty hash map to count frequencies of strings?
easy
A. freq := map[int]string{}
B. freq := make(map[string]int)
C. freq := []string{}
D. freq := make([]int, 0)

Solution

  1. Step 1: Identify correct map initialization syntax

    In Go, make(map[string]int) creates a map with string keys and int values.
  2. Step 2: Check other options

    freq := map[int]string{} reverses key-value types, C and D create slices, not maps.
  3. Final Answer:

    freq := make(map[string]int) -> Option B
  4. Quick Check:

    Correct map syntax = make(map[string]int) [OK]
Hint: Use make(map[keyType]valueType) for maps [OK]
Common Mistakes:
  • Using slices instead of maps
  • Swapping key and value types
  • Forgetting to use make() for map initialization
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
  words := []string{"apple", "banana", "apple", "orange", "banana", "apple"}
  freq := make(map[string]int)
  for _, w := range words {
    freq[w]++
  }
  fmt.Println(freq["apple"])
}
medium
A. 1
B. 2
C. 3
D. 0

Solution

  1. Step 1: Count occurrences of "apple" in the slice

    "apple" appears at indices 0, 2, and 5, total 3 times.
  2. Step 2: Check frequency map update and print

    freq[w]++ increments count for each word; freq["apple"] is 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Count of "apple" = 3 [OK]
Hint: Count each occurrence, then print frequency [OK]
Common Mistakes:
  • Counting unique words instead of occurrences
  • Off-by-one errors in counting
  • Printing wrong key frequency
4. Identify the error in the following Go code that tries to count frequencies of integers:
package main
import "fmt"
func main() {
  nums := []int{1, 2, 2, 3, 1}
  freq := make(map[int]int)
  for i := 0; i <= len(nums); i++ {
    freq[nums[i]]++
  }
  fmt.Println(freq)
}
medium
A. Loop condition should be i < len(nums), not i <= len(nums)
B. Map initialization is incorrect
C. Frequency increment syntax is wrong
D. Slice nums is empty

Solution

  1. Step 1: Check loop boundary conditions

    Loop uses i <= len(nums), which causes index out of range error because last index is len(nums)-1.
  2. Step 2: Correct loop condition

    Change to i < len(nums) to avoid accessing invalid index.
  3. Final Answer:

    Loop condition should be i < len(nums), not i <= len(nums) -> Option A
  4. Quick Check:

    Index out of range fixed by i < len(nums) [OK]
Hint: Use i < len(slice) to avoid index errors [OK]
Common Mistakes:
  • Using <= instead of < in loops
  • Misinitializing the map
  • Incorrect frequency increment syntax
5. Given two slices of integers, a and b, which Go code snippet correctly checks if b contains the same frequency of elements as a using the Frequency Counter Pattern?
hard
A. freqA := make(map[int]int) freqB := make(map[int]int) for _, v := range a { freqA[v]++ } for _, v := range b { freqB[v]++ } for k, v := range freqA { if freqB[k] != v { fmt.Println(false) return } } fmt.Println(true)
B. freq := make(map[int]int) for _, v := range a { freq[v] = 1 } for _, v := range b { if freq[v] == 0 { fmt.Println(false) return } } fmt.Println(true)
C. freq := make(map[int]int) for i := 0; i < len(a); i++ { freq[a[i]]++ freq[b[i]]++ } for _, count := range freq { if count%2 != 0 { fmt.Println(false) return } } fmt.Println(true)
D. freq := make(map[int]int) for _, v := range a { freq[v]++ } for _, v := range b { freq[v]-- } for _, count := range freq { if count != 0 { fmt.Println(false) return } } fmt.Println(true)

Solution

  1. Step 1: Build frequency map for a

    Increment counts for each element in a.
  2. Step 2: Decrement frequency map for b

    Decrement counts for each element in b.
  3. Step 3: Check if all counts are zero

    If any count is not zero, frequencies differ; otherwise, they match.
  4. Final Answer:

    freq := make(map[int]int) for _, v := range a { freq[v]++ } for _, v := range b { freq[v]-- } for _, count := range freq { if count != 0 { fmt.Println(false) return } } fmt.Println(true) -> Option D
  5. Quick Check:

    Increment a, decrement b, check zero counts [OK]
Hint: Increment for a, decrement for b, then check zeros [OK]
Common Mistakes:
  • Only checking presence, not frequency
  • Using separate maps without comparing properly
  • Assuming counts add up without balancing increments and decrements
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