This one took me a second to scope properly.
Start by clarifying requirements and constraints, then propose a core data structure that supports O(log n) operations for all APIs. Use a combination of hash maps and balanced trees (or skip lists) to manage per-user balances and global ordering, and carefully handle concurrency with fine-grained locking or lock-free techniques.
Pro tip: Emphasize the trade-offs between different data structures (e.g., skip list vs. balanced BST) and locking strategies (e.g., per-user locks vs. global lock) to demonstrate depth. Also, discuss how to handle expiration efficiently without scanning all users.
Ask questions to understand expected scale (number of users, operations per second), consistency requirements, and whether expiration is lazy or active. Confirm that O(log n) is per operation and n is number of users.
Propose a hash map for O(1) user lookup and a balanced BST or skip list for maintaining global order by balance. For org-level caps, consider a separate map from org to aggregate balance and a tree for org ordering if needed.
For each API (grant, consume, refund, getBalance, topK), write pseudocode showing how to update the user's balance, adjust the ordered structure, and enforce caps and expiration. Include checks for negative balances and cap violations.
Choose a locking strategy: per-user locks for balance updates, and a global lock for the ordered structure, or use a concurrent skip list. Discuss trade-offs between coarse and fine-grained locking.
Analyze time and space complexity for each operation, highlighting O(log n) for updates and topK. Discuss alternative designs (e.g., using a heap for topK) and their limitations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.