Skip to main content
Back to problems
Leetcode
Medium
Hash Maps
Design
Google
Counter

Design a counter that supports updating counts for string keys and querying them efficiently.

Acceptance 0%
Problem Statement

Problem

Implement a counter data structure that stores integer counts for string keys.

Support operations to:

  • increase the count for a key by 1
  • decrease the count for a key by 1, removing the key if its count becomes 0
  • return the current count for a key

The counter should behave like a mutable in-memory frequency table.

Notes

  • If a key does not exist, its count is considered 0.
  • Decrementing a missing key should have no effect.
  • All operations should be efficient.

Input Format

A sequence of operations on a counter object, each operating on a string key.

Output Format

Return the result of each query operation, and keep internal state updated after each modification.

Constraints

  • Keys are non-empty strings.
  • Counts are integers and never negative.
  • Operations should be near O(1) on average.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.