As reported: Coupang Staff loop (2025), round 1: 1 hour of online code pairing with optimal, working code expected. Design a data structure that stores the counts of strings and can return the strings with the minimum and maximum counts; every function in average
O(1)time.
This is [LeetCode 432 — All O`one Data Structure](/all-oone-data-structure).
Implement AllOne:
AllOne()— initialises the structure.inc(key)— increments the count ofkeyby 1. Ifkeyis not present, insert it with count 1.dec(key)— decrements the count ofkeyby 1. If the count becomes 0, removekey. It is guaranteed thatkeyexists before the decrement.getMaxKey()— returns one of the keys with the maximal count, or""if there are none.getMinKey()— returns one of the keys with the minimal count, or""if there are none.
Each function must run in O(1) average time.
Example
["AllOne","inc","inc","getMaxKey","getMinKey","inc","getMaxKey","getMinKey"]
[[],["hello"],["hello"],[],[],["leet"],[],[]]
-> [null,null,null,"hello","hello",null,"hello","leet"]
Constraints: 1 ≤ key.length ≤ 10; keys are lowercase letters; at most 5 · 10^4 calls.
Code runner format: LeetCode's design format — line 1 the method names, line 2 their arguments. The harness prints the list of results; the tests only ask for the maximum or minimum when exactly one key has it.