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
All O'one Data Structure INPUT Operations Sequence: AllOne() inc("hello") inc("hello") getMaxKey() getMinKey() inc("leet") getMaxKey() getMinKey() HashMap + Doubly Linked List: HashMap key --> node "hello" --> N1 "leet" --> N2 DLL Nodes cnt:1 cnt:2 O(1) access to min/max ALGORITHM STEPS 1 Initialize Structure HashMap + DLL with head/tail 2 inc("hello") x2 Create node, move to cnt=2 "hello":2 3 inc("leet") Add new node at cnt=1 "leet":1 "hello":2 4 Get Min/Max Keys Head.next=min, Tail.prev=max MIN: head.next MAX: tail.prev min="leet" max="hello" Approach: HASH FINAL RESULT Final Data Structure State: HEAD cnt:0 "leet" cnt:1 "hello" cnt:2 TAIL cnt:inf Output Array: [ null, null, null, "hello", "hello", null, "hello", "leet" ] OK - All Operations O(1) getMaxKey() = "hello" (cnt:2) getMinKey() = "leet" (cnt:1) Time: O(1) | Space: O(n) Key Insight: Use a HashMap for O(1) key-to-node lookup, combined with a Doubly Linked List where nodes are sorted by count. Each DLL node contains a set of keys with the same frequency. The head.next gives minimum count keys, tail.prev gives maximum count keys - both in O(1) time! TutorialsPoint - All O'one Data Structure | Hash Approach
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