Tap here to switch tabs
Problem
Submissions

Problem: All O`one Data Structure

hard
40 min
Explore how to design and implement the AllOne data structure that efficiently tracks string key frequencies with O(1) updates and queries. Learn to handle increment and decrement operations while retrieving keys with the highest and lowest counts. This lesson equips you with custom data structure skills essential for coding interviews.

Statement

Design a data structure that tracks the frequency of string keys and allows for efficient updates and queries.

Implement the AllOne class with these methods:

  • Constructor: Initializes the data structure.

  • inc(String key): Increases the count of the given key by 11. If the key is absent, insert it with a count of 11.

  • dec(String key): Decreases the count of the given key by 11. If the count becomes 00 after decrementing, remove the key entirely. The assumption is that the key exists when this function is called.

  • getMaxKey(): Returns any one key with the highest count. If the data structure is empty, return an empty string.

  • getMinKey(): Returns any one key with the lowest count. If the data structure is empty, return an empty string.

Note: All operations must be performed in average O(1)O(1) time complexity.

Constraints:

  • 11 \leq key.length 10\leq 10

  • key consists only of lowercase English letters.

  • It is guaranteed that each call to dec is made with a key that exists in the data structure.

  • At most 5×1025 \times 10^2 calls will be made to inc, dec, getMaxKey, and getMinKey.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: All O`one Data Structure

hard
40 min
Explore how to design and implement the AllOne data structure that efficiently tracks string key frequencies with O(1) updates and queries. Learn to handle increment and decrement operations while retrieving keys with the highest and lowest counts. This lesson equips you with custom data structure skills essential for coding interviews.

Statement

Design a data structure that tracks the frequency of string keys and allows for efficient updates and queries.

Implement the AllOne class with these methods:

  • Constructor: Initializes the data structure.

  • inc(String key): Increases the count of the given key by 11. If the key is absent, insert it with a count of 11.

  • dec(String key): Decreases the count of the given key by 11. If the count becomes 00 after decrementing, remove the key entirely. The assumption is that the key exists when this function is called.

  • getMaxKey(): Returns any one key with the highest count. If the data structure is empty, return an empty string.

  • getMinKey(): Returns any one key with the lowest count. If the data structure is empty, return an empty string.

Note: All operations must be performed in average O(1)O(1) time complexity.

Constraints:

  • 11 \leq key.length 10\leq 10

  • key consists only of lowercase English letters.

  • It is guaranteed that each call to dec is made with a key that exists in the data structure.

  • At most 5×1025 \times 10^2 calls will be made to inc, dec, getMaxKey, and getMinKey.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths