Parse Requests and Compute Balances
Company: Robinhood
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates input parsing, state management, data validation, and use of basic data structures to track user accounts, friend relationships, and transactional balances under strict rule constraints.
Constraints
- 0 <= len(requests) <= 100000
- Each request string length is between 0 and 200 characters
- User IDs contain no spaces in well-formed requests
- Balances and transfer amounts fit in signed 64-bit integers
Examples
Input: ['SIGN_UP alice 100', 'SIGN_UP bob 50', 'REQUEST_FRIEND alice bob', 'ACCEPT_FRIEND bob alice', 'SEND_MONEY alice bob 30']
Expected Output: [['alice', 70], ['bob', 80]]
Explanation: Alice and Bob sign up, become friends, and Alice successfully sends 30 to Bob.
Input: ['SIGN_UP alice 10', 'SIGN_UP alice 20', 'SIGN_UP bob -5', 'REQUEST_FRIEND alice bob', 'SIGN_UP bob 15', 'REQUEST_FRIEND alice alice', 'REQUEST_FRIEND alice bob', 'REQUEST_FRIEND alice bob', 'ACCEPT_FRIEND alice bob', 'DECLINE_FRIEND bob alice', 'SEND_MONEY alice bob 5', 'SEND_MONEY alice bob x', 'SIGN_UP charlie 7 extra']
Expected Output: [['alice', 10], ['bob', 15]]
Explanation: Only the first SIGN_UP for Alice, the SIGN_UP for Bob with balance 15, and the later friend request followed by a valid decline are processed. No money transfer succeeds.
Input: ['SIGN_UP u1 20', 'SIGN_UP u2 0', 'SIGN_UP u3 50', 'REQUEST_FRIEND u1 u2', 'ACCEPT_FRIEND u2 u1', 'SEND_MONEY u1 u2 25', 'SEND_MONEY u1 u2 20', 'REQUEST_FRIEND u2 u3', 'ACCEPT_FRIEND u3 u2', 'SEND_MONEY u3 u2 10']
Expected Output: [['u1', 0], ['u2', 30], ['u3', 40]]
Explanation: The first transfer from u1 to u2 fails due to insufficient funds, but the next transfer succeeds. Then u2 and u3 become friends, and u3 sends 10 to u2.
Input: []
Expected Output: []
Explanation: No requests means no users and no balances to return.
Input: ['SIGN_UP zed 5', 'SIGN_UP amy 8', 'REQUEST_FRIEND zed amy', 'ACCEPT_FRIEND amy zed', 'SEND_MONEY amy zed 0', 'SEND_MONEY amy zed 3']
Expected Output: [['amy', 5], ['zed', 8]]
Explanation: Amy and Zed become friends. A transfer of 0 is invalid, but a transfer of 3 from Amy to Zed is valid. Output is sorted by user_id.
Hints
- Use hash maps to store balances and friendship sets so each valid request can be processed in near O(1) average time.
- Track pending friend requests as directed pairs like (from_user, to_user). Then ACCEPT_FRIEND and DECLINE_FRIEND become simple membership checks.