← Akuna Capital Interview Insights
The hash map approach came to me pretty fast, user pointing to their partner.
Start by clarifying requirements and edge cases, then design the data structures and API contracts before coding. Implement the CommsHandler with a hash map for O(1) lookups, define ConnectionException with clear error messages, and write unit tests to validate all scenarios.
Pro tip: Mention that you would use a bidirectional mapping (e.g., two hash maps or a map of sets) to ensure O(1) operations and easy cleanup, and discuss how you'd extend it to support multiple connections per user if needed.
Ask questions to confirm expected behavior: Can a user have multiple connections? What happens if connect is called with an already connected user? Should hangup remove all connections for a user? Define the exact error conditions.
Choose a data structure that allows O(1) connect, hangup, and clear_all. For one-to-one, a hash map from user to their connected partner works; for multiple, use a map of sets. Define method signatures and ConnectionException.
Create a custom exception class that extends Exception (or RuntimeException) with constructors that accept a message and optionally a cause. Ensure it's serializable and provides clear error messages.
Write connect(userA, userB) to validate inputs (non-null, not self, not already connected), update the data structure, and throw ConnectionException on invalid states. Implement hangup(user) to remove the user's connection and update the partner's state. Implement clear_all() to reset the data structure.
Write unit tests covering normal flow, duplicate connections, self-connections, hanging up non-connected users, and clear_all. Discuss potential extensions like thread safety, persistence, or scaling to multiple connections.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.