Construct smallest number from I/D pattern
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to reason about permutation constraints, sequence ordering, and combinatorial construction under digit-uniqueness and numeric-minimization requirements.
Part 1: Bank Ledger Processor
Constraints
- 0 <= number of accounts <= 10^4
- 0 <= len(operations) <= 10^5
- All balances and amounts are integers
- Valid amounts must be > 0
Examples
Input: ({'A': 100, 'B': 50}, [('deposit', 'A', 20), ('withdraw', 'B', 10), ('transaction', 'A', 'B', 30), ('balance', 'A'), ('balance', 'B')])
Expected Output: [120, 40, (90, 70), 90, 70]
Explanation: Processes all four operation types successfully.
Input: ({'A': 10}, [('withdraw', 'A', 15), ('deposit', 'C', 5), ('transaction', 'A', 'A', 1), ('balance', 'C')])
Expected Output: [-1, -1, -1, -1]
Explanation: Insufficient funds, missing account, self-transfer, and querying a missing account are all invalid.
Input: ({'X': 0}, [('deposit', 'X', 1), ('withdraw', 'X', 1), ('balance', 'X')])
Expected Output: [1, 0, 0]
Explanation: Shows a zero starting balance and a full round trip back to zero.
Input: ({}, [])
Expected Output: []
Explanation: Edge case with no accounts and no operations.
Hints
- A hash map from account ID to current balance lets each operation run in constant time.
- For invalid operations, be careful not to modify any balance before all checks pass.
Part 2: Minimum Operations in the Water Jug Problem
Constraints
- 0 <= jug1, jug2 <= 100
- 0 <= target <= 100
- The target must appear in one jug exactly, not just as the combined total
- The starting state is always (0, 0)
Examples
Input: (3, 5, 4)
Expected Output: 6
Explanation: One shortest path is fill 5, pour to 3, empty 3, pour to 3, fill 5, pour to 3.
Input: (2, 6, 5)
Expected Output: -1
Explanation: It is impossible to get exactly 5 liters in either jug.
Input: (1, 1, 0)
Expected Output: 0
Explanation: Edge case: target 0 is already satisfied at the start.
Input: (0, 5, 5)
Expected Output: 1
Explanation: Fill the 5-liter jug once.
Hints
- Treat each state (a, b) as a node in a graph, where a and b are the current amounts in the two jugs.
- Because every operation has equal cost, breadth-first search gives the shortest number of steps.
Part 3: Construct the Smallest Number from an I/D Pattern
Constraints
- 0 <= len(pattern) <= 8
- pattern contains only 'I' and 'D'
- Use digits 1 through len(pattern) + 1 exactly once
Examples
Input: 'IIDDD'
Expected Output: '126543'
Explanation: This is the smallest arrangement of digits 1 through 6 that matches the pattern.
Input: 'D'
Expected Output: '21'
Explanation: A single decreasing relation requires the smallest descending pair.
Input: ''
Expected Output: '1'
Explanation: Edge case: no relations means the smallest available single digit.
Input: 'IDID'
Expected Output: '13254'
Explanation: The stack-based construction yields the lexicographically smallest valid result.
Hints
- A run of consecutive 'D' characters forces a descending block in the answer.
- Try pushing digits to a stack and flushing the stack whenever you see an 'I' or reach the end.
Part 4: Group Chat Unread Counter
Constraints
- 0 <= len(operations) <= 10^4
- Group IDs and user IDs are strings
- Total users stored across all groups is at most 10^4
- For read operations, k is an integer and must be >= 0 to be valid
Examples
Input: [('create', 'g1', ['A', 'B', 'C']), ('send', 'g1', 'A'), ('unread', 'g1', 'B'), ('read', 'g1', 'B', 1), ('add', 'g1', 'D'), ('send', 'g1', 'C'), ('unread', 'g1', 'D')]
Expected Output: [3, 2, 1, 0, 4, 3, 1]
Explanation: Shows creation, sending, reading, adding a user, and unread queries.
Input: [('send', 'g1', 'A'), ('create', 'g1', ['A', 'A']), ('create', 'g1', []), ('unread', 'g1', 'A')]
Expected Output: [-1, -1, 0, -1]
Explanation: Edge cases: sending to a missing group, duplicate users on creation, creating an empty group, and querying a non-member.
Input: [('create', 'g', ['A', 'B']), ('send', 'g', 'A'), ('remove', 'g', 'B'), ('add', 'g', 'B'), ('unread', 'g', 'B')]
Expected Output: [2, 1, 1, 2, 0]
Explanation: Unread state is discarded when a user leaves and resets to 0 on rejoin.
Input: [('create', 'solo', ['U']), ('send', 'solo', 'U'), ('read', 'solo', 'U', 5), ('unread', 'solo', 'U')]
Expected Output: [1, 0, 0, 0]
Explanation: A one-person group can send messages, but no one else receives them.
Hints
- Use one hash map to store each group's current members and another to store unread counts per user.
- When a user leaves and later rejoins, their unread count should restart at 0.