All OOne Operations
Learn this problemProblem statement
A quick note: this problem is backed by a real Spotnana backend interview report that directly named the All O(1) Data Structure task. The report did not reproduce the complete API, examples, or constraints. FastPrep uses the standard inc, dec, getMaxKey, and getMinKey behavior in an operation-list interface. The core task match is about 95%.
Design a data structure that stores string counts and supports incrementing a key, decrementing a key, returning a key with the maximum count, and returning a key with the minimum count.
Process the given operations in order:
["inc", key]: increasekey's count by1, inserting it with count1if needed.["dec", key]: decreasekey's count by1. If the count becomes0, remove the key. The key exists before this operation.["getMaxKey"]: append one key with the maximum count, or""if the structure is empty.["getMinKey"]: append one key with the minimum count, or""if the structure is empty.
Return the outputs from the getMaxKey and getMinKey operations in order.
Interview Follow-up
When the implementation hit a segmentation fault, the candidate explained the doubly linked list and hash map design in detail. The interviewers said they were satisfied with the reasoning even without a fully compiling final submission.
Function
runAllOOne(operations: String[][]) → String[]Examples
Example 1
operations = [["inc","hello"],["inc","hello"],["getMaxKey"],["getMinKey"],["inc","leet"],["getMaxKey"],["getMinKey"]]return = ["hello","hello","hello","leet"]After two increments, hello is both max and min. After leet is inserted once, hello remains max and leet is min.
Example 2
operations = [["getMaxKey"],["inc","a"],["inc","b"],["inc","b"],["dec","b"],["dec","b"],["getMinKey"],["getMaxKey"]]return = ["","a","a"]The first query sees an empty structure. Key b is removed after its count reaches zero, leaving only a.