Count users over n connections
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Count users over n connections states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= number of events <= 100000 (the stream may also be empty)
- Each event is [user_a, user_b, op] with op in {"connect", "disconnect"}
- 0 <= n
- Users appear lazily — a user exists once referenced in any event
- Connections are undirected; connect/disconnect on an already-present/absent edge is idempotent (no double counting)
Examples
Input: ([['u1', 'u2', 'connect'], ['u2', 'u3', 'disconnect']], 0)
Expected Output: 2
Explanation: u1-u2 are connected (each degree 1). The u2-u3 disconnect targets an edge that was never created, so it is a no-op (u3 has degree 0). With n=0, u1 and u2 (degree 1 > 0) qualify, u3 does not. Answer: 2.
Input: ([['a', 'b', 'connect'], ['a', 'c', 'connect'], ['a', 'd', 'connect']], 2)
Expected Output: 1
Explanation: a connects to b, c, d (degree 3); b, c, d each have degree 1. Only a has degree > 2. Answer: 1.
Hints
- Maintain an adjacency set per user so that a repeated "connect" on the same pair does not increase the degree twice, and a "disconnect" on a non-existent edge is a no-op.
- A user's degree is simply the size of their adjacency set. After processing all events, count how many users have a set size strictly greater than n.
- Using sets keeps each connect/disconnect O(1) on average, so the whole stream is processed in linear time — important for up to 100,000 events.