Square (Block)Customer Transaction Network
Three-part CoderPad screen: (1) a class that records customer-to-customer transactions and answers whether two customers have ever transacted together; (2) findNetwork(customer) returning everyone transitively connected; (3) findNetwork(customer, n) limited to n degrees of separation. Build an undirected graph, then BFS, then BFS with depth.
Problem
Part 1
Create a class that tracks customer transactions. The class should identify which customers have transacted together at any given time.
Have Jim and Pam ever transacted together? -> No
Jim pays Pam
Have Jim and Pam ever transacted together? -> Yes
Pam pays Michael
Have Michael and Jim ever transacted together? -> NoPart 2
Given a customer, find their network. A person's network is considered everyone they have directly transacted with, all of the people their direct connections transacted with, and all of their 2nd degree connections transacted with, etc.
Part 3
Add a second parameter to the method made in the previous part,
n, which will limit the network size based on the number of degrees of separation away from the target individual.
Examples
Part 2:
Transactions t([a, b], [b, c], [a, d], [d, e], [g, f])
t.findNetwork(a) // [b, c, d, e]
t.findNetwork(g) // [f]
Transactions t([a, b], [b, d], [d, c], [c, a])
t.findNetwork(a) // [b, c, d]Part 3:
// isolated networks
Transactions t([a, b], [b, c], [a, d], [d, e], [g, f])
t.findNetwork(a, 1) // [b, d]
t.findNetwork(g, 1) // [f]
// network with loop
Transactions t([a, b], [b, d], [d, c], [c, a])
t.findNetwork(a, 1) // [b, c]
// large interspersed network
Transactions t([a, b], [b, c], [c, d], [b, e], [c, f], [d, g], [f, g], [g, h])
t.findNetwork(d, 2) // [c, g, b, f, h]
t.findNetwork(h, 3) // [g, d, f, c]Pitfalls
- Part 1 asks about direct transactions only; the "Michael and Jim -> No" example rules out transitive answers.
- The network excludes the starting customer, and the loop example (
a-b-d-c-a) needs a visited set to avoid revisiting. - Interviewers require refactoring the earlier code before adding the next part, so a Part 1 design that only stores pairs (rather than a graph) costs time later.
- Writing your own tests for every part eats a large share of the hour — keep test helpers short and reusable.
Follow-ups
n degrees of separation (Part 3).Preparation
- Implement the full three-part class in one sitting under 45 minutes, including tests for every example above.
- Write depth-limited BFS from memory and check it against the "large interspersed network" example.
