PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Square

Implement transaction network queries

Last updated: Mar 29, 2026

Quick Overview

This question evaluates the ability to model and query dynamic graph connectivity and reachability for pairwise transactions, testing competencies in graph representation, dynamic data structures, and reachability queries within the Coding & Algorithms domain and emphasizing practical application of algorithmic concepts.

  • hard
  • Square
  • Coding & Algorithms
  • Software Engineer

Implement transaction network queries

Company: Square

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem You are building a small in-memory library to track **pairwise customer transactions** and answer connectivity questions about who has transacted with whom. Model each transaction as an interaction between **exactly two** customers (e.g., `"Jim pays Pam"`). Once two customers transact, they are considered directly connected. Treat the relationship as **undirected** (if A transacts with B, then B has transacted with A). Design a class (e.g., `Transactions`) that supports the following capabilities. --- ## Part 1 — Direct/indirect existence query ("ever transacted together") Implement an operation to record a transaction between two customers, and a query: - `haveEverTransactedTogether(x, y) -> bool` Return `true` if and only if customers `x` and `y` have **ever** been part of the **same connected component** in the transaction graph **at the time of the query** (i.e., based on all transactions recorded so far). ### Example 1. `haveEverTransactedTogether(Jim, Pam) -> false` 2. Record: `Jim` transacts with `Pam` 3. `haveEverTransactedTogether(Jim, Pam) -> true` 4. Record: `Pam` transacts with `Michael` 5. `haveEverTransactedTogether(Michael, Jim) -> true` (because Michael—Pam—Jim forms a connected component) --- ## Part 2 — Full network query Implement: - `findNetwork(person) -> list/set` A person’s **network** is everyone who is reachable from that person via one or more transactions (i.e., all nodes in the same connected component), **excluding the person themself**. ### Example 1 (two disconnected components) Transactions: `(a,b), (b,c), (a,d), (d,e), (g,f)` - `findNetwork(a) -> {b,c,d,e}` - `findNetwork(g) -> {f}` ### Example 2 (loop) Transactions: `(a,b), (b,d), (d,c), (c,a)` - `findNetwork(a) -> {b,c,d}` --- ## Part 3 — Network query with degree limit Extend the previous method to accept a second parameter `n`: - `findNetwork(person, n) -> list/set` Return all customers within **at most `n` degrees of separation** from `person`, excluding `person`. - Degree 1: direct transaction partners - Degree 2: partners of partners, etc. ### Example 1 Transactions: `(a,b), (b,c), (a,d), (d,e), (g,f)` - `findNetwork(a, 1) -> {b,d}` - `findNetwork(g, 1) -> {f}` ### Example 2 (loop) Transactions: `(a,b), (b,d), (d,c), (c,a)` - `findNetwork(a, 1) -> {b,c}` ### Example 3 Transactions: `(a,b), (b,c), (c,d), (b,e), (c,f), (d,g), (f,g), (g,h)` - `findNetwork(d, 2) -> {c,g,b,f,h}` - `findNetwork(h, 3) -> {g,d,f,c}` - `findNetwork(a, 1) -> {b}` --- ## Notes / expectations - You may assume customer identifiers are strings. - The class should support adding transactions over time and answering queries at any point. - Clearly define any helper methods and choose appropriate data structures. - Be careful about cycles and repeated visits when exploring the network.

Quick Answer: This question evaluates the ability to model and query dynamic graph connectivity and reachability for pairwise transactions, testing competencies in graph representation, dynamic data structures, and reachability queries within the Coding & Algorithms domain and emphasizing practical application of algorithmic concepts.

Square logo
Square
Oct 19, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

Problem

You are building a small in-memory library to track pairwise customer transactions and answer connectivity questions about who has transacted with whom.

Model each transaction as an interaction between exactly two customers (e.g., "Jim pays Pam"). Once two customers transact, they are considered directly connected. Treat the relationship as undirected (if A transacts with B, then B has transacted with A).

Design a class (e.g., Transactions) that supports the following capabilities.

Part 1 — Direct/indirect existence query ("ever transacted together")

Implement an operation to record a transaction between two customers, and a query:

  • haveEverTransactedTogether(x, y) -> bool

Return true if and only if customers x and y have ever been part of the same connected component in the transaction graph at the time of the query (i.e., based on all transactions recorded so far).

Example

  1. haveEverTransactedTogether(Jim, Pam) -> false
  2. Record: Jim transacts with Pam
  3. haveEverTransactedTogether(Jim, Pam) -> true
  4. Record: Pam transacts with Michael
  5. haveEverTransactedTogether(Michael, Jim) -> true (because Michael—Pam—Jim forms a connected component)

Part 2 — Full network query

Implement:

  • findNetwork(person) -> list/set

A person’s network is everyone who is reachable from that person via one or more transactions (i.e., all nodes in the same connected component), excluding the person themself.

Example 1 (two disconnected components)

Transactions: (a,b), (b,c), (a,d), (d,e), (g,f)

  • findNetwork(a) -> {b,c,d,e}
  • findNetwork(g) -> {f}

Example 2 (loop)

Transactions: (a,b), (b,d), (d,c), (c,a)

  • findNetwork(a) -> {b,c,d}

Part 3 — Network query with degree limit

Extend the previous method to accept a second parameter n:

  • findNetwork(person, n) -> list/set

Return all customers within at most n degrees of separation from person, excluding person.

  • Degree 1: direct transaction partners
  • Degree 2: partners of partners, etc.

Example 1

Transactions: (a,b), (b,c), (a,d), (d,e), (g,f)

  • findNetwork(a, 1) -> {b,d}
  • findNetwork(g, 1) -> {f}

Example 2 (loop)

Transactions: (a,b), (b,d), (d,c), (c,a)

  • findNetwork(a, 1) -> {b,c}

Example 3

Transactions: (a,b), (b,c), (c,d), (b,e), (c,f), (d,g), (f,g), (g,h)

  • findNetwork(d, 2) -> {c,g,b,f,h}
  • findNetwork(h, 3) -> {g,d,f,c}
  • findNetwork(a, 1) -> {b}

Notes / expectations

  • You may assume customer identifiers are strings.
  • The class should support adding transactions over time and answering queries at any point.
  • Clearly define any helper methods and choose appropriate data structures.
  • Be careful about cycles and repeated visits when exploring the network.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Square•More Software Engineer•Square Software Engineer•Square Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.