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.6K 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