All O`one Data Structure - Problem

Design a powerful data structure that tracks string frequencies and provides instant access to both the most and least frequent strings!

You need to implement the AllOne class with these operations:

  • AllOne(): Initialize the data structure
  • inc(String key): Increment the count of key by 1. If the key doesn't exist, insert it with count 1
  • dec(String key): Decrement the count of key by 1. If the count becomes 0, remove the key completely
  • getMaxKey(): Return any key with the maximum count (or empty string if none exist)
  • getMinKey(): Return any key with the minimum count (or empty string if none exist)

The Challenge: All operations must run in O(1) average time complexity! This means you can't simply iterate through all keys to find min/max.

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"] [[], ["hello"], ["hello"], [], [], ["leet"], [], []]
โ€บ Output: [null, null, null, "hello", "hello", null, "hello", "leet"]
๐Ÿ’ก Note: Initialize AllOne, increment "hello" twice (count=2), then add "leet" once (count=1). "hello" has max count, "leet" has min count.
example_2.py โ€” Decrement Operations
$ Input: ["AllOne", "inc", "inc", "inc", "dec", "getMaxKey", "getMinKey"] [[], ["a"], ["b"], ["b"], ["b"], [], []]
โ€บ Output: [null, null, null, null, null, "b", "a"]
๐Ÿ’ก Note: After operations: "a" has count 1, "b" has count 2 (incremented twice, decremented once). "b" is max, "a" is min.
example_3.py โ€” Edge Case Empty
$ Input: ["AllOne", "getMaxKey", "getMinKey", "inc", "dec", "getMaxKey", "getMinKey"] [[], [], [], ["test"], ["test"], [], []]
โ€บ Output: [null, "", "", null, null, "", ""]
๐Ÿ’ก Note: Initially empty, so getMaxKey/getMinKey return empty strings. After inc then dec "test", it's removed, so empty again.

Constraints

  • 1 โ‰ค key.length โ‰ค 10
  • key consists of lowercase English letters
  • At most 5 ร— 104 calls will be made to inc, dec, getMaxKey, and getMinKey
  • All function calls must have O(1) average time complexity

Visualization

Tap to expand
๐ŸŽญ VIP Nightclub Tier Management System๐Ÿ‘ฅ BRONZE1 VisitSarah, Mike๐Ÿฅˆ SILVER3 VisitsAlex๐Ÿฅ‡ GOLD5 VisitsEmma๐Ÿ’Ž DIAMOND8 VisitsJohn๐Ÿ—‚๏ธ Guest Directory (Hash Map)"Sarah" โ†’ Bronze"Alex" โ†’ Silver"Emma" โ†’ Gold"John" โ†’ DiamondgetMinKey() = "Sarah"getMaxKey() = "John"โšก All Operations: O(1) Time!๐ŸŽฏ Instant tier lookup and promotion/demotion๐Ÿ† Direct access to VIP and newest members
Understanding the Visualization
1
Setup Structure
Create membership tiers (frequency levels) connected in order
2
Guest Arrives
Move guest to appropriate tier or create new tier if needed
3
Update Directory
Hash map tracks each guest's current tier for instant lookup
4
VIP Access
Instantly identify top-tier (max) and new members (min)
Key Takeaway
๐ŸŽฏ Key Insight: By organizing data into frequency tiers (doubly-linked list) with instant guest lookup (hash map), we achieve O(1) operations for a real-time VIP management system!
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 28
67.5K Views
Medium-High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen