My first instinct was to use a HashSet for O(1) add/remove, but then I realized route() needs random access by index and a HashSet doesn't give you that cleanly.
Start by clarifying requirements and constraints, then design a data structure that supports O(1) add, remove, and random routing. Implement using an array with a hash map for index tracking, and discuss trade-offs like uniform randomness, capacity handling, and concurrency.
Pro tip: Emphasize the importance of uniform random selection and how the swap-with-last removal technique maintains O(1) operations while preserving uniformity. Also mention how you would handle edge cases like empty pool or full capacity.
Ask about expected load, concurrency needs, capacity cap behavior (e.g., reject or evict), and whether nodes have weights. Confirm that routing must be uniformly random.
Propose using a dynamic array to store node references for O(1) random access, and a hash map from node ID to array index for O(1) add/remove. Discuss how to maintain uniformity when removing.
Detail add: append to array and update map, checking capacity. Remove: swap with last element, pop, update map for swapped node, and remove from map. Route: generate random index and return node.
Address empty pool (return null or throw), full capacity (reject add), and thread-safety (e.g., use locks or concurrent structures). Discuss trade-offs of locking vs lock-free.
State that all operations are O(1) average time. Discuss memory overhead of hash map, and alternatives like reservoir sampling if nodes are added/removed frequently.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.